Skip to content

Feature/using join#2822

Open
stefanoperenzoni wants to merge 2 commits into
malloydata:mainfrom
stefanoperenzoni:feature/using-join
Open

Feature/using join#2822
stefanoperenzoni wants to merge 2 commits into
malloydata:mainfrom
stefanoperenzoni:feature/using-join

Conversation

@stefanoperenzoni
Copy link
Copy Markdown
Contributor

Description

This PR implements the USING clause syntax for joins in Malloy. 90% agent generated.

Previously, users were forced to use the with or on keywords to define join conditions (e.g., join_one: aircraft_models on aircraft_model_code = aircraft_models.aircraft_model_code). With this change, users can succinctly join tables sharing the same column names using the SQL standard USING syntax.

Syntax supported:
```malloy
source: aircraft extend {
join_one: aircraft_models using (aircraft_model_code)
}

run: table1 extend {
join_one: table2 using (id, code)
} -> { select: id, code, val1, table2.val2 }
```

What this PR does

  • Parser Updates: Added USING rules to MalloyLexer.g4 and MalloyParser.g4.
  • AST/IR: Created a UsingJoin node (join.ts) which generates an internal equivalence relation so that Malloy's semantic engine understands the lineage of the fields.
  • SQL Generation:
    • Modified query_query.ts to output valid USING (col1, col2) SQL.
    • Added logic to automatically push extra join filters (where: conditions on the joined source) into a subquery, preventing invalid SQL since AND cannot be appended to a USING clause.
  • Alias Prefix Stripping: Updated field_instance.ts to automatically strip table aliases from columns involved in an active USING join. Because dialects like BigQuery coalesce USING columns, fully qualifying them (e.g. base_table.id) results in query failures.
  • Testing: Added thorough integration tests in join.spec.ts to cover single-column joins, multi-column joins, source-level refinements, and inline query joins.

Related Issues

Resolves #2541

Checklist

  • Code compiles correctly
  • Linter (npm run lint) passes
  • Tests (npm run test) pass
  • Developer Certificate of Origin (DCO) sign-off is included

@mtoy-googly-moogly mtoy-googly-moogly self-requested a review May 21, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: USING key word in queries join

1 participant