/
push_filter_into_stream_join_key.go
97 lines (88 loc) · 3.01 KB
/
push_filter_into_stream_join_key.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
package optimizer
import (
"github.com/cube2222/octosql/octosql"
. "github.com/cube2222/octosql/physical"
)
func PushDownFilterPredicatesIntoStreamJoinKey(node Node) (Node, bool) {
changed := false
t := Transformers{
NodeTransformer: func(node Node) Node {
if node.NodeType != NodeTypeFilter {
return node
}
if node.Filter.Source.NodeType != NodeTypeStreamJoin {
return node
}
leftSchema := node.Filter.Source.StreamJoin.Left.Schema
rightSchema := node.Filter.Source.StreamJoin.Right.Schema
filterPredicates := node.Filter.Predicate.SplitByAnd()
var stayedAbove, leftKeyAdd, rightKeyAdd []Expression
for i := range filterPredicates {
if filterPredicates[i].ExpressionType != ExpressionTypeFunctionCall {
stayedAbove = append(stayedAbove, filterPredicates[i])
continue
}
if filterPredicates[i].FunctionCall.Name != "=" {
stayedAbove = append(stayedAbove, filterPredicates[i])
continue
}
firstPart := filterPredicates[i].FunctionCall.Arguments[0]
secondPart := filterPredicates[i].FunctionCall.Arguments[1]
firstPartVariables := firstPart.VariablesUsed()
firstPartUsesLeftVariables := UsesVariablesFromSchema(leftSchema, firstPartVariables)
firstPartUsesRightVariables := UsesVariablesFromSchema(rightSchema, firstPartVariables)
secondPartVariables := secondPart.VariablesUsed()
secondPartUsesLeftVariables := UsesVariablesFromSchema(leftSchema, secondPartVariables)
secondPartUsesRightVariables := UsesVariablesFromSchema(rightSchema, secondPartVariables)
if firstPartUsesLeftVariables && !firstPartUsesRightVariables &&
!secondPartUsesLeftVariables && secondPartUsesRightVariables {
leftKeyAdd = append(leftKeyAdd, firstPart)
rightKeyAdd = append(rightKeyAdd, secondPart)
} else if !firstPartUsesLeftVariables && firstPartUsesRightVariables &&
secondPartUsesLeftVariables && !secondPartUsesRightVariables {
rightKeyAdd = append(rightKeyAdd, firstPart)
leftKeyAdd = append(leftKeyAdd, secondPart)
} else {
stayedAbove = append(stayedAbove, filterPredicates[i])
}
}
if len(stayedAbove) == len(filterPredicates) {
return node
}
changed = true
out := Node{
Schema: node.Filter.Source.Schema,
NodeType: NodeTypeStreamJoin,
StreamJoin: &StreamJoin{
LeftKey: append(node.Filter.Source.StreamJoin.LeftKey, leftKeyAdd...),
RightKey: append(node.Filter.Source.StreamJoin.RightKey, rightKeyAdd...),
Left: node.Filter.Source.StreamJoin.Left,
Right: node.Filter.Source.StreamJoin.Right,
},
}
if len(stayedAbove) > 0 {
out = Node{
Schema: out.Schema,
NodeType: NodeTypeFilter,
Filter: &Filter{
Predicate: Expression{
Type: octosql.Boolean,
ExpressionType: ExpressionTypeAnd,
And: &And{
Arguments: stayedAbove,
},
},
Source: out,
},
}
}
return out
},
}
output := t.TransformNode(node)
if changed {
return output, true
} else {
return node, false
}
}