SIGN IN SIGN UP

Merge upstream SQLite 3.47.0

This merges the version-3.47.0 tag (f5fb820c0f "Version 3.47.0") of
upstream SQLite into libSQL. Conflicts were resolved with rename-following
(libsql vendors SQLite under libsql-sqlite3/, while upstream paths are at
the root), using a large merge.renameLimit so the ~2000 top-level ->
libsql-sqlite3/ moves are all detected:

    git -c merge.renameLimit=999999 -c diff.renameLimit=999999 \
        merge --no-commit version-3.47.0

Only files under libsql-sqlite3/ are changed by this merge. The
libsql-ffi/ bundled amalgamation and the SQLite3MultipleCiphers tree are
intentionally left untouched here; regenerating the bundle is a separate
step (cargo xtask build-bundled + scripts/update-sqlite-bundle.sh).

== Renames / path moves ==

* ext/wasm reorganization. Upstream renamed several wasm API sources to
  the ".c-pp.js" preprocessing convention and split the exported-symbol
  list. We followed upstream's layout (the new GNUmakefile / mkwasmbuilds.c
  expect it), and the rename/rename content merge preserved libsql's local
  additions inside the renamed files:
    sqlite3-api-glue.js     -> sqlite3-api-glue.c-pp.js
    sqlite3-api-oo1.js      -> sqlite3-api-oo1.c-pp.js   (keeps libsql's
                               DB.getAutocommit()/lastInsertRowid())
    sqlite3-api-worker1.js  -> sqlite3-api-worker1.c-pp.js
    EXPORTED_FUNCTIONS.sqlite3-api -> EXPORTED_FUNCTIONS.sqlite3-core
  plus new files accepted: EXPORTED_FUNCTIONS.sqlite3-extras,
  mkwasmbuilds.c.
* src/test_vfstrace.c -> ext/misc/vfstrace.c (upstream relocated it; no
  libsql changes, followed upstream).
* autoconf/tea/README -> autoconf/tea/README.txt (upstream replacement;
  libsql had no local changes).
* ext/wasm/fiddle/emscripten.css deleted by upstream, but kept here
  because libsql's fiddle/index.html still references it (libsql enables
  the jquery.terminal view and "libSQL" branding).

== Resolved conflicts ==

src/parse.y
  Combined: kept libsql's FUNCTION/LANGUAGE tokens (CREATE/DROP FUNCTION
  ... LANGUAGE, the WASM-UDF grammar) and added upstream's new ISNOT token
  (used by the auto-merged token-class list).

src/where.c
  Kept libsql's vector-index planner guard
  `|| pIndex->idxIsVector` in the ORDER-BY-satisfiability check, and took
  upstream's removal of the now-redundant standalone `nColumn = 1;`. The
  latter is required, not cosmetic: upstream relocated that assignment into
  the new SQLITE_OrderBySubq inner if/else, so keeping libsql's old line
  would have unconditionally clobbered the nColumn=0 that the new
  subquery-ORDER-BY optimization sets.

src/os_unix.c (3 hunks)
  Kept libsql's `#ifndef SQLITE_OMIT_SHARED_MEM` guard around the unix shm
  machinery (forward decl, SQLITE_FCNTL_EXTERNAL_READER, the shm
  implementation block). Upstream had changed those same lines to
  `#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL)`. libsql gates
  the whole shm subsystem on SQLITE_OMIT_SHARED_MEM (its "WAL without
  shared memory" design); the surrounding ~10 guards and the matching
  #endif already use that symbol, so all three must too. libsql's WASI/wasm
  builds define SQLITE_OMIT_SHARED_MEM, which subsumes upstream's WASI
  condition.

src/wal.c, src/wal.h
  Took libsql's side for both wal.c hunks: the `struct Wal` definition
  stays out of wal.c (libsql exposes it from wal.h as `struct sqlite3_wal`
  for the virtual-WAL interface), and the SQLITE_USE_SEH path keeps libsql's
  `#error "SEH is not supported in libSQL ..."` (SEH is incompatible with
  the virtual WAL). Upstream 3.47.0 added one new field to struct Wal,
  `int bGetSnapshot;` (SQLITE_ENABLE_SNAPSHOT); since the in-file struct is
  dropped on libsql's side, that field was added to wal.h's
  struct sqlite3_wal so SQLITE_ENABLE_SNAPSHOT builds still compile (the
  auto-merged wal.c references pWal->bGetSnapshot in 3 places).

src/sqlite.h.in
  Took upstream's deletion of the `# define SQLITE_OMIT_WAL 1` lines in the
  `#if defined(__wasi__)` block. libsql had previously commented those
  lines out with a NOTICE; upstream removed them outright. Same end state
  (WAL not forced off for WASI), so the libsql NOTICE/commented-out lines
  are no longer needed.

src/shell.c.in (4 hunks)
  Upstream refactored the shell's output API from oputf()/sputf() to
  sqlite3_fprintf() and removed the old helpers entirely. Re-expressed
  libsql's customizations in the new API rather than keeping the now-
  undefined oputf/sputf calls: the "libSQL %s (based on SQLite %s)" version
  banners (libsql_libversion()) in the .version meta-command, the -version
  flag and the interactive startup line, plus the LIBSQL_STMTSTATUS_ROWS_READ
  / ROWS_WRITTEN prepared-statement counters.

src/test3.c
  Took upstream's consolidated `#include "tclsqlite.h"` (the new header),
  and kept libsql's `#include "wal.h"` (needed for libsql_wal_methods,
  referenced in this file).

