Skip to main content

Module sqlselect

Module sqlselect 

Source
Expand description

A real SQL SELECT engine — expressions, aliases, CASE, functions, joins.

§Why this module exists

The pgwire layer translates SQL text into NQL text. That works beautifully for SELECT col FROM t WHERE x = 1, and it cannot be stretched any further: NQL has no expressions, no table aliases, no CASE, no scalar functions and no joins. Those are not missing features of the translation — they are things the target language cannot say.

And they are exactly what catalogue introspection is made of. psql’s \dt is one statement containing a two-table LEFT JOIN, a nine-branch CASE, two scalar function calls, four qualified column references, an IN list, a !~ regex and ORDER BY 1,2. Every one of those has to work or the command does not.

So this is a small but genuine SQL evaluator: lexer, parser, expression evaluator, nested-loop join. It operates over rows supplied by a callback, which is what lets the same engine serve synthesised catalogue relations today and stored collections later.

§What it is NOT

It is not a query planner and does not pretend to be. The join is a nested loop, which is honest for catalogue relations (tens of rows) and would be wrong to point at a large collection without an index strategy. That boundary is enforced by the caller, not hidden here.

§The rule this module follows

Anything it cannot evaluate is REFUSED with an error naming the construct. It never guesses. A catalogue query that silently returns the wrong rows produces an empty or wrong table list, and a wrong table list is indistinguishable from a correct one until somebody’s data appears to be missing.

Structs§

Bound
One row of a (possibly joined) result: an ordered list of (binding, row-or-NULL).
Join
Opts
Execution options. Every switch exists so a differential test can run the SAME query with the optimisation on and off and compare — without that, a test believing it exercised an optimisation could be measuring the unoptimised path, and the equivalence suite would prove nothing.
OrderBy
OutCol
One output column: the key it is stored under, and the name the client sees.
Select
SelectItem
TableRef
VecRelation
A relation backed by an already-materialised Vec.

Enums§

Dir
Expr
JoinKind
Tok

Traits§

Relation
A relation, delivered one row at a time.

Functions§

eval
Evaluate an expression against one bound row.
execute
Run a parsed SELECT, returning (column names, rows).
execute_explain
Run a parsed SELECT, also reporting how each join was executed.
execute_opts
The full form.
execute_with
As execute_explain, with predicate pushdown switchable.
from_vec
Wrap a materialised relation.
lex
parse
Parse one SELECT statement.
run
Parse and run in one call.

Type Aliases§

Resolver
Everything one execution needs from the outside world.