Merge pull request #196 from barweiss/fix-presto-limit-and-offset-order

Fix #195 the order of LIMIT and OFFSET clauses when using the Presto flavor
This commit is contained in:
Huan Du
2025-03-20 18:28:01 +08:00
committed by GitHub
2 changed files with 17 additions and 3 deletions
+15 -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,20 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
buf.WriteLeadingString("OFFSET ")
buf.WriteString(sb.offsetVar)
}
case Presto:
// There might be a hidden constraint in Presto requiring offset to be set before limit.
// The select statement documentation (https://prestodb.io/docs/current/sql/select.html)
// puts offset before limit, and Trino, which is based on Presto, seems
// to require this specific order.
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