|
|
@ -350,7 +350,8 @@ func (e *SQLEngine) executeAggregationQuery(ctx context.Context, hybridScanner * |
|
|
|
// executeAggregationQueryWithPlan handles SELECT queries with aggregation functions and populates execution plan
|
|
|
|
func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridScanner *HybridMessageScanner, aggregations []AggregationSpec, stmt *SelectStatement, plan *QueryExecutionPlan) (*QueryResult, error) { |
|
|
|
// Parse LIMIT and OFFSET for aggregation results (do this first)
|
|
|
|
limit := 0 |
|
|
|
// Use -1 to distinguish "no LIMIT" from "LIMIT 0"
|
|
|
|
limit := -1 |
|
|
|
offset := 0 |
|
|
|
if stmt.Limit != nil && stmt.Limit.Rowcount != nil { |
|
|
|
if limitExpr, ok := stmt.Limit.Rowcount.(*SQLVal); ok && limitExpr.Type == IntVal { |
|
|
@ -393,8 +394,13 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS |
|
|
|
} |
|
|
|
|
|
|
|
// Apply OFFSET and LIMIT to fast path results too
|
|
|
|
if offset > 0 || limit > 0 { |
|
|
|
// Limit semantics: -1 = no limit, 0 = LIMIT 0 (empty), >0 = limit to N rows
|
|
|
|
if offset > 0 || limit >= 0 { |
|
|
|
rows := fastResult.Rows |
|
|
|
// Handle LIMIT 0 first
|
|
|
|
if limit == 0 { |
|
|
|
rows = [][]sqltypes.Value{} |
|
|
|
} else { |
|
|
|
// Apply OFFSET first
|
|
|
|
if offset > 0 { |
|
|
|
if offset >= len(rows) { |
|
|
@ -403,11 +409,8 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS |
|
|
|
rows = rows[offset:] |
|
|
|
} |
|
|
|
} |
|
|
|
// Apply LIMIT after OFFSET
|
|
|
|
if limit >= 0 { // Handle LIMIT 0 case
|
|
|
|
if limit == 0 { |
|
|
|
rows = [][]sqltypes.Value{} |
|
|
|
} else if len(rows) > limit { |
|
|
|
// Apply LIMIT after OFFSET (only if limit > 0)
|
|
|
|
if limit > 0 && len(rows) > limit { |
|
|
|
rows = rows[:limit] |
|
|
|
} |
|
|
|
} |
|
|
@ -484,8 +487,13 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS |
|
|
|
} |
|
|
|
|
|
|
|
// Apply OFFSET and LIMIT to aggregation results
|
|
|
|
// Limit semantics: -1 = no limit, 0 = LIMIT 0 (empty), >0 = limit to N rows
|
|
|
|
rows := [][]sqltypes.Value{row} |
|
|
|
if offset > 0 || limit > 0 { |
|
|
|
if offset > 0 || limit >= 0 { |
|
|
|
// Handle LIMIT 0 first
|
|
|
|
if limit == 0 { |
|
|
|
rows = [][]sqltypes.Value{} |
|
|
|
} else { |
|
|
|
// Apply OFFSET first
|
|
|
|
if offset > 0 { |
|
|
|
if offset >= len(rows) { |
|
|
@ -495,11 +503,8 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Apply LIMIT after OFFSET
|
|
|
|
if limit >= 0 { // Handle LIMIT 0 case
|
|
|
|
if limit == 0 { |
|
|
|
rows = [][]sqltypes.Value{} |
|
|
|
} else if len(rows) > limit { |
|
|
|
// Apply LIMIT after OFFSET (only if limit > 0)
|
|
|
|
if limit > 0 && len(rows) > limit { |
|
|
|
rows = rows[:limit] |
|
|
|
} |
|
|
|
} |
|
|
|