forked from polarsignals/frostdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct.go
170 lines (141 loc) 路 3.69 KB
/
distinct.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
162
163
164
165
166
167
168
169
170
package physicalplan
import (
"context"
"fmt"
"hash/maphash"
"strings"
"sync"
"github.com/apache/arrow/go/v15/arrow"
"github.com/apache/arrow/go/v15/arrow/array"
"github.com/apache/arrow/go/v15/arrow/memory"
"github.com/apache/arrow/go/v15/arrow/scalar"
"go.opentelemetry.io/otel/trace"
"github.com/dreamsxin/frostdb/dynparquet"
"github.com/dreamsxin/frostdb/pqarrow/builder"
"github.com/dreamsxin/frostdb/query/logicalplan"
)
type Distinction struct {
pool memory.Allocator
tracer trace.Tracer
next PhysicalPlan
columns []logicalplan.Expr
hashSeed maphash.Seed
mtx *sync.RWMutex
seen map[uint64]struct{}
}
func (d *Distinction) Draw() *Diagram {
var child *Diagram
if d.next != nil {
child = d.next.Draw()
}
var columns []string
for _, c := range d.columns {
columns = append(columns, c.Name())
}
return &Diagram{Details: fmt.Sprintf("Distinction (%s)", strings.Join(columns, ",")), Child: child}
}
func Distinct(pool memory.Allocator, tracer trace.Tracer, columns []logicalplan.Expr) *Distinction {
return &Distinction{
pool: pool,
tracer: tracer,
columns: columns,
hashSeed: maphash.MakeSeed(),
mtx: &sync.RWMutex{},
seen: make(map[uint64]struct{}),
}
}
func (d *Distinction) SetNext(plan PhysicalPlan) {
d.next = plan
}
func (d *Distinction) Finish(ctx context.Context) error {
return d.next.Finish(ctx)
}
func (d *Distinction) Close() {
d.next.Close()
}
func (d *Distinction) Callback(ctx context.Context, r arrow.Record) error {
// Generates high volume of spans. Comment out if needed during development.
// ctx, span := d.tracer.Start(ctx, "Distinction/Callback")
// defer span.End()
distinctFields := make([]arrow.Field, 0, 10)
distinctFieldHashes := make([]uint64, 0, 10)
distinctArrays := make([]arrow.Array, 0, 10)
for i := 0; i < r.Schema().NumFields(); i++ {
field := r.Schema().Field(i)
for _, col := range d.columns {
if col.MatchColumn(field.Name) {
distinctFields = append(distinctFields, field)
distinctFieldHashes = append(distinctFieldHashes, scalar.Hash(d.hashSeed, scalar.NewStringScalar(field.Name)))
distinctArrays = append(distinctArrays, r.Column(i))
}
}
}
resBuilders := make([]builder.ColumnBuilder, 0, len(distinctArrays))
defer func() {
for _, builder := range resBuilders {
builder.Release()
}
}()
for _, arr := range distinctArrays {
resBuilders = append(resBuilders, builder.NewBuilder(d.pool, arr.DataType()))
}
rows := int64(0)
numRows := int(r.NumRows())
colHashes := make([][]uint64, len(distinctFields))
for i, arr := range distinctArrays {
colHashes[i] = dynparquet.HashArray(arr)
}
for i := 0; i < numRows; i++ {
hash := uint64(0)
for j := range colHashes {
if colHashes[j][i] == 0 {
continue
}
hash = hashCombine(
hash,
hashCombine(
distinctFieldHashes[j],
colHashes[j][i],
),
)
}
d.mtx.RLock()
if _, ok := d.seen[hash]; ok {
d.mtx.RUnlock()
continue
}
d.mtx.RUnlock()
for j, arr := range distinctArrays {
err := builder.AppendValue(resBuilders[j], arr, i)
if err != nil {
return err
}
}
rows++
d.mtx.Lock()
d.seen[hash] = struct{}{}
d.mtx.Unlock()
}
if rows == 0 {
// No need to call anything further down the chain, no new values were
// seen so we can skip.
return nil
}
resArrays := make([]arrow.Array, 0, len(resBuilders))
defer func() {
for _, arr := range resArrays {
arr.Release()
}
}()
for _, builder := range resBuilders {
resArrays = append(resArrays, builder.NewArray())
}
schema := arrow.NewSchema(distinctFields, nil)
distinctRecord := array.NewRecord(
schema,
resArrays,
rows,
)
defer distinctRecord.Release()
return d.next.Callback(ctx, distinctRecord)
}