Jainbaba opened a new issue, #2194:
URL: https://github.com/apache/datafusion-sqlparser-rs/issues/2194
### Describe the bug
The PostgreSQL dialect parser fails to parse valid PostgreSQL syntax when
`LIMIT` appears after `FOR UPDATE SKIP LOCKED`, even though PostgreSQL accepts
both clause orderings.
### To Reproduce
```rust
use sqlparser::dialect::PostgreSqlDialect;
use sqlparser::parser::Parser;
fn main() {
let sql = r#"
SELECT "id", "status"
FROM "jobs"
WHERE "status" = 'pending'
ORDER BY "createdAt"
FOR UPDATE SKIP LOCKED
LIMIT 5
"#;
let dialect = PostgreSqlDialect {};
let result = Parser::parse_sql(&dialect, sql);
println!("{:?}", result);
}
```
**Error:**
```
sql parser error: Expected: end of statement, found: LIMIT at Line: X,
Column: Y
```
### Expected behavior
The parser should accept both clause orderings since PostgreSQL does:
- ✅ `ORDER BY ... LIMIT 5 FOR UPDATE SKIP LOCKED` (currently works)
- ❌ `ORDER BY ... FOR UPDATE SKIP LOCKED LIMIT 5` (should also work)
Both are semantically identical in PostgreSQL.
### Additional context
- PostgreSQL documentation specifies `FOR UPDATE` should come last, but
PostgreSQL actually accepts `LIMIT` after it
- This is commonly used in job queue patterns
- jOOQ had the same issue and fixed it in version 3.19.0 (see
[jOOQ/jOOQ#15826](https://github.com/jOOQ/jOOQ/issues/15826))
- Real-world example:
https://www.inferable.ai/blog/posts/postgres-skip-locked
- sqlparser version: 0.59
### Tested on PostgreSQL
```sql
-- Both work in PostgreSQL 16:
SELECT * FROM jobs ORDER BY id LIMIT 5 FOR UPDATE SKIP LOCKED;
SELECT * FROM jobs ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 5;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]