Skip to content

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

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

icecrasher321 wants to merge 4 commits 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.

AI-assisted.

@icecrasher321

Copy link
Copy Markdown
Author

@LucaCappelletti94 let me know if this looks good to you

@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.74359% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.68%. Comparing base (540e398) to head (b026ef6).

Files with missing lines Patch % Lines
src/parser/mod.rs 89.74% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3049      +/-   ##
==========================================
+ Coverage   83.64%   83.68%   +0.03%     
==========================================
  Files          42       42              
  Lines       35833    35860      +27     
  Branches    35833    35860      +27     
==========================================
+ Hits        29974    30009      +35     
+ Misses       2418     2414       -4     
+ Partials     3441     3437       -4     

☔ 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

Comment thread tests/sqlparser_postgres.rs Outdated
] {
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

Comment thread tests/sqlparser_postgres.rs Outdated
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

Comment thread tests/sqlparser_postgres.rs Outdated
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

icecrasher321 and others added 2 commits November 30, 2026 08:09
…ch-optional

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	tests/sqlparser_postgres.rs
Co-authored-by: Cursor <cursoragent@cursor.com>
@icecrasher321

Copy link
Copy Markdown
Author

@LucaCappelletti94 made the changes, thank you

@icecrasher321

Copy link
Copy Markdown
Author

@LucaCappelletti94 let me know if you need any more changes or this looks good!

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