forked from polarsignals/frostdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
249 lines (218 loc) 路 4.88 KB
/
builder.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package logicalplan
import (
"errors"
"fmt"
"github.com/apache/arrow/go/v15/arrow"
)
type Builder struct {
plan *LogicalPlan
err error
}
func (b Builder) Scan(
provider TableProvider,
tableName string,
) Builder {
return Builder{
err: b.err,
plan: &LogicalPlan{
TableScan: &TableScan{
TableProvider: provider,
TableName: tableName,
},
},
}
}
func (b Builder) ScanSchema(
provider TableProvider,
tableName string,
) Builder {
return Builder{
err: b.err,
plan: &LogicalPlan{
SchemaScan: &SchemaScan{
TableProvider: provider,
TableName: tableName,
},
},
}
}
func (b Builder) Project(
exprs ...Expr,
) Builder {
return Builder{
err: b.err,
plan: &LogicalPlan{
Input: b.plan,
Projection: &Projection{
Exprs: exprs,
},
},
}
}
type Visitor interface {
PreVisit(expr Expr) bool
Visit(expr Expr) bool
PostVisit(expr Expr) bool
}
type ExprTypeFinder interface {
DataTypeForExpr(expr Expr) (arrow.DataType, error)
}
type Expr interface {
DataType(ExprTypeFinder) (arrow.DataType, error)
Accept(Visitor) bool
Name() string
Equal(Expr) bool
fmt.Stringer
// ColumnsUsedExprs extracts all the expressions that are used that cause
// physical data to be read from a column.
ColumnsUsedExprs() []Expr
// MatchColumn returns whether it would operate on the passed column. In
// contrast to the ColumnUsedExprs function from the Expr interface, it is not
// useful to identify which columns are to be read physically. This is
// necessary to distinguish between projections.
//
// Take the example of a column that projects `XYZ > 0`. Matcher can be
// used to identify the column in the resulting Apache Arrow frames, while
// ColumnsUsed will return `XYZ` to be necessary to be loaded physically.
MatchColumn(columnName string) bool
// MatchPath returns whether it would operate on the passed path. This is nessesary for nested schemas.
MatchPath(path string) bool
// Computed returns whether the expression is computed as opposed to being
// a static value or unmodified physical column.
Computed() bool
// Clone returns a deep copy of the expression.
Clone() Expr
}
func (b Builder) Filter(expr Expr) Builder {
if expr == nil {
return b
}
return Builder{
err: b.err,
plan: &LogicalPlan{
Input: b.plan,
Filter: &Filter{
Expr: expr,
},
},
}
}
func (b Builder) Distinct(
exprs ...Expr,
) Builder {
return Builder{
err: b.err,
plan: &LogicalPlan{
Distinct: &Distinct{
Exprs: exprs,
},
Input: &LogicalPlan{
Projection: &Projection{
Exprs: exprs,
},
Input: b.plan,
},
},
}
}
func (b Builder) Limit(expr Expr) Builder {
if expr == nil {
return b
}
return Builder{
err: b.err,
plan: &LogicalPlan{
Input: b.plan,
Limit: &Limit{
Expr: expr,
},
},
}
}
func (b Builder) Aggregate(
aggExpr []*AggregationFunction,
groupExprs []Expr,
) Builder {
resolvedAggExpr := make([]*AggregationFunction, 0, len(aggExpr))
projectExprs := make([]Expr, 0, len(aggExpr))
needsPostProcessing := false
var err error
for _, agg := range aggExpr {
resolvedAggregations, projections, changed, rerr := resolveAggregation(b.plan, agg)
if err != nil {
err = errors.Join(err, rerr)
}
if changed {
needsPostProcessing = true
}
resolvedAggExpr = append(resolvedAggExpr, resolvedAggregations...)
projectExprs = append(projectExprs, projections...)
}
if !needsPostProcessing {
return Builder{
err: err,
plan: &LogicalPlan{
Aggregation: &Aggregation{
GroupExprs: groupExprs,
AggExprs: aggExpr,
},
Input: b.plan,
},
}
}
return Builder{
err: err,
plan: &LogicalPlan{
Projection: &Projection{
Exprs: append(groupExprs, projectExprs...),
},
Input: &LogicalPlan{
Aggregation: &Aggregation{
GroupExprs: groupExprs,
AggExprs: resolvedAggExpr,
},
Input: b.plan,
},
},
}
}
func resolveAggregation(plan *LogicalPlan, agg *AggregationFunction) ([]*AggregationFunction, []Expr, bool, error) {
switch agg.Func {
case AggFuncAvg:
sum := &AggregationFunction{
Func: AggFuncSum,
Expr: agg.Expr,
}
count := &AggregationFunction{
Func: AggFuncCount,
Expr: agg.Expr,
}
var (
countExpr Expr = count
aggType arrow.DataType
)
aggType, err := agg.Expr.DataType(plan)
// intentionally not handling the error here, as it will be handled
// in the build function.
if !arrow.TypeEqual(aggType, arrow.PrimitiveTypes.Int64) {
countExpr = Convert(countExpr, aggType)
}
div := (&BinaryExpr{
Left: sum,
Op: OpDiv,
Right: countExpr,
}).Alias(agg.String())
return []*AggregationFunction{sum, count}, []Expr{div}, true, err
default:
return []*AggregationFunction{agg}, []Expr{agg}, false, nil
}
}
func (b Builder) Build() (*LogicalPlan, error) {
if b.err != nil {
return nil, b.err
}
if err := Validate(b.plan); err != nil {
return nil, err
}
return b.plan, nil
}