Browse Source

limit with offset

pull/7185/head
chrislu 1 month ago
parent
commit
02d36637de
  1. 31
      weed/query/engine/aggregations.go
  2. 6
      weed/query/engine/engine.go
  3. 11
      weed/query/engine/hybrid_message_scanner.go

31
weed/query/engine/aggregations.go

@ -350,7 +350,8 @@ func (e *SQLEngine) executeAggregationQuery(ctx context.Context, hybridScanner *
// executeAggregationQueryWithPlan handles SELECT queries with aggregation functions and populates execution plan // 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) { 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) // 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 offset := 0
if stmt.Limit != nil && stmt.Limit.Rowcount != nil { if stmt.Limit != nil && stmt.Limit.Rowcount != nil {
if limitExpr, ok := stmt.Limit.Rowcount.(*SQLVal); ok && limitExpr.Type == IntVal { 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 // 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 rows := fastResult.Rows
// Handle LIMIT 0 first
if limit == 0 {
rows = [][]sqltypes.Value{}
} else {
// Apply OFFSET first // Apply OFFSET first
if offset > 0 { if offset > 0 {
if offset >= len(rows) { if offset >= len(rows) {
@ -403,11 +409,8 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS
rows = rows[offset:] 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] rows = rows[:limit]
} }
} }
@ -484,8 +487,13 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS
} }
// Apply OFFSET and LIMIT to aggregation results // 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} 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 // Apply OFFSET first
if offset > 0 { if offset > 0 {
if offset >= len(rows) { 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] rows = rows[:limit]
} }
} }

6
weed/query/engine/engine.go

@ -1416,7 +1416,8 @@ func (e *SQLEngine) executeSelectStatement(ctx context.Context, stmt *SelectStat
} }
// Parse LIMIT and OFFSET clauses // Parse LIMIT and OFFSET clauses
limit := 0
// Use -1 to distinguish "no LIMIT" from "LIMIT 0"
limit := -1
offset := 0 offset := 0
if stmt.Limit != nil && stmt.Limit.Rowcount != nil { if stmt.Limit != nil && stmt.Limit.Rowcount != nil {
switch limitExpr := stmt.Limit.Rowcount.(type) { switch limitExpr := stmt.Limit.Rowcount.(type) {
@ -1605,7 +1606,8 @@ func (e *SQLEngine) executeSelectStatementWithBrokerStats(ctx context.Context, s
} }
// Parse LIMIT and OFFSET clauses // Parse LIMIT and OFFSET clauses
limit := 0
// Use -1 to distinguish "no LIMIT" from "LIMIT 0"
limit := -1
offset := 0 offset := 0
if stmt.Limit != nil && stmt.Limit.Rowcount != nil { if stmt.Limit != nil && stmt.Limit.Rowcount != nil {
switch limitExpr := stmt.Limit.Rowcount.(type) { switch limitExpr := stmt.Limit.Rowcount.(type) {

11
weed/query/engine/hybrid_message_scanner.go

@ -233,11 +233,13 @@ func (hms *HybridMessageScanner) ScanWithStats(ctx context.Context, options Hybr
} }
// Apply final OFFSET and LIMIT processing (done once at the end) // Apply final OFFSET and LIMIT processing (done once at the end)
// Limit semantics: -1 = no limit, 0 = LIMIT 0 (empty), >0 = limit to N rows
if options.Offset > 0 || options.Limit >= 0 { if options.Offset > 0 || options.Limit >= 0 {
// Handle LIMIT 0 special case - return empty result immediately
// Handle LIMIT 0 special case first
if options.Limit == 0 { if options.Limit == 0 {
results = []HybridScanResult{}
} else {
return []HybridScanResult{}, stats, nil
}
// Apply OFFSET first // Apply OFFSET first
if options.Offset > 0 { if options.Offset > 0 {
if options.Offset >= len(results) { if options.Offset >= len(results) {
@ -247,12 +249,11 @@ func (hms *HybridMessageScanner) ScanWithStats(ctx context.Context, options Hybr
} }
} }
// Apply LIMIT after OFFSET
// Apply LIMIT after OFFSET (only if limit > 0)
if options.Limit > 0 && len(results) > options.Limit { if options.Limit > 0 && len(results) > options.Limit {
results = results[:options.Limit] results = results[:options.Limit]
} }
} }
}
return results, stats, nil return results, stats, nil
} }

Loading…
Cancel
Save