src/vacuum.c
  Semantic merge fix. Upstream 3.47.0 changed the VACUUM temp database from
  the fixed name "vacuum_db" to a random "vacuum_%016llx" (zDbVacuum). That
  rename landed in the ATTACH and the auto-merged data-copy, but libsql's
  local "#ifndef SQLITE_OMIT_VECTOR" block (which special-cases vector
  shadow tables) still emitted INSERT SQL against the literal "vacuum_db" -
  a database that no longer exists, so every VACUUM failed with
  SQLITE_ERROR. Updated libsql's vector block to use %s/zDbVacuum, matching
  upstream's #else branch. (Caught by running the Tcl suite; a pure diff
  review missed it because both sides changed adjacent code.)

ext/fts5/{fts5_main.c,fts5_tokenize.c} and fts5 tests
  Aligned to upstream verbatim. libsql's local fts5 changes here were
  superseded by upstream: the pTokApi field (libsql's way of storing the
  tokenizer API) is now provided natively by upstream's Fts5TokenizerConfig
  (t.pApi1/pApi2) - and fts5Int.h/fts5_config.c had already auto-merged to
  that, so taking upstream in fts5_main.c was required for the build, not
  just preferred. The trigram tokenizer odd-argument fix was likewise
  superseded by upstream's up-front `if( nArg%2 )` guard. The corresponding
  test expectations ("no such tokenizer" -> "SQL logic error", new
  fts5secure8 case) follow.

libsql-sqlite3/Makefile.in (8 hunks)
  Adopted upstream's restructuring - the $(libtclsqlite3.la_$(HAVE_TCL)) /
  $(tcl_install_$(HAVE_TCL)) idioms, the new tclextension-install/uninstall/
  list targets, the tidy:/clean:/distclean: split, sqldiff depending on
  ext/misc/sqlite3_stdio.h, src-verify$(BEXE), and dropping dbfuzz2-asan/msan
  - while preserving every libsql target and flag: liblibsql.la,
  liblibsql_wasm, libsqlapi, testlibsql, rusttest, rusttestwasm, libsql.pc,
  $(OPT_STATIC_LIBLIBSQL_WASM), and libsql-specific cleanup entries.

ext/wasm/{GNUmakefile,fiddle.make,fiddle/index.html,api/sqlite3-opfs-async-proxy.js,api/sqlite3-vfs-opfs.c-pp.js}
  Took upstream's reorganized structure (build-rule generation moved into
  mkwasmbuilds.c) while keeping libsql's c-pp flags (-DSQLITE_OMIT_SHARED_MEM,
  -lm) and the wasm-opt/maybe-wasm-shrink size-optimization definition. The
  opfs JS conflicts were upstream cleaning up its own code (operand-order,
  dead else) - no libsql logic. NOTE: libsql's wasm-opt/gzip weaving into
  the old build recipes is dropped, since upstream replaced those recipes
  with generated ones. None of ext/wasm is built in libsql CI.

manifest, manifest.uuid, VERSION, configure, autoconf/tea/configure.ac,
doc/compile-for-windows.md, README.md
  Mechanical. manifest / manifest.uuid taken verbatim from upstream 3.47.0
  (libsql tracks them as-is); VERSION -> 3.47.0; configure and tea
  configure.ac version-bumped to 3.47.0 (configure's only conflicts were
  version strings); compile-for-windows.md took upstream's TCL 9.0 docs;
  top-level README.md kept libsql's.

== Rename-misrouting fix ==

Four files exist twice in the libsql tree - once under libsql-sqlite3/ and
once under libsql-ffi/bundled/SQLite3MultipleCiphers/src/ - and both copies
were byte-identical to the merge base. git's rename detection paired the
base files with the (alphabetically first) libsql-ffi copies, so upstream's
3.47.0 changes were applied to the cipher-bundle copies while the real
libsql-sqlite3/ sources were left at 3.46.1. Corrected by reverting the
libsql-ffi copies to pre-merge HEAD (the bundle is updated out-of-band) and
syncing the libsql-sqlite3/ sources to upstream 3.47.0 (none carry libsql
changes - tclsqlite.c/shathree.c were verbatim upstream, fileio.c differed
only in whitespace, series.c is version-tracked):
    libsql-sqlite3/src/tclsqlite.c        (Tcl 9.0 support: Tcl_Size, etc.)
    libsql-sqlite3/ext/misc/shathree.c
    libsql-sqlite3/ext/misc/series.c
    libsql-sqlite3/ext/misc/fileio.c

== Validation ==

testfixture builds against Tcl 9.0 (pre-merge HEAD does not - exactly the
upstream tclsqlite.c fix restored above) and `make test` passes the full
Tcl suite with a single exception, vector-index-v2-query-4. That test is a
pre-existing over-specified assertion against an exact cosine-distance tie
(the query vector is equidistant from rows 'b' and 'c'); it runs against a
frozen, md5-locked fixture using vector code that this merge does not touch,
so the result is independent of this merge. Left as-is for a separate fix.
P
Pekka Enberg committed
ebd1563f9596fefd0235fcde386d56f84c9d1547