mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
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:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user