-
Notifications
You must be signed in to change notification settings - Fork 100
/
arrow.go
161 lines (142 loc) · 3.73 KB
/
arrow.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
149
150
151
152
153
154
155
156
157
158
159
160
161
package bigquery
import (
"bytes"
"context"
"errors"
"sync/atomic"
"time"
"cloud.google.com/go/bigquery"
"github.com/apache/arrow/go/v14/arrow"
"github.com/apache/arrow/go/v14/arrow/array"
"github.com/apache/arrow/go/v14/arrow/ipc"
"github.com/apache/arrow/go/v14/arrow/memory"
"github.com/rilldata/rill/runtime/pkg/observability"
"go.uber.org/zap"
"google.golang.org/api/iterator"
)
func (f *fileIterator) AsArrowRecordReader() (array.RecordReader, error) {
arrowIt, err := f.bqIter.ArrowIterator()
if err != nil {
return nil, err
}
allocator := memory.DefaultAllocator
buf := bytes.NewBuffer(arrowIt.SerializedArrowSchema())
rdr, err := ipc.NewReader(buf, ipc.WithAllocator(allocator))
if err != nil {
return nil, err
}
defer rdr.Release()
rec := &arrowRecordReader{
bqIter: arrowIt,
arrowSchema: rdr.Schema(),
refCount: atomic.Int64{},
allocator: allocator,
logger: f.logger,
records: make([]arrow.Record, 0),
ctx: f.ctx,
buf: &bytes.Buffer{},
}
rec.refCount.Store(1)
return rec, nil
}
// some impl details are copied from array.simpleRecords
type arrowRecordReader struct {
bqIter bigquery.ArrowIterator
records []arrow.Record
cur arrow.Record
arrowSchema *arrow.Schema
refCount atomic.Int64
err error
logger *zap.Logger
allocator memory.Allocator
apinext time.Duration
ipcread time.Duration
ctx context.Context
buf *bytes.Buffer
}
// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (rs *arrowRecordReader) Retain() {
rs.refCount.Add(1)
}
// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func (rs *arrowRecordReader) Release() {
if rs.refCount.Load() <= 0 {
rs.logger.Warn("too many releases")
return
}
if rs.refCount.Add(-1) == 0 {
if rs.cur != nil {
rs.cur.Release()
}
for _, rec := range rs.records {
rec.Release()
}
rs.records = nil
}
rs.logger.Debug("next api call took", zap.Float64("seconds", rs.apinext.Seconds()), observability.ZapCtx(rs.ctx))
rs.logger.Debug("next ipc read took", zap.Float64("seconds", rs.ipcread.Seconds()), observability.ZapCtx(rs.ctx))
}
// Schema returns the underlying arrow schema
func (rs *arrowRecordReader) Schema() *arrow.Schema {
return rs.arrowSchema
}
// Record returns the current record. Call Next before consuming another record.
func (rs *arrowRecordReader) Record() arrow.Record {
return rs.cur
}
// Next returns true if another record can be produced
func (rs *arrowRecordReader) Next() bool {
if rs.err != nil {
return false
}
if len(rs.records) == 0 {
tz := time.Now()
next, err := rs.bqIter.Next()
if err != nil {
rs.err = err
return false
}
rs.apinext += time.Since(tz)
rs.records, rs.err = rs.nextArrowRecords(next)
if rs.err != nil {
return false
}
}
if rs.cur != nil {
rs.cur.Release()
rs.cur = nil
}
rs.cur = rs.records[0]
rs.records = rs.records[1:]
return true
}
func (rs *arrowRecordReader) Err() error {
if errors.Is(rs.err, iterator.Done) {
return nil
}
return rs.err
}
func (rs *arrowRecordReader) nextArrowRecords(r *bigquery.ArrowRecordBatch) ([]arrow.Record, error) {
t := time.Now()
defer func() {
rs.ipcread += time.Since(t)
}()
rs.buf.Reset()
rs.buf.Write(rs.bqIter.SerializedArrowSchema())
rs.buf.Write(r.Data)
rdr, err := ipc.NewReader(rs.buf, ipc.WithSchema(rs.arrowSchema), ipc.WithAllocator(rs.allocator))
if err != nil {
return nil, err
}
defer rdr.Release()
records := make([]arrow.Record, 0)
for rdr.Next() {
rec := rdr.Record()
rec.Retain()
records = append(records, rec)
}
return records, rdr.Err()
}