Expand description
SQL importer for Quarb.
Translates a SQL SELECT statement into an equivalent Quarb
query, following the mapping in the SQL cookbook — in reverse.
The translatable subset covers the single-statement query core;
everything else is refused with an
SqlError::Unsupported naming the construct, never silently
approximated.
SELECT cols FROM t→/t/* | rec(::col, …);SELECT *→ the row nodes;SELECT DISTINCT col→/t/*::col @| unique.WHERE→ a predicate: comparisons,AND/OR/NOT,IS [NOT] NULL→ truthiness,IN (…)→ anor-chain,LIKE→*=for%x%,=~anchors forx%/%x.- Aggregates (
COUNT(*),COUNT(col),SUM/AVG/MIN/MAX) →@|reductions;GROUP BY k→@| group(::k)with the aggregate riding the plain pipe andHAVINGa filter after it. JOIN t2 ON t2.b = t1.a→ correlation:/t1/* <=> /t2/*[::b = $*1::a], with two-sided select lists projected through the witness ($*1::col).ORDER BY→@| sort_by(…)(DESCvia@| reverse, or a numeric-key when mixed);LIMIT n→@| [..n].
Refused: subqueries, UNION, window functions, CASE,
multi-join chains (one JOIN translates), aggregate arithmetic
(SUM(a) + 1), and any non-SELECT statement.
Known semantic divergences are reported as
Translation::notes rather than errors:
- Quarb’s
countcounts all rows (SQLCOUNT(col)skips NULLs; the translation adds the dropna filter, and says so). - Quarb streams records (JSONL), not a result table.
LIKEtranslations are case-sensitive; MySQL’s default collation is not.
Structs§
- Partial
- A partial-pushdown plan: the leading row predicates as a WHERE
clause for
table’s fetch. The caller runs the original query against the filtered adapter — the engine re-applies the pushed predicates (a no-op on pre-filtered rows), so no rewriting. - Pushdown
- A pushdown plan: SQL whose execution is provably identical to
the Quarb query’s — plus the table whose primary key must order
the rows (
Nonefor a single aggregate row, where order is moot). The driver appends theORDER BY, since the key lives in its catalog. - Translation
- A successful translation: the Quarb query, plus notes on known semantic divergences that apply to it.
Enums§
- SqlError
- An error translating a SQL statement.
Functions§
- export
- Translate a Quarb query to a SQL
SELECTstatement. - partial_
pushdown - Attempt a partial pushdown:
Somewhen the query’s leading row predicates translate strictly and the rest of the query provably cannot observe the filtering. The gates, each with its reason: a single/table/*branch with no correlations (other shapes address other data); only the leading run of expression predicates pushes (a positional predicate before an expression one sees unfiltered rows on the scan path); the table’s name appears exactly once among the query’s steps (a second reach —^-anchored subcontexts, self-references — would see the filtered subset); no crosslink or resolution axes anywhere (backlinks and reverse resolution into the table would too); and no:::/::;metadata anywhere (a filtered::;n-rowswould lie). - partial_
pushdown_ explained partial_pushdown, keeping the refusal reason.- pushdown
- Attempt the pushdown translation:
Someonly when every construct in the query is in the verified-safe set. Anything else — including everythingexportwould merely annotate with a divergence note — returnsNone, and the caller scans. - pushdown_
explained pushdown, keeping the refusal: the error names the first construct that kept the query on the scan path.- translate
- Translate one SQL
SELECTstatement to a Quarb query.