Skip to content

fix(RF01): support parenthesized joined tables in FROM clause#2245

Open
benfdking wants to merge 1 commit intomainfrom
fix/rf01-parenthesized-joins
Open

fix(RF01): support parenthesized joined tables in FROM clause#2245
benfdking wants to merge 1 commit intomainfrom
fix/rf01-parenthesized-joins

Conversation

@benfdking
Copy link
Collaborator

Summary

Fixes #1573

This PR adds support for parsing and analyzing parenthesized joined tables in FROM clauses, such as:

SELECT c.id, ci.id
FROM users AS u
CROSS JOIN (
    course_instances AS ci
    JOIN pl_courses AS c ON c.id = ci.course_id
)
LEFT JOIN enrollments AS e ON e.user_id = u.user_id

Previously, sqruff would fail to parse the parenthesized join expression and the RF01 rule would incorrectly report that table aliases like c and ci were not found in the FROM clause.

Changes

  • Add JoinTargetGrammar in ANSI dialect that allows either:
    • FromExpressionElementSegment (simple table)
    • Bracketed(FromExpressionSegment) (parenthesized joined tables)
  • Update JoinClauseSegment to use JoinTargetGrammar instead of FromExpressionElementSegment
  • Update JoinClauseSegment::eventual_aliases() to extract table aliases from bracketed FromExpression segments
  • Add test case for the reported issue

Test plan

  • Verified the SQL from the issue now parses correctly
  • Verified RF01 no longer reports false positives for table aliases inside parenthesized joins
  • Ran cargo clippy and cargo fmt
  • Existing dialect tests pass

🤖 Generated with Claude Code

@github-actions
Copy link

Benchmark for cdbba8b

Click to view benchmark
Test Base PR %
DepthMap::from_parent 52.7±0.68µs 52.2±1.45µs -0.95%
fix_complex_query 12.4±0.15ms 12.3±0.12ms -0.81%
fix_superlong 189.8±7.53ms 186.4±10.64ms -1.79%
parse_complex_query 4.1±0.03µs 4.2±0.05µs +2.44%
parse_expression_recursion 7.1±0.09µs 7.2±0.12µs +1.41%
parse_simple_query 1043.1±10.29ns 1052.4±20.72ns +0.89%

Add support for parsing and analyzing parenthesized joined tables like:
`FROM a CROSS JOIN (b JOIN c ON b.id = c.b_id)`

Changes:
- Add JoinTargetGrammar in ANSI dialect that allows either a simple
  table expression or a bracketed FROM expression as join targets
- Update JoinClauseSegment to use JoinTargetGrammar
- Update JoinClauseSegment::eventual_aliases() to extract table aliases
  from bracketed FromExpression segments

Fixes #1573

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@benfdking benfdking force-pushed the fix/rf01-parenthesized-joins branch from 3273ebd to d6118f7 Compare February 3, 2026 21:54
@github-actions
Copy link

github-actions bot commented Feb 3, 2026

Benchmark for b62b564

Click to view benchmark
Test Base PR %
DepthMap::from_parent 52.1±0.51µs 51.8±0.66µs -0.58%
fix_complex_query 12.3±0.46ms 12.3±0.10ms 0.00%
fix_superlong 156.9±4.22ms 164.4±11.59ms +4.78%
parse_complex_query 4.2±0.08µs 4.2±0.04µs 0.00%
parse_expression_recursion 7.2±0.10µs 7.1±0.10µs -1.39%
parse_simple_query 1043.2±16.53ns 1073.3±11.70ns +2.89%

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.

[Bug]: Nested references in select

1 participant