@@ -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,20 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
467467 buf .WriteLeadingString ("OFFSET " )
468468 buf .WriteString (sb .offsetVar )
469469 }
470+ case Presto :
471+ // There might be a hidden constraint in Presto requiring offset to be set before limit.
472+ // The select statement documentation (https://prestodb.io/docs/current/sql/select.html)
473+ // puts offset before limit, and Trino, which is based on Presto, seems
474+ // to require this specific order.
475+ if len (sb .offsetVar ) > 0 {
476+ buf .WriteLeadingString ("OFFSET " )
477+ buf .WriteString (sb .offsetVar )
478+ }
479+
480+ if len (sb .limitVar ) > 0 {
481+ buf .WriteLeadingString ("LIMIT " )
482+ buf .WriteString (sb .limitVar )
483+ }
470484
471485 case SQLServer :
472486 // If ORDER BY is not set, sort column #1 by default.
0 commit comments