/
function.go
144 lines (134 loc) · 3.88 KB
/
function.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
package logical
import (
"context"
"fmt"
"strings"
"github.com/cube2222/octosql/octosql"
"github.com/cube2222/octosql/physical"
)
type FunctionExpression struct {
Name string
Arguments []Expression
}
func NewFunctionExpression(name string, args []Expression) *FunctionExpression {
return &FunctionExpression{
Name: name,
Arguments: args,
}
}
func (fe *FunctionExpression) Typecheck(ctx context.Context, env physical.Environment, logicalEnv Environment) physical.Expression {
arguments := make([]physical.Expression, len(fe.Arguments))
for i := range fe.Arguments {
arguments[i] = fe.Arguments[i].Typecheck(ctx, env, logicalEnv)
}
argumentTypes := make([]octosql.Type, len(arguments))
nonNullableArgumentTypes := make([]octosql.Type, len(arguments))
for i := range arguments {
argumentTypes[i] = arguments[i].Type
nonNullableArgumentTypes[i] = octosql.NonNullable(arguments[i].Type)
}
var out physical.Expression
var found bool
details := env.Functions[fe.Name]
descriptorLoop:
for _, descriptor := range details.Descriptors {
argTypes := argumentTypes
if descriptor.Strict {
argTypes = nonNullableArgumentTypes
}
if descriptor.TypeFn != nil {
if outputType, ok := descriptor.TypeFn(argTypes); ok {
found = true
out = physical.Expression{
Type: outputType,
ExpressionType: physical.ExpressionTypeFunctionCall,
FunctionCall: &physical.FunctionCall{
Name: fe.Name,
Arguments: arguments,
FunctionDescriptor: descriptor,
},
}
}
} else {
if len(argTypes) != len(descriptor.ArgumentTypes) {
continue
}
for i := range argTypes {
if argTypes[i].Is(descriptor.ArgumentTypes[i]) < octosql.TypeRelationIs {
continue descriptorLoop
}
}
found = true
out = physical.Expression{
Type: descriptor.OutputType,
ExpressionType: physical.ExpressionTypeFunctionCall,
FunctionCall: &physical.FunctionCall{
Name: fe.Name,
Arguments: arguments,
FunctionDescriptor: descriptor,
},
}
}
}
if !found {
descriptorLoop2:
for _, descriptor := range details.Descriptors {
argTypes := argumentTypes
if descriptor.Strict {
argTypes = nonNullableArgumentTypes
}
if len(argTypes) != len(descriptor.ArgumentTypes) {
continue
}
isMaybe := make([]bool, len(argTypes))
for i := range argTypes {
if rel := argTypes[i].Is(descriptor.ArgumentTypes[i]); rel < octosql.TypeRelationMaybe {
continue descriptorLoop2
} else if rel == octosql.TypeRelationMaybe {
isMaybe[i] = true
}
}
for i := range arguments {
if isMaybe[i] {
targetType := descriptor.ArgumentTypes[i]
if descriptor.Strict {
targetType = octosql.TypeSum(targetType, octosql.Null)
}
arguments[i] = physical.Expression{
ExpressionType: physical.ExpressionTypeTypeAssertion,
Type: *octosql.TypeIntersection(targetType, arguments[i].Type),
TypeAssertion: &physical.TypeAssertion{
Expression: arguments[i],
TargetType: targetType,
},
}
}
}
found = true
out = physical.Expression{
Type: descriptor.OutputType,
ExpressionType: physical.ExpressionTypeFunctionCall,
FunctionCall: &physical.FunctionCall{
Name: fe.Name,
Arguments: arguments,
FunctionDescriptor: descriptor,
},
}
}
}
if !found {
argTypeNames := make([]string, len(arguments))
for i := range argTypeNames {
argTypeNames[i] = arguments[i].Type.String()
}
panic(fmt.Sprintf("unknown function: %s(%s)", fe.Name, strings.Join(argTypeNames, ", ")))
}
if out.FunctionCall.FunctionDescriptor.Strict {
for i := range out.FunctionCall.Arguments {
if octosql.Null.Is(out.FunctionCall.Arguments[i].Type) == octosql.TypeRelationIs {
out.Type = octosql.TypeSum(out.Type, octosql.Null)
}
}
}
return out
}