Skip to main content

Module joins

Module joins 

Source
Expand description

Multi-table JOIN support scaffolding.

This module introduces two helpers that per-dialect parsers (postgres, mysql, sqlite) can call when they detect a JOIN in a query:

  • AliasMap — a lookup from alias-or-table-name to a TableDef reference, built by scanning the FROM/JOIN clauses.
  • parse_join_clauses — walks a query’s FROM clause, returning the alias map. Errors on OUTER/USING/NATURAL joins with a pointer to the v1.2 roadmap.
  • resolve_multi_table_select_column — given a qualified select expression like users.id or u.name AS username, look up the table and column in the alias map and return a fully-typed ColumnDef with source_table populated.

The helpers are not yet wired into any dialect parser. The existing ensure_supported_select_expr guard still rejects qualified selects in every dialect. A follow-up PR per dialect (postgres, mysql, sqlite) will flip each to call into these helpers when JOIN clauses are present.

Scope for v1.1: INNER JOIN only, qualified columns only, no SELECT * across joins. OUTER JOIN nullability propagation, USING, NATURAL JOIN, lateral joins, and self-joins with aliases are v1.2 work — they would require ColumnDef.nullable to become per-query-context rather than per-schema.

Structs§

AliasMap
Maps alias (or bare table name) to the underlying TableDef. Both the alias and the table name are valid qualifiers for a column, so both are stored when an alias is present.

Functions§

has_outer_join
Returns true if the query’s outer FROM clause contains a JOIN. Run this instead of matching \bJOIN\b against the full SQL — matching against the full SQL false-positives on JOINs inside subqueries.
parse_join_clauses
Walk a query’s FROM clause and return the alias → table mapping. Returns an empty map (no join detected) when the query has no FROM clause. Returns an error for OUTER / USING / NATURAL / CROSS joins with a message pointing to the v1.2 roadmap.
resolve_multi_table_columns
Resolve a SELECT column list against a multi-table JOIN context. Shared across dialect parsers (postgres, mysql, sqlite): they detect the JOIN via has_outer_join, pull the columns-part out of the SELECT, and call this function to build the typed ColumnDef list.
resolve_multi_table_select_column
Resolve a qualified select expression (like users.id or u.name AS username) against an AliasMap. Returns a fully-typed ColumnDef with source_table populated so codegen can disambiguate colliding names.