/
limit.go
148 lines (127 loc) 路 3.33 KB
/
limit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package physicalplan
import (
"context"
"fmt"
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
"github.com/apache/arrow/go/v16/arrow/compute"
"github.com/apache/arrow/go/v16/arrow/memory"
"github.com/apache/arrow/go/v16/arrow/scalar"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"github.com/polarsignals/frostdb/pqarrow/arrowutils"
"github.com/polarsignals/frostdb/query/logicalplan"
)
type Limiter struct {
pool memory.Allocator
tracer trace.Tracer
next PhysicalPlan
count uint64
}
func Limit(pool memory.Allocator, tracer trace.Tracer, expr logicalplan.Expr) (*Limiter, error) {
literal, ok := expr.(*logicalplan.LiteralExpr)
if !ok {
return nil, fmt.Errorf("expected literal expression, got %T", expr)
}
var count uint64
switch v := literal.Value.(type) {
case *scalar.Uint64:
count = v.Value
case *scalar.Int64:
count = uint64(v.Value)
default:
return nil, fmt.Errorf("expected limit count type, got %T", v)
}
return &Limiter{
pool: pool,
tracer: tracer,
count: count,
}, nil
}
func (l *Limiter) SetNext(next PhysicalPlan) { l.next = next }
func (l *Limiter) Finish(ctx context.Context) error { return l.next.Finish(ctx) }
func (l *Limiter) Close() { l.next.Close() }
func (l *Limiter) Draw() *Diagram {
var child *Diagram
if l.next != nil {
child = l.next.Draw()
}
details := fmt.Sprintf("Limit(%d)", l.count)
return &Diagram{Details: details, Child: child}
}
func (l *Limiter) Callback(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
return l.next.Callback(ctx, r)
}
if l.count == 0 {
newRecord := array.NewRecord(r.Schema(), nil, 0)
return l.next.Callback(ctx, newRecord)
}
if uint64(r.NumRows()) <= l.count {
return l.next.Callback(ctx, r)
}
// TODO: We should figure out a way to avoid copying the record here.
// Maybe we can use a different approach to limit the record.
indicesBuilder := array.NewInt32Builder(l.pool)
defer indicesBuilder.Release()
for i := int32(0); i < int32(l.count); i++ {
indicesBuilder.Append(i)
}
indices := indicesBuilder.NewInt32Array()
defer indices.Release()
// compute.Take doesn't support dictionaries. Use take on r when r does not have
// dictionary column.
var hasDictionary bool
for i := 0; i < int(r.NumCols()); i++ {
if r.Column(i).DataType().ID() == arrow.DICTIONARY {
hasDictionary = true
break
}
}
if !hasDictionary {
res, err := compute.Take(
ctx,
compute.TakeOptions{BoundsCheck: true},
compute.NewDatumWithoutOwning(r),
compute.NewDatumWithoutOwning(indices),
)
if err != nil {
return err
}
r.Release()
return l.next.Callback(ctx, res.(*compute.RecordDatum).Value)
}
resArr := make([]arrow.Array, r.NumCols())
defer func() {
for _, a := range resArr {
if a != nil {
a.Release()
}
}
}()
var g errgroup.Group
for i := 0; i < int(r.NumCols()); i++ {
i := i
col := r.Column(i)
if d, ok := col.(*array.Dictionary); ok {
g.Go(func() error {
return arrowutils.TakeDictColumn(ctx, d, i, resArr, indices)
})
} else {
g.Go(func() error {
return arrowutils.TakeColumn(ctx, col, i, resArr, indices)
})
}
}
err := g.Wait()
if err != nil {
return err
}
if err := l.next.Callback(
ctx,
array.NewRecord(r.Schema(), resArr, int64(indices.Len())),
); err != nil {
return err
}
return nil
}