-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathsqlflow_parser.go
291 lines (261 loc) · 7.62 KB
/
sqlflow_parser.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2020 The SQLFlow Authors. All rights reserved.
// 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 parser
import (
"fmt"
"strings"
"sqlflow.org/sqlflow/go/parser/external"
)
// SQLFlowStmt represents a parsed SQL statement. The original
// statement is in Original. If it is a standard SQL statement,
// Original has the statement as well, and Extended is nil. Or, if it
// is a statement with SQLFlow syntax extension, Original is the whole
// statement and Extended is the parsed extension.
type SQLFlowStmt struct {
IsUnfinishedSelect bool
Original string
*SQLFlowSelectStmt
Inputs []string
Outputs []string
}
// IsExtendedSyntax returns true if a parsed statement uses any
// SQLFlow syntax extensions.
func (stmt *SQLFlowStmt) IsExtendedSyntax() bool {
return stmt.SQLFlowSelectStmt != nil
}
// ParseStatement parses a SQL program by calling Parse, and
// asserts that this program contains one and only one statement.
// Notice: If program contains more than one statement,
// we should call sql.RewriteSQLWithHints
func ParseStatement(dialect, program string) (*SQLFlowStmt, error) {
stmts, err := Parse(dialect, program)
if err != nil {
return nil, err
}
if len(stmts) != 1 {
return nil, fmt.Errorf("expecting one statement, got %d", len(stmts))
}
return stmts[0], nil
}
// Parse a SQL program in the given dialect into a list of SQL statements.
func Parse(dialect, program string) ([]*SQLFlowStmt, error) {
//all := []*SQLFlowStmt{{Original: `SHOW create table sqlflow_models.my_dnn_model;`}}
all := []*SQLFlowStmt{}
for {
// SELECT ...; SELECT * FROM my_table TO TRAIN ...
// ^
// i
// or
// SELECT ...; SHOW TRAIN my_model;
// ^
// i
sqls, i, err := thirdPartyParse(dialect, program)
if err != nil {
return nil, err
}
all = append(all, sqls...)
if i < 0 {
return all, nil
}
program = program[i:]
extended, j, err := parseFirstSQLFlowStmt(program)
if err != nil {
return nil, err
}
// SELECT ... .TO ...
if len(sqls) > 0 && sqls[len(sqls)-1].IsUnfinishedSelect {
if extended.ShowTrain {
return nil, fmt.Errorf("select should followed by 'to train/predict/explain'")
}
left := all[len(all)-1].Original
right := program[:j]
all[len(all)-1].Original = left + right
all[len(all)-1].SQLFlowSelectStmt = extended
all[len(all)-1].StandardSelect.origin = left
program = program[j:]
} else {
// Purely extended sql stmt
if !extended.ShowTrain {
return nil, fmt.Errorf("invalid 'to train/predict/explain' with no 'select'")
}
sql := &SQLFlowStmt{Original: program[:j], SQLFlowSelectStmt: extended}
all = append(all, sql)
program = program[j:]
}
if len(strings.TrimSpace(program)) == 0 {
return all, nil
}
}
}
func parseFirstSQLFlowStmt(program string) (*SQLFlowSelectStmt, int, error) {
// extendedSyntaxDebug = 5
// extendedSyntaxErrorVerbose = true
pr, idx, err := parseSQLFlowStmt(program)
if err != nil {
var e error
pr, idx, e = parseSQLFlowStmt(program[:idx])
if e != nil {
// return the original error since it saw the entire program
return nil, -1, err
}
return pr, idx, nil
}
return pr, idx, nil
}
func thirdPartyParse(dialect, program string) ([]*SQLFlowStmt, int, error) {
p, err := external.NewParser(dialect)
if err != nil {
return nil, -1, fmt.Errorf("thirdPartyParse failed: %v", err)
}
sqls, i, err := p.Parse(program)
if err != nil {
return nil, -1, fmt.Errorf("thirdPartyParse failed: %v", err)
}
var spr []*SQLFlowStmt
for _, sql := range sqls {
spr = append(spr, &SQLFlowStmt{
Original: sql.String,
Inputs: sql.Inputs,
Outputs: sql.Outputs,
SQLFlowSelectStmt: nil,
IsUnfinishedSelect: sql.IsUnfinishedSelect})
}
return spr, i, nil
}
// findMatchedQuotationMarks finds the matched "" and '' indices in the
// SQL statement.
func findMatchedQuotationMarks(sql string) ([]int, []int, error) {
// check whether the quotation mark " or ' is in the form of \" or \'
isEscapeRune := func(idx int) bool {
cnt := 0
for idx--; idx >= 0; idx-- {
if sql[idx] != '\\' {
break
}
cnt++
}
return cnt%2 == 1
}
leftQuotationIndex := make([]int, 0)
rightQuotationIndex := make([]int, 0)
offset := 0
for {
leftIdx := strings.IndexAny(sql[offset:], `"'`)
if leftIdx < 0 {
break
}
target := rune(sql[offset+leftIdx])
offset += leftIdx + 1
for {
rightIdx := strings.IndexRune(sql[offset:], target)
if rightIdx < 0 {
return nil, nil, fmt.Errorf("unmatched quotation marks")
}
offset += rightIdx + 1
if !isEscapeRune(offset - 1) {
leftQuotationIndex = append(leftQuotationIndex, leftIdx)
rightQuotationIndex = append(rightQuotationIndex, offset-1)
break
}
}
}
return leftQuotationIndex, rightQuotationIndex, nil
}
func removeSingleLineComment(sql string) (string, error) {
leftQuotationIndices, rightQuotationIndices, err := findMatchedQuotationMarks(sql)
if err != nil {
return "", err
}
findRightQuotationIndex := func(idx int) int {
for i := range leftQuotationIndices {
if idx >= leftQuotationIndices[i] && idx <= rightQuotationIndices[i] {
return rightQuotationIndices[i]
}
}
return -1
}
resultSQL := ""
offset := 0
for {
leftIdx := strings.Index(sql[offset:], "--")
if leftIdx < 0 {
resultSQL += sql[offset:]
break
}
rightQuotationIdx := findRightQuotationIndex(offset + leftIdx)
if rightQuotationIdx >= 0 {
resultSQL += sql[offset : rightQuotationIdx+1]
offset = rightQuotationIdx + 1
continue
}
resultSQL += sql[offset : offset+leftIdx]
rightIdx := strings.Index(sql[offset+leftIdx+2:], "\n")
if rightIdx < 0 {
break
}
offset += leftIdx + rightIdx + 2
}
return resultSQL, nil
}
func removeMultipleLineComment(sql string) (string, error) {
leftQuotationIndices, rightQuotationIndices, err := findMatchedQuotationMarks(sql)
if err != nil {
return "", err
}
findRightQuotationIndex := func(idx int) int {
for i := range leftQuotationIndices {
if idx >= leftQuotationIndices[i] && idx <= rightQuotationIndices[i] {
return rightQuotationIndices[i]
}
}
return -1
}
resultSQL := ""
offset := 0
for {
leftIdx := strings.Index(sql[offset:], "/*")
if leftIdx < 0 {
resultSQL += sql[offset:]
break
}
rightQuotationIdx := findRightQuotationIndex(offset + leftIdx)
if rightQuotationIdx >= 0 {
resultSQL += sql[offset : rightQuotationIdx+1]
offset = rightQuotationIdx + 1
continue
}
resultSQL += sql[offset : offset+leftIdx]
offset += leftIdx + 2
rightIdx := strings.Index(sql[offset:], "*/")
if rightIdx < 0 {
return "", fmt.Errorf("unmatched comment /*...*/")
}
resultSQL += " "
offset += rightIdx + 2
}
return resultSQL, nil
}
// RemoveCommentInSQLStatement removes the comments in the
// SQL statement, including single line comment (--) and multiple
// line comment (/*...*/)
func RemoveCommentInSQLStatement(sql string) (string, error) {
sql, err := removeMultipleLineComment(sql)
if err != nil {
return "", err
}
sql, err = removeSingleLineComment(sql)
if err != nil {
return "", err
}
return sql, nil
}