Skip to content

PostgreSQL: Support optional FROM/IN and direction in FETCH - #3049

Open
icecrasher321 wants to merge 1 commit into
apache:mainfrom
formalco:upstream/postgres-fetch-optional
Open

icecrasher321 wants to merge 1 commit into
apache:mainfrom
formalco:upstream/postgres-fetch-optional

Conversation

@icecrasher321

@icecrasher321 icecrasher321 commented Nov 24, 2026 •

Copy link
Copy Markdown

Dialects: PostgreSQL

Closes #3050

PostgreSQL's FETCH accepts an optional direction, optional FROM/IN, and signed counts, which currently fail to parse:

FETCH c;                -- Expected: a concrete value, found: c
FETCH 1 c;              -- Expected FROM or IN
FETCH FORWARD FROM c;   -- Expected: a concrete value, found: FROM
FETCH -1 FROM c;        -- Expected: a value, found: -

Makes FROM/IN optional, defaults an omitted direction to NEXT, makes the FORWARD/BACKWARD count optional, and parses signed counts (no AST changes). Tested all these forms of the query.

@icecrasher321

Copy link
Copy Markdown
Author

@LucaCappelletti94 let me know if this looks good to you

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.58%. Comparing base (ccacedc) to head (4060c11).

Files with missing lines Patch % Lines
src/parser/mod.rs 87.87% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3049      +/-   ##
==========================================
+ Coverage   83.55%   83.58%   +0.03%     
==========================================
  Files          42       42              
  Lines       35692    35714      +22     
  Branches    35692    35714      +22     
==========================================
+ Hits        29822    29852      +30     
+ Misses       2429     2426       -3     
+ Partials     3441     3436       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the seed fuzzers, then run the differential fuzzer with pg_query to determine whether you are covering the full set of this new syntax you are adding.

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request waiting on contributor The review needs further refinements by its author labels Nov 25, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
@icecrasher321
icecrasher321 force-pushed the upstream/postgres-fetch-optional branch from 4060c11 to 0afdfc9 Compare November 25, 2026 19:44
@icecrasher321

Copy link
Copy Markdown
Author

@LucaCappelletti94 added, thanks

] {
assert_eq!(
pg().parse_sql_statements(sql).unwrap_err().to_string(),
"sql parser error: Expected: identifier, found: EOF",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the typed error

pg().parse_sql_statements("FETCH ABSOLUTE c")
.unwrap_err()
.to_string(),
"sql parser error: Expected: a concrete value, found: c"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the error type

pg().parse_sql_statements("FETCH -$1 FROM c")
.unwrap_err()
.to_string(),
"sql parser error: Expected: literal number, found: $1"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the typed error, maybe a for loop for these few repeated patterns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PostgreSQL waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSqlDialect rejects FETCH statements with omitted direction, FROM/IN, or signed counts

3 participants