-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
parse.go
195 lines (173 loc) · 6.19 KB
/
parse.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
194
195
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in licenses/BSD-vitess.txt.
// Portions of this file are additionally subject to the following
// license and copyright.
//
// Copyright 2015 The Cockroach 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.
// This code was derived from https://github.com/youtube/vitess.
package parser
import (
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
// Parser wraps a scanner, parser and other utilities present in the parser
// package.
type Parser struct {
scanner scanner
parserImpl sqlParserImpl
}
// Parse parses the sql and returns a list of statements.
func (p *Parser) Parse(sql string) (stmts tree.StatementList, err error) {
return p.parseWithDepth(1, sql, coltypes.Int8)
}
func (p *Parser) parseWithDepth(
depth int, sql string, nakedIntType *coltypes.TInt,
) (stmts tree.StatementList, err error) {
p.scanner.init(sql)
p.scanner.nakedIntType = nakedIntType
if p.parserImpl.Parse(&p.scanner) != 0 {
var err *pgerror.Error
if feat := p.scanner.lastError.unimplementedFeature; feat != "" {
// UnimplementedWithDepth populates the generic hint. However
// in some cases we have a more specific hint. This is overridden
// below.
err = pgerror.UnimplementedWithDepth(depth+1, "syntax."+feat, p.scanner.lastError.msg)
} else {
err = pgerror.NewErrorWithDepth(depth+1, pgerror.CodeSyntaxError, p.scanner.lastError.msg)
}
if p.scanner.lastError.hint != "" {
// If lastError.hint is not set, e.g. from (*scanner).Unimplemented(),
// we're OK with the default hint. Otherwise, override it.
err.Hint = p.scanner.lastError.hint
}
err.Detail = p.scanner.lastError.detail
return nil, err
}
return p.scanner.stmts, nil
}
// unaryNegation constructs an AST node for a negation. This attempts
// to preserve constant NumVals and embed the negative sign inside
// them instead of wrapping in an UnaryExpr. This in turn ensures
// that negative numbers get considered as a single constant
// for the purpose of formatting and scrubbing.
func unaryNegation(e tree.Expr) tree.Expr {
if cst, ok := e.(*tree.NumVal); ok {
cst.Negative = !cst.Negative
return cst
}
// Common case.
return &tree.UnaryExpr{Operator: tree.UnaryMinus, Expr: e}
}
// Parse parses a sql statement string and returns a list of Statements.
func Parse(sql string) (tree.StatementList, error) {
return parseWithDepth(1, sql, coltypes.Int8)
}
// ParseWithInt parses a sql statement string and returns a list of
// Statements. The INT token will result in the specified TInt type.
func ParseWithInt(sql string, nakedIntType *coltypes.TInt) (tree.StatementList, error) {
return parseWithDepth(1, sql, nakedIntType)
}
func parseWithDepth(
depth int, sql string, nakedIntType *coltypes.TInt,
) (tree.StatementList, error) {
var p Parser
return p.parseWithDepth(depth+1, sql, nakedIntType)
}
// ParseOne parses a sql statement string, ensuring that it contains only a
// single statement, and returns that Statement.
func ParseOne(sql string) (tree.Statement, error) {
stmts, err := parseWithDepth(1, sql, coltypes.Int8)
if err != nil {
return nil, err
}
if len(stmts) != 1 {
return nil, pgerror.NewAssertionErrorf("expected 1 statement, but found %d", len(stmts))
}
return stmts[0], nil
}
// ParseTableNameWithIndex parses a table name with index.
func ParseTableNameWithIndex(sql string) (tree.TableNameWithIndex, error) {
// We wrap the name we want to parse into a dummy statement since our parser
// can only parse full statements.
stmt, err := ParseOne(fmt.Sprintf("ALTER INDEX %s RENAME TO x", sql))
if err != nil {
return tree.TableNameWithIndex{}, err
}
rename, ok := stmt.(*tree.RenameIndex)
if !ok {
return tree.TableNameWithIndex{}, pgerror.NewAssertionErrorf("expected an ALTER INDEX statement, but found %T", stmt)
}
return *rename.Index, nil
}
// ParseTableName parses a table name.
func ParseTableName(sql string) (*tree.TableName, error) {
// We wrap the name we want to parse into a dummy statement since our parser
// can only parse full statements.
stmt, err := ParseOne(fmt.Sprintf("ALTER TABLE %s RENAME TO x", sql))
if err != nil {
return nil, err
}
rename, ok := stmt.(*tree.RenameTable)
if !ok {
return nil, pgerror.NewAssertionErrorf("expected an ALTER TABLE statement, but found %T", stmt)
}
return &rename.Name, nil
}
// parseExprs parses one or more sql expressions.
func parseExprs(exprs []string) (tree.Exprs, error) {
stmt, err := ParseOne(fmt.Sprintf("SET ROW (%s)", strings.Join(exprs, ",")))
if err != nil {
return nil, err
}
set, ok := stmt.(*tree.SetVar)
if !ok {
return nil, pgerror.NewAssertionErrorf("expected a SET statement, but found %T", stmt)
}
return set.Values, nil
}
// ParseExprs is a short-hand for parseExprs(sql)
func ParseExprs(sql []string) (tree.Exprs, error) {
if len(sql) == 0 {
return tree.Exprs{}, nil
}
return parseExprs(sql)
}
// ParseExpr is a short-hand for parseExprs([]string{sql})
func ParseExpr(sql string) (tree.Expr, error) {
exprs, err := parseExprs([]string{sql})
if err != nil {
return nil, err
}
if len(exprs) != 1 {
return nil, pgerror.NewAssertionErrorf("expected 1 expression, found %d", len(exprs))
}
return exprs[0], nil
}
// ParseType parses a column type.
func ParseType(sql string) (coltypes.CastTargetType, error) {
expr, err := ParseExpr(fmt.Sprintf("1::%s", sql))
if err != nil {
return nil, err
}
cast, ok := expr.(*tree.CastExpr)
if !ok {
return nil, pgerror.NewAssertionErrorf("expected a tree.CastExpr, but found %T", expr)
}
return cast.Type, nil
}