Fix the order of LIMIT and OFFSET clauses when using the Presto flavor (issue #195)

Queries that have both a limit and an offset build a query with a limit clause *before* the offset clause.

```go
fmt.Println(Select("*").From("table").Offset(5).Limit(10))
// SELECT * FROM table LIMIT 10 OFFSET 5
```

This seems to generate syntax errors in Trino. [Presto documentation specifies OFFSET before LIMIT](https://prestodb.io/docs/current/sql/select.html#limit-clause) in the SELECT statement documentation too, but is not very explicit about the reverse not being supported.

If the OFFSET and LIMIT clauses are swapped, the query succeeds.

Expected:

```go
fmt.Println(Select("*").From("table").Offset(5).Limit(10))
// SELECT * FROM table OFFSET 5 LIMIT 10
```
This commit is contained in:
Bar Weiss
2025-03-20 10:32:26 +02:00
parent 903c2500cd
commit feef77c099
2 changed files with 13 additions and 3 deletions
+11 -1
View File
@@ -457,7 +457,7 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
buf.WriteLeadingString("LIMIT ")
buf.WriteString(sb.limitVar)
}
case PostgreSQL, Presto:
case PostgreSQL:
if len(sb.limitVar) > 0 {
buf.WriteLeadingString("LIMIT ")
buf.WriteString(sb.limitVar)
@@ -467,6 +467,16 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
buf.WriteLeadingString("OFFSET ")
buf.WriteString(sb.offsetVar)
}
case Presto:
if len(sb.offsetVar) > 0 {
buf.WriteLeadingString("OFFSET ")
buf.WriteString(sb.offsetVar)
}
if len(sb.limitVar) > 0 {
buf.WriteLeadingString("LIMIT ")
buf.WriteString(sb.limitVar)
}
case SQLServer:
// If ORDER BY is not set, sort column #1 by default.
+2 -2
View File
@@ -229,9 +229,9 @@ func ExampleSelectBuilder_limit_offset() {
// Presto
// #1: SELECT * FROM user
// #2: SELECT * FROM user OFFSET ?
// #3: SELECT * FROM user LIMIT ? OFFSET ?
// #3: SELECT * FROM user OFFSET ? LIMIT ?
// #4: SELECT * FROM user LIMIT ?
// #5: SELECT * FROM user ORDER BY id LIMIT ? OFFSET ?
// #5: SELECT * FROM user ORDER BY id OFFSET ? LIMIT ?
//
// Oracle
// #1: SELECT * FROM user