Skip to content

fix: SELECT DISTINCT parsing in Postgres dialect#2304

Merged
benfdking merged 3 commits intomainfrom
claude/fix-account-year-join-Jf6I4
Feb 13, 2026
Merged

fix: SELECT DISTINCT parsing in Postgres dialect#2304
benfdking merged 3 commits intomainfrom
claude/fix-account-year-join-Jf6I4

Conversation

@benfdking
Copy link
Copy Markdown
Collaborator

Summary

Fixed parsing of the DISTINCT keyword in SELECT statements to be recognized as a select clause modifier rather than a function name, aligning with SQL standard syntax.

Key Changes

  • Made the select clause modifier group optional in the Postgres dialect definition to properly handle cases where modifiers like DISTINCT may or may not be present
  • Updated the Redshift dialect test fixture to reflect the correct parse tree structure where DISTINCT is now recognized as a select_clause_modifier keyword instead of being incorrectly parsed as a function_name_identifier

Implementation Details

  • The change in postgres.rs adds .config(|this| this.optional()) to the select clause modifier matcher, allowing the parser to correctly handle SELECT statements with or without modifiers
  • The fixture update shows the corrected AST structure where DISTINCT appears as a sibling keyword modifier to the select clause, not as part of a function expression

https://claude.ai/code/session_01GohGSsbQjCDEjukcAbk6Ux

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark for 572a0c7

Click to view benchmark
Test Base PR %
DepthMap::from_parent 53.3±1.05µs 52.2±0.59µs -2.06%
fix_complex_query 12.3±0.06ms 12.4±0.10ms +0.81%
fix_superlong 161.7±5.36ms 163.3±11.02ms +0.99%
parse_complex_query 4.2±0.05µs 4.1±0.06µs -2.38%
parse_expression_recursion 7.2±0.06µs 7.2±0.08µs 0.00%
parse_simple_query 1056.6±10.65ns 1060.6±13.76ns +0.38%

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark for 450266a

Click to view benchmark
Test Base PR %
DepthMap::from_parent 52.8±0.63µs 52.5±0.58µs -0.57%
fix_complex_query 12.8±0.07ms 12.8±0.16ms 0.00%
fix_superlong 176.7±11.27ms 178.5±11.86ms +1.02%
parse_complex_query 4.3±0.08µs 4.1±0.07µs -4.65%
parse_expression_recursion 7.3±0.09µs 7.1±0.08µs -2.74%
parse_simple_query 1126.3±15.33ns 1054.2±19.62ns -6.40%

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark for 41256b6

Click to view benchmark
Test Base PR %
DepthMap::from_parent 54.2±0.63µs 54.7±0.78µs +0.92%
fix_complex_query 13.8±0.36ms 13.8±0.30ms 0.00%
fix_superlong 184.0±9.20ms 226.4±8.55ms +23.04%
parse_complex_query 4.3±0.10µs 4.3±0.08µs 0.00%
parse_expression_recursion 7.6±0.11µs 7.7±0.07µs +1.32%
parse_simple_query 1065.3±16.35ns 1128.9±55.98ns +5.97%

@benfdking benfdking changed the title Fix SELECT DISTINCT parsing in Postgres dialect fix: SELECT DISTINCT parsing in Postgres dialect Feb 13, 2026
…lects

The Postgres SelectClauseModifierSegment grammar required DISTINCT to be
followed by ON (...), making plain SELECT DISTINCT unparsable. This
affected all dialects inheriting from Postgres (DuckDB, Redshift). Mark
the ON (...) sequence as optional so both DISTINCT and DISTINCT ON (...)
are valid.

https://claude.ai/code/session_01GohGSsbQjCDEjukcAbk6Ux
…RACT

Adds test cases covering:
- SELECT DISTINCT in a subquery with LEFT JOIN and BETWEEN
- SELECT DISTINCT with EXTRACT, CTEs, CROSS JOIN, and UNION ALL

These reproduce the parsing errors that were fixed by making the ON (...)
sequence optional in the Postgres SelectClauseModifierSegment.

https://claude.ai/code/session_01GohGSsbQjCDEjukcAbk6Ux
Move the new test fixtures from sqlfluff/ to sqruff/ subfolder, matching
the convention used by other dialects (clickhouse, postgres, sqlite) for
sqruff-originated test cases.

https://claude.ai/code/session_01GohGSsbQjCDEjukcAbk6Ux
@benfdking benfdking force-pushed the claude/fix-account-year-join-Jf6I4 branch from 2ee8e36 to 1a9fdb5 Compare February 13, 2026 20:59
@benfdking benfdking merged commit 400a5a6 into main Feb 13, 2026
@benfdking benfdking deleted the claude/fix-account-year-join-Jf6I4 branch February 13, 2026 20:59
@github-actions
Copy link
Copy Markdown
Contributor

Benchmark for 2138d4e

Click to view benchmark
Test Base PR %
DepthMap::from_parent 52.2±0.93µs 52.7±0.64µs +0.96%
fix_complex_query 12.7±0.19ms 13.0±0.25ms +2.36%
fix_superlong 181.6±5.62ms 183.7±4.26ms +1.16%
parse_complex_query 4.2±0.07µs 4.2±0.06µs 0.00%
parse_expression_recursion 7.3±0.13µs 7.2±0.07µs -1.37%
parse_simple_query 1063.6±19.46ns 1056.7±19.28ns -0.65%

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.

2 participants