fix(sql): allow JOINs without an explicit alias on the joined table#97
Merged
Conversation
Closes #95. `Join.validate()` rejected any join whose joined table had no alias — but standard SQL doesn't require one. The bare table name is a valid qualifier (PostgreSQL, MySQL, DuckDB, ClickHouse, SQL Server). Removing the rule surfaces two latent issues that have to be fixed together: 1. `From.scala` — `Join.validate()` Drop the two unconditional alias-required branches (the explicit alias match + the redundant `case j if alias.isEmpty` arm in the second match). Keep the ON-clause guard for non-CROSS joins. The downstream sites that key off the join alias already fall back to the source name when no alias is set (`From.tableAliases`, `From.joinAliases`, `From.unnestAliases`, `JoinKey.apply`, `Unnest.innerHitsName`), so no call site needs to change. 2. `From.scala` — `StandardJoin.update()` Stop calling `source.update(request)`. The source of a JOIN is a TABLE name, not a column expression. With the alias requirement gone, `From.tableAliases` now contains a `customers → customers` self-mapping for the alias-less join — which made `GenericIdentifier.update` treat `name="customers"` as `alias.column`, take `parts.tail.mkString(".")` of a single-part name, and end up with `name=""`. The ON clause still gets `.update(request)` so column resolution within ON works. 3. `Parser.scala` — `reservedKeywords` Add `inner`, `left`, `right`, `full`, `cross`, `outer`, `on`. The alias regex's negative-lookahead filter excluded `from`/`join`/etc. but not the join-type keywords — so without an alias on `orders`, the regex greedily consumed `LEFT` as the alias, leaving the `join` rule to parse a junk JOIN with no joinType. Functions like `LEFT(x, 5)` and `RIGHT(x, 3)` are parsed by their dedicated function parsers (not via `identifierRegex`/`regexAlias`), so they continue to work — verified by ParserSpec line 225's SELECT that exercises both. ParserSpec gets three regression cases: - parses `LEFT JOIN customers ON orders.customer_id = customers.id` and exposes `join.alias = None` and `join.source.name = "customers"`. - alias-less joined table is reachable in `From.tableAliases` by its bare name (`"customers" -> "customers"`). - a JOIN with no ON clause still fails — only the alias rule was relaxed, the ON rule was not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #95.
Join.validate()rejected any join whose joined table had no alias — but standard SQL doesn't require one. The bare table name is a valid qualifier (PostgreSQL, MySQL, DuckDB, ClickHouse, SQL Server). Removing the rule surfaces two latent issues that have to be fixed together:From.scala—Join.validate()Drop the two unconditional alias-required branches (the explicit alias match + the redundantcase j if alias.isEmptyarm in the second match). Keep the ON-clause guard for non-CROSS joins. The downstream sites that key off the join alias already fall back to the source name when no alias is set (From.tableAliases,From.joinAliases,From.unnestAliases,JoinKey.apply,Unnest.innerHitsName), so no call site needs to change.From.scala—StandardJoin.update()Stop callingsource.update(request). The source of a JOIN is a TABLE name, not a column expression. With the alias requirement gone,From.tableAliasesnow contains acustomers → customersself-mapping for the alias-less join — which madeGenericIdentifier.updatetreatname="customers"asalias.column, takeparts.tail.mkString(".")of a single-part name, and end up withname="". The ON clause still gets.update(request)so column resolution within ON works.Parser.scala—reservedKeywordsAddinner,left,right,full,cross,outer,on. The alias regex's negative-lookahead filter excludedfrom/join/etc. but not the join-type keywords — so without an alias onorders, the regex greedily consumedLEFTas the alias, leaving thejoinrule to parse a junk JOIN with no joinType. Functions likeLEFT(x, 5)andRIGHT(x, 3)are parsed by their dedicated function parsers (not viaidentifierRegex/regexAlias), so they continue to work — verified by ParserSpec line 225's SELECT that exercises both.ParserSpec gets three regression cases:
LEFT JOIN customers ON orders.customer_id = customers.idand exposesjoin.alias = Noneandjoin.source.name = "customers".From.tableAliasesby its bare name ("customers" -> "customers").