@@ -107,7 +107,7 @@ func (b *Builder) buildSelect(inScope *scope, s *ast.Select) (outScope *scope) {
107107 b .buildProjection (outScope , projScope )
108108 outScope = projScope
109109
110- b .buildDistinct (outScope , s .QueryOpts . Distinct )
110+ b .buildDistinct (outScope , s .QueryOpts )
111111
112112 // OFFSET and LIMIT are last
113113 offset := b .buildOffset (outScope , s .Limit )
@@ -117,7 +117,9 @@ func (b *Builder) buildSelect(inScope *scope, s *ast.Select) (outScope *scope) {
117117 limit := b .buildLimit (outScope , s .Limit )
118118 if limit != nil {
119119 l := plan .NewLimit (limit , outScope .node )
120- l .CalcFoundRows = s .QueryOpts .SQLCalcFoundRows
120+ if s .QueryOpts != nil {
121+ l .CalcFoundRows = s .QueryOpts .SQLCalcFoundRows
122+ }
121123 outScope .node = l
122124 }
123125
@@ -194,8 +196,8 @@ func (b *Builder) typeCoerceLiteral(e sql.Expression) sql.Expression {
194196
195197// buildDistinct creates a new plan.Distinct node if the query has a DISTINCT option.
196198// If the query has both DISTINCT and ALL, an error is returned.
197- func (b * Builder ) buildDistinct (inScope * scope , distinct bool ) {
198- if distinct {
199+ func (b * Builder ) buildDistinct (inScope * scope , qo * ast. QueryOpts ) {
200+ if qo != nil && qo . Distinct {
199201 inScope .node = plan .NewDistinct (inScope .node )
200202 }
201203}
0 commit comments