Skip to content

Commit a7c1eeb

Browse files
author
Bar Weiss
committed
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 ```
1 parent 87b9c12 commit a7c1eeb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

select.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
457457
buf.WriteLeadingString("LIMIT ")
458458
buf.WriteString(sb.limitVar)
459459
}
460-
case PostgreSQL, Presto:
460+
case PostgreSQL:
461461
if len(sb.limitVar) > 0 {
462462
buf.WriteLeadingString("LIMIT ")
463463
buf.WriteString(sb.limitVar)
@@ -467,6 +467,16 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
467467
buf.WriteLeadingString("OFFSET ")
468468
buf.WriteString(sb.offsetVar)
469469
}
470+
case Presto:
471+
if len(sb.offsetVar) > 0 {
472+
buf.WriteLeadingString("OFFSET ")
473+
buf.WriteString(sb.offsetVar)
474+
}
475+
476+
if len(sb.limitVar) > 0 {
477+
buf.WriteLeadingString("LIMIT ")
478+
buf.WriteString(sb.limitVar)
479+
}
470480

471481
case SQLServer:
472482
// If ORDER BY is not set, sort column #1 by default.

select_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ func ExampleSelectBuilder_limit_offset() {
229229
// Presto
230230
// #1: SELECT * FROM user
231231
// #2: SELECT * FROM user OFFSET ?
232-
// #3: SELECT * FROM user LIMIT ? OFFSET ?
232+
// #3: SELECT * FROM user OFFSET ? LIMIT ?
233233
// #4: SELECT * FROM user LIMIT ?
234-
// #5: SELECT * FROM user ORDER BY id LIMIT ? OFFSET ?
234+
// #5: SELECT * FROM user ORDER BY id OFFSET ? LIMIT ?
235235
//
236236
// Oracle
237237
// #1: SELECT * FROM user

0 commit comments

Comments
 (0)