-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
check_invalid.go
193 lines (179 loc) · 5.66 KB
/
check_invalid.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
191
192
193
/*
Copyright 2023 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 semantics
import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
func (a *analyzer) checkForInvalidConstructs(cursor *sqlparser.Cursor) error {
switch node := cursor.Node().(type) {
case *sqlparser.Update:
return checkUpdate(node)
case *sqlparser.Select:
return a.checkSelect(cursor, node)
case *sqlparser.Nextval:
return a.checkNextVal()
case *sqlparser.JoinTableExpr:
return a.checkJoin(node)
case *sqlparser.LockingFunc:
return &LockOnlyWithDualError{Node: node}
case *sqlparser.Union:
return checkUnion(node)
case *sqlparser.JSONTableExpr:
return &JSONTablesError{}
case *sqlparser.DerivedTable:
return checkDerived(node)
case *sqlparser.AssignmentExpr:
return vterrors.VT12001("Assignment expression")
case *sqlparser.Subquery:
return a.checkSubqueryColumns(cursor.Parent(), node)
case *sqlparser.With:
if node.Recursive {
return vterrors.VT12001("recursive common table expression")
}
case *sqlparser.Insert:
if node.Action == sqlparser.ReplaceAct {
return ShardedError{Inner: &UnsupportedConstruct{errString: "REPLACE INTO with sharded keyspace"}}
}
}
return nil
}
// checkSubqueryColumns checks that subqueries used in comparisons have the correct number of columns
func (a *analyzer) checkSubqueryColumns(parent sqlparser.SQLNode, subq *sqlparser.Subquery) error {
cmp, ok := parent.(*sqlparser.ComparisonExpr)
if !ok {
return nil
}
var otherSide sqlparser.Expr
if cmp.Left == subq {
otherSide = cmp.Right
} else {
otherSide = cmp.Left
}
cols := 1
if tuple, ok := otherSide.(sqlparser.ValTuple); ok {
cols = len(tuple)
}
columns := subq.Select.GetColumns()
for _, expr := range columns {
_, ok := expr.(*sqlparser.StarExpr)
if ok {
// we can't check these queries properly. if we are able to push it down to mysql,
// it will be checked there. if not, we'll fail because we are missing the column
// information when we get to offset planning
return nil
}
}
if len(columns) != cols {
return &SubqueryColumnCountError{Expected: cols}
}
return nil
}
func checkDerived(node *sqlparser.DerivedTable) error {
if node.Lateral {
return vterrors.VT12001("lateral derived tables")
}
return nil
}
func checkUnion(node *sqlparser.Union) error {
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.ColName:
if !node.Qualifier.IsEmpty() {
return false, &QualifiedOrderInUnionError{Table: node.Qualifier.Name.String()}
}
case *sqlparser.Subquery:
return false, nil
}
return true, nil
}, node.OrderBy)
if err != nil {
return err
}
err = checkUnionColumns(node)
if err != nil {
return err
}
return nil
}
func (a *analyzer) checkJoin(j *sqlparser.JoinTableExpr) error {
if j.Join == sqlparser.NaturalJoinType || j.Join == sqlparser.NaturalRightJoinType || j.Join == sqlparser.NaturalLeftJoinType {
return &UnsupportedNaturalJoinError{JoinExpr: j}
}
return nil
}
func (a *analyzer) checkNextVal() error {
currScope := a.scoper.currentScope()
if currScope.parent != nil {
// This is defensively checking that we are not inside a subquery or derived table
// Will probably already have been checked on the SELECT level
return &CantUseOptionHereError{Msg: "INTO"}
}
if len(currScope.tables) != 1 {
// This is defensively checking that we don't have too many tables.
// Hard to check this with unit tests, since the parser does not accept these queries
return &NextWithMultipleTablesError{CountTables: len(currScope.tables)}
}
vindexTbl := currScope.tables[0].GetVindexTable()
if vindexTbl == nil {
return &MissingInVSchemaError{
Table: currScope.tables[0],
}
}
if vindexTbl.Type != vindexes.TypeSequence {
return &NotSequenceTableError{Table: vindexTbl.Name.String()}
}
return nil
}
func (a *analyzer) checkSelect(cursor *sqlparser.Cursor, node *sqlparser.Select) error {
parent := cursor.Parent()
if _, isUnion := parent.(*sqlparser.Union); isUnion && node.SQLCalcFoundRows {
return &UnionWithSQLCalcFoundRowsError{}
}
if _, isRoot := parent.(*sqlparser.RootNode); !isRoot && node.SQLCalcFoundRows {
return &SQLCalcFoundRowsUsageError{}
}
errMsg := "INTO"
nextVal := false
if len(node.SelectExprs) == 1 {
if _, isNextVal := node.SelectExprs[0].(*sqlparser.Nextval); isNextVal {
nextVal = true
errMsg = "NEXT"
}
}
if !nextVal && node.Into == nil {
return nil
}
if a.scoper.currentScope().parent != nil {
return &CantUseOptionHereError{Msg: errMsg}
}
if node.Into != nil {
return ShardedError{Inner: &UnsupportedConstruct{errString: "INTO on sharded keyspace"}}
}
return nil
}
func checkUpdate(node *sqlparser.Update) error {
if len(node.TableExprs) != 1 {
return ShardedError{Inner: &UnsupportedMultiTablesInUpdateError{ExprCount: len(node.TableExprs)}}
}
alias, isAlias := node.TableExprs[0].(*sqlparser.AliasedTableExpr)
if !isAlias {
return ShardedError{Inner: &UnsupportedMultiTablesInUpdateError{NotAlias: true}}
}
_, isDerived := alias.Expr.(*sqlparser.DerivedTable)
if isDerived {
return &TableNotUpdatableError{Table: alias.As.String()}
}
return nil
}