-
Notifications
You must be signed in to change notification settings - Fork 8
/
variants.go
377 lines (345 loc) · 12.8 KB
/
variants.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// SPDX-License-Identifier: Apache-2.0
package extensions
import (
"fmt"
"strings"
substraitgo "github.com/substrait-io/substrait-go"
"github.com/substrait-io/substrait-go/types"
"github.com/substrait-io/substrait-go/types/parser"
)
type FunctionVariant interface {
Name() string
CompoundName() string
Description() string
Args() ArgumentList
Options() map[string]Option
URI() string
ResolveType(argTypes []types.Type) (types.Type, error)
Variadic() *VariadicBehavior
}
func EvaluateTypeExpression(nullHandling NullabilityHandling, expr parser.TypeExpression, paramTypeList ArgumentList, actualTypes []types.Type) (types.Type, error) {
if len(paramTypeList) != len(actualTypes) {
return nil, fmt.Errorf("%w: mismatch in number of arguments provided. got %d, expected %d",
substraitgo.ErrInvalidExpr, len(actualTypes), len(paramTypeList))
}
allNonNull := true
for i, p := range paramTypeList {
switch p := p.(type) {
case EnumArg:
if actualTypes[i] != nil {
return nil, fmt.Errorf("%w: arg #%d (%s) should be an enum",
substraitgo.ErrInvalidType, i, p.Name)
}
case ValueArg:
if actualTypes[i] == nil {
return nil, fmt.Errorf("%w: arg #%d should be of type %s",
substraitgo.ErrInvalidType, i, p.toTypeString())
}
isNullable := actualTypes[i].GetNullability() != types.NullabilityRequired
if isNullable {
allNonNull = false
}
if nullHandling == DiscreteNullability {
if t, ok := p.Value.Expr.(*parser.Type); ok {
if isNullable != t.Optional() {
return nil, fmt.Errorf("%w: discrete nullability did not match for arg #%d",
substraitgo.ErrInvalidType, i)
}
} else {
return nil, substraitgo.ErrNotImplemented
}
}
case TypeArg:
return nil, substraitgo.ErrNotImplemented
}
}
var outType types.Type
if t, ok := expr.Expr.(*parser.Type); ok {
var err error
outType, err = t.Type()
if err != nil {
return nil, err
}
} else {
return nil, substraitgo.ErrNotImplemented
}
if nullHandling == MirrorNullability || nullHandling == "" {
if allNonNull {
return outType.WithNullability(types.NullabilityRequired), nil
}
return outType.WithNullability(types.NullabilityNullable), nil
}
return outType, nil
}
func parseFuncName(compoundName string) (name string, args ArgumentList) {
name, argsStr, _ := strings.Cut(compoundName, ":")
if len(argsStr) == 0 {
return name, nil
}
splitArgs := strings.Split(argsStr, "_")
for _, argStr := range splitArgs {
parsed, err := defParser.ParseString(argStr)
if err != nil {
panic(err)
}
exp := ValueArg{Name: name, Value: parsed}
args = append(args, exp)
}
return name, args
}
// NewScalarFuncVariant constructs a variant with the provided name and uri
// and uses the defaults for everything else.
//
// Return expressions aren't included here as using this variant to construct
// an expression requires an output type argument. This is for creating an
// on-the-fly function variant that will not be registered as an extension.
func NewScalarFuncVariant(id ID) *ScalarFunctionVariant {
simpleName, args := parseFuncName(id.Name)
return &ScalarFunctionVariant{
name: simpleName,
uri: id.URI,
impl: ScalarFunctionImpl{Args: args},
}
}
// NewScalarFuncVariantWithProps is the same as NewScalarFuncVariant but allows
// setting the values for the SessionDependant, Variadic Behavior and Deterministic
// properties.
func NewScalarFuncVariantWithProps(id ID, variadic *VariadicBehavior, sessionDependant, deterministic bool) *ScalarFunctionVariant {
simpleName, args := parseFuncName(id.Name)
return &ScalarFunctionVariant{
name: simpleName,
uri: id.URI,
impl: ScalarFunctionImpl{
Args: args,
Variadic: variadic,
SessionDependent: sessionDependant,
Deterministic: deterministic,
},
}
}
type ScalarFunctionVariant struct {
name string
description string
uri string
impl ScalarFunctionImpl
}
func (s *ScalarFunctionVariant) Name() string { return s.name }
func (s *ScalarFunctionVariant) Description() string { return s.description }
func (s *ScalarFunctionVariant) Args() ArgumentList { return s.impl.Args }
func (s *ScalarFunctionVariant) Options() map[string]Option { return s.impl.Options }
func (s *ScalarFunctionVariant) Variadic() *VariadicBehavior { return s.impl.Variadic }
func (s *ScalarFunctionVariant) Deterministic() bool { return s.impl.Deterministic }
func (s *ScalarFunctionVariant) SessionDependent() bool { return s.impl.SessionDependent }
func (s *ScalarFunctionVariant) Nullability() NullabilityHandling { return s.impl.Nullability }
func (s *ScalarFunctionVariant) URI() string { return s.uri }
func (s *ScalarFunctionVariant) ResolveType(argumentTypes []types.Type) (types.Type, error) {
return EvaluateTypeExpression(s.impl.Nullability, s.impl.Return, s.impl.Args, argumentTypes)
}
func (s *ScalarFunctionVariant) CompoundName() string {
return s.name + ":" + s.impl.signatureKey()
}
func (s *ScalarFunctionVariant) ID() ID {
return ID{URI: s.uri, Name: s.CompoundName()}
}
// NewAggFuncVariant constructs a variant with the provided name and uri
// and uses the defaults for everything else.
//
// Return expressions aren't included here as using this variant to construct
// an expression requires an output type argument. This is for creating an
// on-the-fly function variant that will not be registered as an extension.
func NewAggFuncVariant(id ID) *AggregateFunctionVariant {
simpleName, args := parseFuncName(id.Name)
return &AggregateFunctionVariant{
name: simpleName,
uri: id.URI,
impl: AggregateFunctionImpl{
ScalarFunctionImpl: ScalarFunctionImpl{
Args: args,
},
Decomposable: DecomposeNone,
},
}
}
type AggVariantOptions struct {
Variadic *VariadicBehavior
SessionDependant bool
Deterministic bool
Ordered bool
// value of 0 == unlimited
MaxSet uint
Decomposable DecomposeType
// should be a type expression
// must not be empty if decomposable is not DecomposeNone
IntermediateOutputType string
}
var (
defParser, _ = parser.New()
)
func NewAggFuncVariantOpts(id ID, opts AggVariantOptions) *AggregateFunctionVariant {
var aggIntermediate parser.TypeExpression
if opts.Decomposable == "" {
opts.Decomposable = DecomposeNone
}
if opts.Decomposable != DecomposeNone {
if opts.IntermediateOutputType == "" {
panic(fmt.Errorf("%w: custom Aggregate function variant %s. must provide Intermediate output type",
substraitgo.ErrInvalidExpr, id))
}
intermediate, err := defParser.ParseString(opts.IntermediateOutputType)
if err != nil {
panic(err)
}
aggIntermediate = *intermediate
}
simpleName, args := parseFuncName(id.Name)
return &AggregateFunctionVariant{
name: simpleName,
uri: id.URI,
impl: AggregateFunctionImpl{
ScalarFunctionImpl: ScalarFunctionImpl{
Args: args,
Variadic: opts.Variadic,
SessionDependent: opts.SessionDependant,
Deterministic: opts.Deterministic,
},
Ordered: opts.Ordered,
MaxSet: int(opts.MaxSet),
Decomposable: opts.Decomposable,
Intermediate: aggIntermediate,
},
}
}
type AggregateFunctionVariant struct {
name string
description string
uri string
impl AggregateFunctionImpl
}
func (s *AggregateFunctionVariant) Name() string { return s.name }
func (s *AggregateFunctionVariant) Description() string { return s.description }
func (s *AggregateFunctionVariant) Args() ArgumentList { return s.impl.Args }
func (s *AggregateFunctionVariant) Options() map[string]Option { return s.impl.Options }
func (s *AggregateFunctionVariant) Variadic() *VariadicBehavior { return s.impl.Variadic }
func (s *AggregateFunctionVariant) Deterministic() bool { return s.impl.Deterministic }
func (s *AggregateFunctionVariant) SessionDependent() bool { return s.impl.SessionDependent }
func (s *AggregateFunctionVariant) Nullability() NullabilityHandling { return s.impl.Nullability }
func (s *AggregateFunctionVariant) URI() string { return s.uri }
func (s *AggregateFunctionVariant) ResolveType(argumentTypes []types.Type) (types.Type, error) {
return EvaluateTypeExpression(s.impl.Nullability, s.impl.Return, s.impl.Args, argumentTypes)
}
func (s *AggregateFunctionVariant) CompoundName() string {
return s.name + ":" + s.impl.signatureKey()
}
func (s *AggregateFunctionVariant) ID() ID {
return ID{URI: s.uri, Name: s.CompoundName()}
}
func (s *AggregateFunctionVariant) Decomposability() DecomposeType { return s.impl.Decomposable }
func (s *AggregateFunctionVariant) Intermediate() (types.Type, error) {
if t, ok := s.impl.Intermediate.Expr.(*parser.Type); ok {
return t.Type()
}
return nil, fmt.Errorf("%w: bad intermediate type expression", substraitgo.ErrInvalidType)
}
func (s *AggregateFunctionVariant) Ordered() bool { return s.impl.Ordered }
func (s *AggregateFunctionVariant) MaxSet() int { return s.impl.MaxSet }
type WindowFunctionVariant struct {
name string
description string
uri string
impl WindowFunctionImpl
}
func NewWindowFuncVariant(id ID) *WindowFunctionVariant {
simpleName, args := parseFuncName(id.Name)
return &WindowFunctionVariant{
name: simpleName,
uri: id.URI,
impl: WindowFunctionImpl{
AggregateFunctionImpl: AggregateFunctionImpl{
ScalarFunctionImpl: ScalarFunctionImpl{Args: args},
Decomposable: DecomposeNone,
},
WindowType: PartitionWindow,
},
}
}
type WindowVariantOpts struct {
Variadic *VariadicBehavior
SessionDependant bool
Deterministic bool
Ordered bool
// value of 0 == unlimited
MaxSet uint
Decomposable DecomposeType
// should be a type expression
// must not be empty if decomposable is not DecomposeNone
IntermediateOutputType string
WindowType WindowType
}
func NewWindowFuncVariantOpts(id ID, opts WindowVariantOpts) *WindowFunctionVariant {
var aggIntermediate parser.TypeExpression
if opts.Decomposable == "" {
opts.Decomposable = DecomposeNone
}
if opts.WindowType == "" {
opts.WindowType = PartitionWindow
}
if opts.Decomposable != DecomposeNone {
if opts.IntermediateOutputType == "" {
panic(fmt.Errorf("%w: custom Aggregate function variant %s. must provide Intermediate output type",
substraitgo.ErrInvalidExpr, id))
}
intermediate, err := defParser.ParseString(opts.IntermediateOutputType)
if err != nil {
panic(err)
}
aggIntermediate = *intermediate
}
simpleName, args := parseFuncName(id.Name)
return &WindowFunctionVariant{
name: simpleName,
uri: id.URI,
impl: WindowFunctionImpl{
AggregateFunctionImpl: AggregateFunctionImpl{
ScalarFunctionImpl: ScalarFunctionImpl{
Args: args,
Variadic: opts.Variadic,
SessionDependent: opts.SessionDependant,
Deterministic: opts.Deterministic,
},
Ordered: opts.Ordered,
MaxSet: int(opts.MaxSet),
Decomposable: opts.Decomposable,
Intermediate: aggIntermediate,
},
WindowType: opts.WindowType,
},
}
}
func (s *WindowFunctionVariant) Name() string { return s.name }
func (s *WindowFunctionVariant) Description() string { return s.description }
func (s *WindowFunctionVariant) Args() ArgumentList { return s.impl.Args }
func (s *WindowFunctionVariant) Options() map[string]Option { return s.impl.Options }
func (s *WindowFunctionVariant) Variadic() *VariadicBehavior { return s.impl.Variadic }
func (s *WindowFunctionVariant) Deterministic() bool { return s.impl.Deterministic }
func (s *WindowFunctionVariant) SessionDependent() bool { return s.impl.SessionDependent }
func (s *WindowFunctionVariant) Nullability() NullabilityHandling { return s.impl.Nullability }
func (s *WindowFunctionVariant) URI() string { return s.uri }
func (s *WindowFunctionVariant) ResolveType(argumentTypes []types.Type) (types.Type, error) {
return EvaluateTypeExpression(s.impl.Nullability, s.impl.Return, s.impl.Args, argumentTypes)
}
func (s *WindowFunctionVariant) CompoundName() string {
return s.name + ":" + s.impl.signatureKey()
}
func (s *WindowFunctionVariant) ID() ID {
return ID{URI: s.uri, Name: s.CompoundName()}
}
func (s *WindowFunctionVariant) Decomposability() DecomposeType { return s.impl.Decomposable }
func (s *WindowFunctionVariant) Intermediate() (types.Type, error) {
if t, ok := s.impl.Intermediate.Expr.(*parser.Type); ok {
return t.Type()
}
return nil, fmt.Errorf("%w: bad intermediate type expression", substraitgo.ErrInvalidType)
}
func (s *WindowFunctionVariant) Ordered() bool { return s.impl.Ordered }
func (s *WindowFunctionVariant) MaxSet() int { return s.impl.MaxSet }
func (s *WindowFunctionVariant) WindowType() WindowType { return s.impl.WindowType }