7 Commits

Author SHA1 Message Date
Pekka Enberg
13d2488c76 docs: describe which SQL types jsonb_extract returns
The docs said jsonb_extract always returns BLOB. Scalars come back as
INTEGER, REAL, TEXT or NULL; only arrays, objects and multi-path
results come back as JSONB blobs.
2026-10-12 08:43:52 +03:00
Jussi Saurio
638d7eaf01 core: add nth_value() window function
Add nth_value(value, position), which returns the value at a chosen place in the ordered rows.

Read the position again for every result row, as SQLite does. This lets the position change from row to row, even when rows have equal sort values. Check the position only when returning that row, so LIMIT and OFFSET handle errors the same way as SQLite.

Reject whole-number decimals that are too large instead of letting rounding turn them into a valid position.
2026-07-22 10:54:35 +03:00
Glauber Costa
2d671ede9b docs: refresh SQL reference for Turso 0.7
Fill the gaps in docs/sql-reference that were still stale after the 0.7
release. Verified every claim against the v0.7.0 source (COMPAT.md,
core/function.rs, the parser AST, and cli/).

- window: row_number() and FILTER (WHERE ...) are supported now
- scalar: add unistr, unistr_quote, turso_version()
- vector: add vector32_sparse; note the experimental sparse vector index
- transactions: document SAVEPOINT / RELEASE / ROLLBACK TO
- add REINDEX statement page
- create-trigger: drop the experimental banner (triggers are stable);
  mark INSTEAD OF as not yet supported
- create-table: reclassify WITHOUT ROWID and GENERATED as experimental
  flags; note locale/custom collations
- create-index: document COLLATE and link REINDEX
- select: correct the window ranking note (row_number available)
- experimental-features: triggers are no longer experimental; add
  generated_columns, without_rowid, mvcc_passive_checkpoint
- extensions: document the crypto, fuzzy, and ipaddr extensions
- pragmas: add data_sync_retry, require_where/i_am_a_dummy,
  mvcc_checkpoint_threshold, mvcc_gc_threshold
- cli: drop --experimental-triggers; add the new experimental flags
2026-07-15 09:16:37 -05:00
Glauber Costa
698bf89663 docs/sql-reference: document new functions and PG aliases
Adds reference entries for the new scalar functions and PostgreSQL-
compatible aliases:

  * math.mdx:    gcd(A, B), lcm(A, B)
  * array.mdx:   array_upper, and the now-honored dimension argument
                 to array_length
  * scalar.mdx:  repeat(X, N), lpad/rpad (2- and 3-arg forms),
                 reverse / string_reverse, plus the PG-compatible
                 aliases chr, strpos, btrim, char_length, character_length

Each new function gets a table entry in the overview and a dedicated
detail section with semantics (NULL propagation, overflow, character-
vs-byte counts, sign handling) and runnable examples. Aliases are
folded into the canonical function's section rather than duplicated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-14 15:32:50 -05:00
Glauber Costa
1252875f5a feat(core): add WITHIN GROUP ordered-set aggregates
Implement the SQL-standard ordered-set aggregate syntax
`f(args) WITHIN GROUP (ORDER BY x)` for mode(), percentile_cont() and
percentile_disc() as first-class core aggregates, documented under
docs/sql-reference/functions/aggregate.mdx.

WITHIN is a context-sensitive keyword (only after `)` + GROUP), so it
remains usable as an identifier. The planner rewrites the ordered-set
form into uniform aggregate args and rejects unsupported variants
(DESC/NULLS, multi-column ORDER BY, DISTINCT) with clear errors rather
than returning wrong results.

The percentile fraction is a direct argument: it must be constant with
respect to the aggregated rows. A reference to the query's own input
columns (directly or via a subquery) is rejected, while an outer
(correlated) column is allowed, matching PostgreSQL. A constant fraction
is folded and range-checked at compile time, so an out-of-range fraction
errors regardless of row count (empty / all-NULL / all-filtered input).

Text ordered sets are collation-aware: mode() and percentile_disc() sort
using the value's collation (explicit COLLATE, column-declared collation,
or the BINARY default), recorded in the aggregate payload at step time
and applied when sorting at finalize.

Validated against PostgreSQL 18 across the accept-path plus corner cases
(subqueries, CTEs, FILTER, HAVING, correlated references including an
outer-column fraction, constant-expression fractions, expression/CASE
order keys, and attached databases). The BINARY default matches PG
`COLLATE "C"`; NOCASE / column collations are honored consistently with
ORDER BY. Extension-registered Custom collations fall back to BINARY at
finalize, consistent with the engine's other connection-less comparison
paths.

Tests wrap percentile_cont (REAL) results in CAST(... AS TEXT) so the
whole-number float comparison is stable across the rust, cli and js test
backends (the js backend renders 3.0 as "3").

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 20:42:32 -05:00
Glauber Costa
0bc101a6b3 feat: add array column type support with PostgreSQL-style array functions
Adds array column types (e.g. INTEGER[], TEXT[][]) stored as record-format
blobs with zero-copy ValueIterator streaming. Includes ARRAY[] constructor,
subscript access, comparison operators (@>, &&, =, <, etc.), 12 scalar
functions (array_length, array_append, array_cat, etc.), and array_agg
aggregate. Parser extended with ARRAY keyword, bracket subscripts, and
new operator tokens.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 10:19:39 -04:00
Glauber Costa
b3eeabf38d add SQL language reference documentation
Comprehensive SQL reference at docs/sql-reference/ covering all
supported statements, functions, expressions, PRAGMAs, and
extensions. Includes mdBook-based local preview (./preview.sh).

Turso-specific features (CDC, materialized views, custom types,
MVCC, vector search, FTS, encryption) are integrated inline with
callouts. Experimental features link to a dedicated page with
enablement instructions for the CLI and all SDKs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 14:05:32 -06:00