forked from dolthub/go-mysql-server
/
subqueryalias.go
152 lines (130 loc) · 4.18 KB
/
subqueryalias.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
// Copyright 2020-2021 Dolthub, Inc.
//
// 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 plan
import (
"fmt"
"github.com/gabereiser/go-mysql-server/sql"
)
// SubqueryAlias is a node that gives a subquery a name.
type SubqueryAlias struct {
UnaryNode
Columns []string
name string
TextDefinition string
// OuterScopeVisibility is true when a SubqueryAlias (i.e. derived table) is contained in a subquery
// expression and is eligible to have visibility to outer scopes of the query.
OuterScopeVisibility bool
CanCacheResults bool
}
// NewSubqueryAlias creates a new SubqueryAlias node.
func NewSubqueryAlias(name, textDefinition string, node sql.Node) *SubqueryAlias {
return &SubqueryAlias{
UnaryNode: UnaryNode{Child: node},
name: name,
TextDefinition: textDefinition,
OuterScopeVisibility: false,
}
}
// AsView returns the view wrapper for this subquery
func (sq *SubqueryAlias) AsView(createViewStmt string) *sql.View {
return sql.NewView(sq.Name(), sq, sq.TextDefinition, createViewStmt)
}
// Name implements the Table interface.
func (sq *SubqueryAlias) Name() string { return sq.name }
// Schema implements the Node interface.
func (sq *SubqueryAlias) Schema() sql.Schema {
childSchema := sq.Child.Schema()
schema := make(sql.Schema, len(childSchema))
for i, col := range childSchema {
c := *col
c.Source = sq.name
if len(sq.Columns) > 0 {
c.Name = sq.Columns[i]
}
schema[i] = &c
}
return schema
}
// RowIter implements the Node interface.
func (sq *SubqueryAlias) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
span, ctx := ctx.Span("plan.SubqueryAlias")
if !sq.OuterScopeVisibility {
row = nil
}
iter, err := sq.Child.RowIter(ctx, row)
if err != nil {
span.End()
return nil, err
}
return sql.NewSpanIter(span, iter), nil
}
// WithChildren implements the Node interface.
func (sq *SubqueryAlias) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(sq, len(children), 1)
}
nn := *sq
nn.Child = children[0]
return &nn, nil
}
// CheckPrivileges implements the interface sql.Node.
func (sq *SubqueryAlias) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return sq.Child.CheckPrivileges(ctx, opChecker)
}
func (sq *SubqueryAlias) WithChild(n sql.Node) *SubqueryAlias {
ret := *sq
ret.Child = n
return &ret
}
func (sq *SubqueryAlias) WithName(name string) *SubqueryAlias {
ret := *sq
ret.name = name
return &ret
}
func (sq *SubqueryAlias) WithCachedResults() *SubqueryAlias {
ret := *sq
ret.CanCacheResults = true
return &ret
}
// Opaque implements the OpaqueNode interface.
func (sq *SubqueryAlias) Opaque() bool {
return true
}
func (sq *SubqueryAlias) String() string {
pr := sql.NewTreePrinter()
_ = pr.WriteNode("SubqueryAlias")
children := make([]string, 4)
children[0] = fmt.Sprintf("name: %s", sq.name)
children[1] = fmt.Sprintf("outerVisibility: %t", sq.OuterScopeVisibility)
children[2] = fmt.Sprintf("cacheable: %t", sq.CanCacheResults)
children[3] = sq.Child.String()
_ = pr.WriteChildren(children...)
return pr.String()
}
func (sq *SubqueryAlias) DebugString() string {
pr := sql.NewTreePrinter()
_ = pr.WriteNode("SubqueryAlias")
children := make([]string, 4)
children[0] = fmt.Sprintf("name: %s", sq.name)
children[1] = fmt.Sprintf("outerVisibility: %t", sq.OuterScopeVisibility)
children[2] = fmt.Sprintf("cacheable: %t", sq.CanCacheResults)
children[3] = sql.DebugString(sq.Child)
_ = pr.WriteChildren(children...)
return pr.String()
}
func (sq *SubqueryAlias) WithColumns(columns []string) *SubqueryAlias {
ret := *sq
ret.Columns = columns
return &ret
}