-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
subquery.go
110 lines (93 loc) · 2.86 KB
/
subquery.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
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package operators
import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
)
type (
// SubQuery stores the information about subquery
SubQuery struct {
Outer ops.Operator
Inner []*SubQueryInner
noColumns
noPredicates
}
// SubQueryInner stores the subquery information for a select statement
SubQueryInner struct {
// Inner is the Operator inside the parenthesis of the subquery.
// i.e: select (select 1 union select 1), the Inner here would be
// of type Concatenate since we have a Union.
Inner ops.Operator
// ExtractedSubquery contains all information we need about this subquery
ExtractedSubquery *sqlparser.ExtractedSubquery
noColumns
noPredicates
}
)
var _ ops.Operator = (*SubQuery)(nil)
var _ ops.Operator = (*SubQueryInner)(nil)
// Clone implements the Operator interface
func (s *SubQueryInner) Clone(inputs []ops.Operator) ops.Operator {
return &SubQueryInner{
Inner: inputs[0],
ExtractedSubquery: s.ExtractedSubquery,
}
}
// Inputs implements the Operator interface
func (s *SubQueryInner) Inputs() []ops.Operator {
return []ops.Operator{s.Inner}
}
// Clone implements the Operator interface
func (s *SubQuery) Clone(inputs []ops.Operator) ops.Operator {
result := &SubQuery{
Outer: inputs[0],
}
for idx := range s.Inner {
inner, ok := inputs[idx+1].(*SubQueryInner)
if !ok {
panic("got bad input")
}
result.Inner = append(result.Inner, inner)
}
return result
}
// Inputs implements the Operator interface
func (s *SubQuery) Inputs() []ops.Operator {
operators := []ops.Operator{s.Outer}
for _, inner := range s.Inner {
operators = append(operators, inner)
}
return operators
}
func createSubqueryFromStatement(ctx *plancontext.PlanningContext, stmt sqlparser.Statement) (*SubQuery, error) {
if len(ctx.SemTable.SubqueryMap[stmt]) == 0 {
return nil, nil
}
subq := &SubQuery{}
for _, sq := range ctx.SemTable.SubqueryMap[stmt] {
opInner, err := createLogicalOperatorFromAST(ctx, sq.Subquery.Select)
if err != nil {
return nil, err
}
if horizon, ok := opInner.(*Horizon); ok {
opInner = horizon.Source
}
subq.Inner = append(subq.Inner, &SubQueryInner{
ExtractedSubquery: sq,
Inner: opInner,
})
}
return subq, nil
}