Fix dropped SELECT clauses and lost FROM-list comma#40
Conversation
The SELECT formatter silently discarded several clauses and dropped a comma between mixed FROM items. All four are addressed in src/formatter/select.rs (with a small reusable helper extracted in src/formatter/expr.rs). - #18: FOR UPDATE / FOR SHARE locking clauses were dropped. Added a `for_locking` field to SelectClauses, collect `for_locking_clause`, and render each locking item (including OF list, NOWAIT, SKIP LOCKED, NO KEY UPDATE, KEY SHARE) in both river and left-aligned renderers. - #19: WINDOW clause was dropped, leaving dangling OVER references. Added a `window_clause` field, collect it, and render the named window definitions. Extracted `format_window_spec_body` from `format_over_clause` so the spec body is formatted consistently. - #20: FETCH FIRST n ROWS ONLY was dropped (extract_limit_value could not read select_fetch_first_value). The FETCH FIRST form is now preserved verbatim rather than normalized to LIMIT, since that is what the user wrote and there is no existing LIMIT normalization. - #21: Comma was dropped between a plain table and a JOIN group in mixed FROM lists (both orders). `has_multiple_non_join` only counted non-join refs; replaced with logic that appends a comma after every FROM item except the last, in both river and left-aligned styles. Added regression fixtures: river/{for_locking_clause, window_named_clause, fetch_first_rows_only, from_comma_join_mix} and aweber/from_comma_join_mix. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR preserves ChangesMissing SELECT clause preservation fixes
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/fixtures/river/window_named_clause.expected`:
- Around line 1-4: The expected SQL output is rewriting a bare named-window
reference into an inline window specification; update the formatter/fixture for
the ROW_NUMBER() OVER form so the named window reference stays as OVER w instead
of being rendered as OVER (w). Focus on the window-clause handling in the SQL
formatting path that produces this expected file and preserve the bare
named-window syntax when a WINDOW alias like w is referenced.
In `@tests/fixtures/river/window_named_clause.sql`:
- Line 1: The window formatting in format_over_clause is rewriting named window
references like OVER w into OVER (...), which changes the emitted SQL form.
Update the formatter to preserve a bare named window reference when the OVER
clause points directly to a named window, and only parenthesize inline
specifications; use the existing format_over_clause logic to distinguish these
cases so this fixture stays as OVER w, or add a separate frame-clause case if
needed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: a5d42cda-c290-4a94-857f-bffe9c896966
📒 Files selected for processing (12)
src/formatter/expr.rssrc/formatter/select.rstests/fixtures/aweber/from_comma_join_mix.expectedtests/fixtures/aweber/from_comma_join_mix.sqltests/fixtures/river/fetch_first_rows_only.expectedtests/fixtures/river/fetch_first_rows_only.sqltests/fixtures/river/for_locking_clause.expectedtests/fixtures/river/for_locking_clause.sqltests/fixtures/river/from_comma_join_mix.expectedtests/fixtures/river/from_comma_join_mix.sqltests/fixtures/river/window_named_clause.expectedtests/fixtures/river/window_named_clause.sql
format_over_clause unconditionally wrapped the window reference in parentheses, rewriting `OVER w` into `OVER (w)`. In PostgreSQL these are distinct forms: `OVER w` references a named window from the WINDOW clause, while `OVER (w ...)` copies and extends that definition and is rejected when the named window already carries a frame clause. Only parenthesize when the over_clause has an inline window_specification; render a bare named-window reference (a ColId child) as `OVER name`. Updates the river/window_named_clause fixture to expect the bare form. Addresses CodeRabbit review feedback on PR #40. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* Fix inline comments mangling CREATE FOREIGN TABLE formatting (#41) CREATE FOREIGN TABLE mishandled inline `-- ...` comments the same way CREATE TABLE did before #40: comments between columns were treated as bogus columns (alignment padding plus a trailing comma that mangled the comment text), and a comment after the last element was dropped. Factor the comment-association logic added for CREATE TABLE into a shared `collect_table_elements` helper and use it from both statement formatters. `format_create_foreign_table_stmt` now renders trailing comments at the end of the element they follow (aligned to a common column in river style, single-space in left-aligned styles), keeping the original element order. Also registers the previously unwired CREATE FOREIGN TABLE fixtures in the test harness alongside the new inline-comment fixtures. Closes #41 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Extract shared left-aligned table-element renderer The non-river rendering loop in format_create_foreign_table_stmt was byte-identical to the one in format_create_table_stmt. Extract it into a render_grouped_elements_left_aligned helper that both callers use, mirroring the earlier collect_table_elements extraction. Addresses CodeRabbit nitpick on PR #43. Behavior is unchanged; all fixtures and unit tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
Fixes four bugs in the SELECT formatter (
src/formatter/select.rs) where whole clauses were silently dropped or a comma was lost, producing invalid SQL. A small reusable helper was extracted insrc/formatter/expr.rs.Problem
FOR UPDATE/FOR SHARElocking clauses were dropped entirely.SELECT id FROM accounts WHERE active FOR UPDATE;lostFOR UPDATE.WINDOWclause was dropped, leaving danglingOVER wreferences with no window definition (invalid output).FETCH FIRST n ROWS ONLYform was dropped:extract_limit_valuecould not readselect_fetch_first_value, so no limit was emitted.FROM t1, t2 JOIN t3 ...rendered ast1 t2(parsed ast1 AS t2). Affected both river and left-aligned (aweber) styles.Solution
for_lockingfield toSelectClauses, collectfor_locking_clause, and render each locking item (OF list, NOWAIT, SKIP LOCKED, NO KEY UPDATE, KEY SHARE) in both renderers.window_clausefield, collect and render the named window definitions. Extractedformat_window_spec_bodyfromformat_over_clause(identical output) so the spec body is formatted consistently.FETCH FIRSTform is preserved verbatim (e.g.FETCH FIRST 5 ROWS ONLY) rather than normalized toLIMIT. Rationale: it is what the user wrote, and the existing formatter has no LIMIT/OFFSET normalization, so preserving is the consistent choice.FETCH(5 chars) aligns identically toLIMITin the river.has_multiple_non_joinheuristic with logic that appends a comma after every FROM item except the last, in both river and left-aligned styles.Tests
Added regression fixtures (auto-discovered by
tests/all_fixtures.rs):river/for_locking_clause,river/window_named_clause,river/fetch_first_rows_only,river/from_comma_join_mixaweber/from_comma_join_mix(Comma dropped between plain table and JOIN group in mixed FROM lists, changing semantics #21 affects both styles)All 72 fixtures pass;
just check(fmt + clippy -D warnings + test) is green.Fixes #18
Fixes #19
Fixes #20
Fixes #21
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
OVERwith inline window specifications and named window references.WINDOWdefinitions and row-level locking clauses (FOR UPDATE/FOR SHARE), plus correctFETCH FIRST ... ROWS ONLYoutput.FROMclauses when mixing comma-separated items with explicitJOIN ... ON.Tests
FETCH FIRST, and comma/join combinations.