Skip to main content

Crate quarb_sql

Crate quarb_sql 

Source
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 (…) → an or-chain, LIKE*= for %x%, =~ anchors for x% / %x.
  • Aggregates (COUNT(*), COUNT(col), SUM/AVG/MIN/MAX) → @| reductions; GROUP BY k@| group(::k) with the aggregate riding the plain pipe and HAVING a 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(…) (DESC via @| 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 count counts all rows (SQL COUNT(col) skips NULLs; the translation adds the dropna filter, and says so).
  • Quarb streams records (JSONL), not a result table.
  • LIKE translations 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 (None for a single aggregate row, where order is moot). The driver appends the ORDER 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 SELECT statement.
partial_pushdown
Attempt a partial pushdown: Some when 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-rows would lie).
partial_pushdown_explained
partial_pushdown, keeping the refusal reason.
pushdown
Attempt the pushdown translation: Some only when every construct in the query is in the verified-safe set. Anything else — including everything export would merely annotate with a divergence note — returns None, 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 SELECT statement to a Quarb query.