-
Notifications
You must be signed in to change notification settings - Fork 153
/
equijoin.go
190 lines (166 loc) · 4.77 KB
/
equijoin.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
package join
import (
"context"
"fmt"
"github.com/influxdata/flux"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
)
const EquiJoinKind = "equijoin"
func init() {
plan.RegisterPhysicalRules(EquiJoinPredicateRule{})
execute.RegisterTransformation(EquiJoinKind, createJoinTransformation)
}
type ColumnPair struct {
Left, Right string
}
type EquiJoinProcedureSpec struct {
On []ColumnPair
As interpreter.ResolvedFunction
Left *flux.TableObject
Right *flux.TableObject
Method string
}
func (p *EquiJoinProcedureSpec) Kind() plan.ProcedureKind {
return plan.ProcedureKind(EquiJoinKind)
}
func (p *EquiJoinProcedureSpec) Copy() plan.ProcedureSpec {
return &EquiJoinProcedureSpec{
On: p.On,
As: p.As,
Left: p.Left,
Right: p.Right,
Method: p.Method,
}
}
func (p *EquiJoinProcedureSpec) Cost(inStats []plan.Statistics) (cost plan.Cost, outStats plan.Statistics) {
return plan.Cost{}, plan.Statistics{}
}
func newEquiJoinProcedureSpec(spec *JoinProcedureSpec, cols []ColumnPair) *EquiJoinProcedureSpec {
return &EquiJoinProcedureSpec{
On: cols,
As: spec.As,
Left: spec.Left,
Right: spec.Right,
Method: spec.Method,
}
}
type EquiJoinPredicateRule struct{}
func (EquiJoinPredicateRule) Name() string {
return "equiJoinPredicate"
}
func (EquiJoinPredicateRule) Pattern() plan.Pattern {
return plan.MultiSuccessor(Join2Kind, plan.AnyMultiSuccessor(), plan.AnyMultiSuccessor())
}
func (EquiJoinPredicateRule) Rewrite(ctx context.Context, n plan.Node) (plan.Node, bool, error) {
s := n.ProcedureSpec()
spec, ok := s.(*JoinProcedureSpec)
if !ok {
return nil, false, errors.New(codes.Internal, "invalid spec type on join node")
}
fnBody := spec.On.Fn.Block.Body
if len(fnBody) != 1 {
return nil, false, wrapErr(
codes.Invalid,
"function body should be a single logical expression that compares columns from each table",
)
}
rs, ok := fnBody[0].(*semantic.ReturnStatement)
if !ok {
return nil, false, wrapErr(
codes.Invalid,
"function body should be a single logical expression that compares columns from each table",
)
}
cols := []ColumnPair{}
expr := rs.Argument
var walkErr error
semantic.Walk(semantic.CreateVisitor(func(n semantic.Node) {
switch e := n.(type) {
case *semantic.LogicalExpression:
if e.Operator != ast.AndOperator {
walkErr = wrapErr(
codes.Invalid,
fmt.Sprintf("unsupported operator in join predicate: %s", e.Operator.String()),
)
return
}
case *semantic.BinaryExpression:
if e.Operator != ast.EqualOperator {
walkErr = wrapErr(
codes.Invalid,
fmt.Sprintf("unsupported operator in join predicate: %s", e.Operator.String()),
)
return
}
lhs, ok := e.Left.(*semantic.MemberExpression)
if !ok {
walkErr = wrapErr(codes.Invalid, "left side of comparison is not a member expression")
return
}
rhs, ok := e.Right.(*semantic.MemberExpression)
if !ok {
walkErr = wrapErr(codes.Invalid, "right side of comparison is not a member expression")
return
}
lob, err := getObjectName(lhs)
if err != nil {
walkErr = err
return
}
rob, err := getObjectName(rhs)
if err != nil {
walkErr = err
return
}
// Each side of the binary expression should reference either the `l` or `r` object,
// but they should not reference the same object.
if !((lob == "l") != (rob == "l") && (lob == "r") != (rob == "r")) {
walkErr = wrapErr(
codes.Invalid,
"binary expression operands must reference `l` or `r` only, and may not reference the same object",
)
return
}
lcol := lhs.Property.LocalName
rcol := rhs.Property.LocalName
pair := ColumnPair{}
if lob == "l" {
pair.Left = lcol
pair.Right = rcol
} else {
pair.Left = rcol
pair.Right = lcol
}
cols = append(cols, pair)
case *semantic.MemberExpression:
case *semantic.IdentifierExpression:
default:
walkErr = wrapErr(
codes.Invalid,
fmt.Sprintf("illegal expression type in join predicate: %s", e.NodeType()),
)
}
}), expr)
if walkErr != nil {
return nil, false, walkErr
}
n.ReplaceSpec(newEquiJoinProcedureSpec(spec, cols))
return n, true, nil
}
func wrapErr(code codes.Code, msg string) error {
return errors.Newf(code, fmt.Sprintf("error in join function - some expressions are not yet supported in the `on` parameter: %s", msg))
}
func getObjectName(me *semantic.MemberExpression) (string, error) {
id, ok := me.Object.(*semantic.IdentifierExpression)
if !ok {
return "", errors.New(codes.Internal, "invalid member expression")
}
name := id.Name.LocalName
return name, nil
}