-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathexpander.go
More file actions
507 lines (449 loc) · 12.9 KB
/
expander.go
File metadata and controls
507 lines (449 loc) · 12.9 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package expander
import (
"context"
"fmt"
"io"
"strings"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
"github.com/sqlc-dev/sqlc/internal/sql/format"
)
// Parser is an interface for SQL parsers that can parse SQL into AST statements.
type Parser interface {
Parse(r io.Reader) ([]ast.Statement, error)
}
// ColumnGetter retrieves column names for a query by preparing it against a database.
type ColumnGetter interface {
GetColumnNames(ctx context.Context, query string) ([]string, error)
}
// Expander expands SELECT * and RETURNING * queries by replacing * with explicit column names
// obtained from preparing the query against a database.
type Expander struct {
colGetter ColumnGetter
parser Parser
dialect format.Dialect
}
// New creates a new Expander with the given column getter, parser, and dialect.
func New(colGetter ColumnGetter, parser Parser, dialect format.Dialect) *Expander {
return &Expander{
colGetter: colGetter,
parser: parser,
dialect: dialect,
}
}
// Expand takes a SQL query, and if it contains * in SELECT or RETURNING clause,
// expands it to use explicit column names. Returns the expanded query string.
func (e *Expander) Expand(ctx context.Context, query string) (string, error) {
// Parse the query
stmts, err := e.parser.Parse(strings.NewReader(query))
if err != nil {
return "", fmt.Errorf("failed to parse query: %w", err)
}
if len(stmts) == 0 {
return query, nil
}
stmt := stmts[0].Raw.Stmt
// Check if there's any star in the statement (including CTEs, subqueries, etc.)
if !hasStarAnywhere(stmt) {
return query, nil
}
// Expand all stars in the statement recursively
if err := e.expandNode(ctx, stmt); err != nil {
return "", err
}
// Format the modified AST back to SQL
expanded := ast.Format(stmts[0].Raw, e.dialect)
return expanded, nil
}
// expandNode recursively expands * in all parts of the statement
func (e *Expander) expandNode(ctx context.Context, node ast.Node) error {
if node == nil {
return nil
}
switch n := node.(type) {
case *ast.SelectStmt:
return e.expandSelectStmt(ctx, n)
case *ast.InsertStmt:
return e.expandInsertStmt(ctx, n)
case *ast.UpdateStmt:
return e.expandUpdateStmt(ctx, n)
case *ast.DeleteStmt:
return e.expandDeleteStmt(ctx, n)
case *ast.CommonTableExpr:
return e.expandNode(ctx, n.Ctequery)
}
return nil
}
// expandSelectStmt expands * in a SELECT statement including CTEs and subqueries
func (e *Expander) expandSelectStmt(ctx context.Context, stmt *ast.SelectStmt) error {
// First expand any CTEs - must be done in order since later CTEs may depend on earlier ones
if stmt.WithClause != nil && stmt.WithClause.Ctes != nil {
for _, cteNode := range stmt.WithClause.Ctes.Items {
cte, ok := cteNode.(*ast.CommonTableExpr)
if !ok {
continue
}
cteSelect, ok := cte.Ctequery.(*ast.SelectStmt)
if !ok {
continue
}
if hasStarInList(cteSelect.TargetList) {
// Get column names for this CTE
columns, err := e.getCTEColumnNames(ctx, stmt, cte)
if err != nil {
return err
}
cteSelect.TargetList = rewriteTargetList(cteSelect.TargetList, columns)
}
// Recursively handle nested CTEs/subqueries in this CTE
if err := e.expandSelectStmtInner(ctx, cteSelect); err != nil {
return err
}
}
}
// Expand subqueries in FROM clause
if stmt.FromClause != nil {
for _, fromItem := range stmt.FromClause.Items {
if err := e.expandFromClause(ctx, fromItem); err != nil {
return err
}
}
}
// Expand the target list if it has stars
if hasStarInList(stmt.TargetList) {
// Format the current state to get columns
tempRaw := &ast.RawStmt{Stmt: stmt}
tempQuery := ast.Format(tempRaw, e.dialect)
columns, err := e.getColumnNames(ctx, tempQuery)
if err != nil {
return fmt.Errorf("failed to get column names: %w", err)
}
stmt.TargetList = rewriteTargetList(stmt.TargetList, columns)
}
return nil
}
// expandSelectStmtInner expands nested structures without re-processing the target list
func (e *Expander) expandSelectStmtInner(ctx context.Context, stmt *ast.SelectStmt) error {
// Expand subqueries in FROM clause
if stmt.FromClause != nil {
for _, fromItem := range stmt.FromClause.Items {
if err := e.expandFromClause(ctx, fromItem); err != nil {
return err
}
}
}
return nil
}
// getCTEColumnNames gets the column names for a CTE by constructing a query with proper context
func (e *Expander) getCTEColumnNames(ctx context.Context, stmt *ast.SelectStmt, targetCTE *ast.CommonTableExpr) ([]string, error) {
// Build a temporary query: WITH <all CTEs up to and including target> SELECT * FROM <targetCTE>
var ctesToInclude []ast.Node
for _, cteNode := range stmt.WithClause.Ctes.Items {
ctesToInclude = append(ctesToInclude, cteNode)
cte, ok := cteNode.(*ast.CommonTableExpr)
if ok && cte.Ctename != nil && targetCTE.Ctename != nil && *cte.Ctename == *targetCTE.Ctename {
break
}
}
// Create a SELECT * FROM <ctename> with the relevant CTEs
cteName := ""
if targetCTE.Ctename != nil {
cteName = *targetCTE.Ctename
}
tempStmt := &ast.SelectStmt{
WithClause: &ast.WithClause{
Ctes: &ast.List{Items: ctesToInclude},
Recursive: stmt.WithClause.Recursive,
},
TargetList: &ast.List{
Items: []ast.Node{
&ast.ResTarget{
Val: &ast.ColumnRef{
Fields: &ast.List{
Items: []ast.Node{&ast.A_Star{}},
},
},
},
},
},
FromClause: &ast.List{
Items: []ast.Node{
&ast.RangeVar{
Relname: &cteName,
},
},
},
}
tempRaw := &ast.RawStmt{Stmt: tempStmt}
tempQuery := ast.Format(tempRaw, e.dialect)
return e.getColumnNames(ctx, tempQuery)
}
// expandInsertStmt expands * in an INSERT statement's RETURNING clause
func (e *Expander) expandInsertStmt(ctx context.Context, stmt *ast.InsertStmt) error {
// Expand CTEs first
if stmt.WithClause != nil && stmt.WithClause.Ctes != nil {
for _, cte := range stmt.WithClause.Ctes.Items {
if err := e.expandNode(ctx, cte); err != nil {
return err
}
}
}
// Expand the SELECT part if present
if stmt.SelectStmt != nil {
if err := e.expandNode(ctx, stmt.SelectStmt); err != nil {
return err
}
}
// Expand RETURNING clause
if hasStarInList(stmt.ReturningList) {
tempRaw := &ast.RawStmt{Stmt: stmt}
tempQuery := ast.Format(tempRaw, e.dialect)
columns, err := e.getColumnNames(ctx, tempQuery)
if err != nil {
return fmt.Errorf("failed to get column names: %w", err)
}
stmt.ReturningList = rewriteTargetList(stmt.ReturningList, columns)
}
return nil
}
// expandUpdateStmt expands * in an UPDATE statement's RETURNING clause
func (e *Expander) expandUpdateStmt(ctx context.Context, stmt *ast.UpdateStmt) error {
// Expand CTEs first
if stmt.WithClause != nil && stmt.WithClause.Ctes != nil {
for _, cte := range stmt.WithClause.Ctes.Items {
if err := e.expandNode(ctx, cte); err != nil {
return err
}
}
}
// Expand RETURNING clause
if hasStarInList(stmt.ReturningList) {
tempRaw := &ast.RawStmt{Stmt: stmt}
tempQuery := ast.Format(tempRaw, e.dialect)
columns, err := e.getColumnNames(ctx, tempQuery)
if err != nil {
return fmt.Errorf("failed to get column names: %w", err)
}
stmt.ReturningList = rewriteTargetList(stmt.ReturningList, columns)
}
return nil
}
// expandDeleteStmt expands * in a DELETE statement's RETURNING clause
func (e *Expander) expandDeleteStmt(ctx context.Context, stmt *ast.DeleteStmt) error {
// Expand CTEs first
if stmt.WithClause != nil && stmt.WithClause.Ctes != nil {
for _, cte := range stmt.WithClause.Ctes.Items {
if err := e.expandNode(ctx, cte); err != nil {
return err
}
}
}
// Expand RETURNING clause
if hasStarInList(stmt.ReturningList) {
tempRaw := &ast.RawStmt{Stmt: stmt}
tempQuery := ast.Format(tempRaw, e.dialect)
columns, err := e.getColumnNames(ctx, tempQuery)
if err != nil {
return fmt.Errorf("failed to get column names: %w", err)
}
stmt.ReturningList = rewriteTargetList(stmt.ReturningList, columns)
}
return nil
}
// expandFromClause expands * in subqueries within FROM clause
func (e *Expander) expandFromClause(ctx context.Context, node ast.Node) error {
if node == nil {
return nil
}
switch n := node.(type) {
case *ast.RangeSubselect:
if n.Subquery != nil {
return e.expandNode(ctx, n.Subquery)
}
case *ast.JoinExpr:
if err := e.expandFromClause(ctx, n.Larg); err != nil {
return err
}
if err := e.expandFromClause(ctx, n.Rarg); err != nil {
return err
}
}
return nil
}
// hasStarAnywhere checks if there's a * anywhere in the statement using astutils.Search
func hasStarAnywhere(node ast.Node) bool {
if node == nil {
return false
}
// Use astutils.Search to find any A_Star node in the AST
stars := astutils.Search(node, func(n ast.Node) bool {
_, ok := n.(*ast.A_Star)
return ok
})
return len(stars.Items) > 0
}
// hasStarInList checks if a target list contains a * expression using astutils.Search
func hasStarInList(targets *ast.List) bool {
if targets == nil {
return false
}
// Use astutils.Search to find any A_Star node in the target list
stars := astutils.Search(targets, func(n ast.Node) bool {
_, ok := n.(*ast.A_Star)
return ok
})
return len(stars.Items) > 0
}
// getColumnNames prepares the query and returns the column names from the result
func (e *Expander) getColumnNames(ctx context.Context, query string) ([]string, error) {
return e.colGetter.GetColumnNames(ctx, query)
}
// countStarsInList counts the number of * expressions in a target list
func countStarsInList(targets *ast.List) int {
if targets == nil {
return 0
}
count := 0
for _, target := range targets.Items {
resTarget, ok := target.(*ast.ResTarget)
if !ok {
continue
}
if resTarget.Val == nil {
continue
}
colRef, ok := resTarget.Val.(*ast.ColumnRef)
if !ok {
continue
}
if colRef.Fields == nil {
continue
}
for _, field := range colRef.Fields.Items {
if _, ok := field.(*ast.A_Star); ok {
count++
break
}
}
}
return count
}
// countNonStarsInList counts the number of non-* expressions in a target list
func countNonStarsInList(targets *ast.List) int {
if targets == nil {
return 0
}
count := 0
for _, target := range targets.Items {
resTarget, ok := target.(*ast.ResTarget)
if !ok {
count++
continue
}
if resTarget.Val == nil {
count++
continue
}
colRef, ok := resTarget.Val.(*ast.ColumnRef)
if !ok {
count++
continue
}
if colRef.Fields == nil {
count++
continue
}
isStar := false
for _, field := range colRef.Fields.Items {
if _, ok := field.(*ast.A_Star); ok {
isStar = true
break
}
}
if !isStar {
count++
}
}
return count
}
// rewriteTargetList replaces * in a target list with explicit column references
func rewriteTargetList(targets *ast.List, columns []string) *ast.List {
if targets == nil {
return nil
}
starCount := countStarsInList(targets)
nonStarCount := countNonStarsInList(targets)
// Calculate how many columns each * expands to
// Total columns = (columns per star * number of stars) + non-star columns
// So: columns per star = (total - non-star) / stars
columnsPerStar := 0
if starCount > 0 {
columnsPerStar = (len(columns) - nonStarCount) / starCount
}
newItems := make([]ast.Node, 0, len(columns))
colIndex := 0
for _, target := range targets.Items {
resTarget, ok := target.(*ast.ResTarget)
if !ok {
newItems = append(newItems, target)
colIndex++
continue
}
if resTarget.Val == nil {
newItems = append(newItems, target)
colIndex++
continue
}
colRef, ok := resTarget.Val.(*ast.ColumnRef)
if !ok {
newItems = append(newItems, target)
colIndex++
continue
}
if colRef.Fields == nil {
newItems = append(newItems, target)
colIndex++
continue
}
// Check if this is a * (with or without table qualifier)
// and extract any table prefix
isStar := false
var tablePrefix []string
for _, field := range colRef.Fields.Items {
if _, ok := field.(*ast.A_Star); ok {
isStar = true
break
}
// Collect prefix parts (schema, table name)
if str, ok := field.(*ast.String); ok {
tablePrefix = append(tablePrefix, str.Str)
}
}
if !isStar {
newItems = append(newItems, target)
colIndex++
continue
}
// Replace * with explicit column references
for i := 0; i < columnsPerStar && colIndex < len(columns); i++ {
newItems = append(newItems, makeColumnTargetWithPrefix(columns[colIndex], tablePrefix))
colIndex++
}
}
return &ast.List{Items: newItems}
}
// makeColumnTargetWithPrefix creates a ResTarget node for a column reference with optional table prefix
func makeColumnTargetWithPrefix(colName string, prefix []string) ast.Node {
fields := make([]ast.Node, 0, len(prefix)+1)
// Add prefix parts (schema, table name)
for _, p := range prefix {
fields = append(fields, &ast.String{Str: p})
}
// Add column name
fields = append(fields, &ast.String{Str: colName})
return &ast.ResTarget{
Val: &ast.ColumnRef{
Fields: &ast.List{Items: fields},
},
}
}