-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalysis.go
More file actions
465 lines (403 loc) · 13.9 KB
/
analysis.go
File metadata and controls
465 lines (403 loc) · 13.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
// Package cel2sql provides query analysis and index recommendations
package cel2sql
import (
"fmt"
"log/slog"
"time"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/operators"
"github.com/google/cel-go/common/overloads"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"github.com/spandigital/cel2sql/v3/dialect"
"github.com/spandigital/cel2sql/v3/dialect/postgres"
)
// Index type constants for recommendations (kept for backward compatibility).
const (
// IndexTypeBTree represents a B-tree index for efficient range queries and equality checks
IndexTypeBTree = "BTREE"
// IndexTypeGIN represents a GIN (Generalized Inverted Index) for JSON, arrays, and full-text search
IndexTypeGIN = "GIN"
// IndexTypeGIST represents a GIST (Generalized Search Tree) index
IndexTypeGIST = "GIST"
)
// IndexRecommendation represents a database index recommendation based on CEL query patterns.
// It provides actionable guidance for optimizing query performance through appropriate indexing strategies.
type IndexRecommendation struct {
// Column is the database column that should be indexed
Column string
// IndexType specifies the index type (e.g., "BTREE", "GIN", "ART", "CLUSTERING")
IndexType string
// Expression is the complete DDL statement that can be executed directly
Expression string
// Reason explains why this index is recommended and what query patterns it optimizes
Reason string
}
// analysisConverter extends converter with index pattern tracking capabilities
type analysisConverter struct {
*converter
recommendations map[string]*IndexRecommendation // Key: column name, Value: recommendation
visitedColumns map[string]bool // Track which columns have been accessed
advisor dialect.IndexAdvisor // Dialect-specific index advisor
}
// AnalyzeQuery converts a CEL AST to SQL and provides dialect-specific index recommendations.
// It analyzes the query patterns to suggest indexes that would optimize performance.
//
// The function detects patterns that benefit from specific index types:
// - JSON/JSONB path operations → GIN indexes (PostgreSQL), functional indexes (MySQL), search indexes (BigQuery)
// - Array operations → GIN indexes (PostgreSQL), ART indexes (DuckDB)
// - Regex matching → GIN indexes with pg_trgm (PostgreSQL), FULLTEXT indexes (MySQL)
// - Comparison operations → B-tree indexes (PostgreSQL/MySQL/SQLite), ART (DuckDB), clustering (BigQuery)
//
// Use WithDialect() to get dialect-specific index recommendations. Defaults to PostgreSQL.
//
// Example:
//
// sql, recommendations, err := cel2sql.AnalyzeQuery(ast,
// cel2sql.WithSchemas(schemas),
// cel2sql.WithDialect(mysql.New()))
// if err != nil {
// return err
// }
//
// // Use the SQL query
// rows, err := db.Query("SELECT * FROM table WHERE " + sql)
//
// // Apply index recommendations
// for _, rec := range recommendations {
// fmt.Printf("Recommendation: %s\n", rec.Reason)
// fmt.Printf("Execute: %s\n\n", rec.Expression)
// }
func AnalyzeQuery(ast *cel.Ast, opts ...ConvertOption) (string, []IndexRecommendation, error) {
start := time.Now()
// Parse options
options := &convertOptions{
logger: slog.New(slog.DiscardHandler), // Default: no-op logger with zero overhead
maxDepth: defaultMaxRecursionDepth,
maxOutputLen: defaultMaxSQLOutputLength,
}
for _, opt := range opts {
opt(options)
}
// Default to PostgreSQL dialect if none specified
d := options.dialect
if d == nil {
d = postgres.New()
}
// Get the IndexAdvisor for the dialect (all built-in dialects implement it)
advisor, hasAdvisor := dialect.GetIndexAdvisor(d)
if !hasAdvisor {
// Fallback: use PostgreSQL advisor for backward compatibility
advisor = postgres.New()
}
// Convert AST to CheckedExpr
checkedExpr, err := cel.AstToCheckedExpr(ast)
if err != nil {
return "", nil, fmt.Errorf("failed to convert AST to CheckedExpr: %w", err)
}
// Pass 1: Analyze the AST to collect index recommendations
baseConverter := &converter{
typeMap: checkedExpr.TypeMap,
schemas: options.schemas,
ctx: options.ctx,
logger: options.logger,
maxDepth: options.maxDepth,
maxOutputLen: options.maxOutputLen,
}
analyzer := &analysisConverter{
converter: baseConverter,
recommendations: make(map[string]*IndexRecommendation),
visitedColumns: make(map[string]bool),
advisor: advisor,
}
// Analyze the expression tree to collect index patterns
if err := analyzer.analyzeVisit(checkedExpr.Expr); err != nil {
return "", nil, fmt.Errorf("analysis failed: %w", err)
}
// Pass 2: Generate SQL using the standard conversion
sql, err := Convert(ast, opts...)
if err != nil {
return "", nil, fmt.Errorf("SQL generation failed: %w", err)
}
duration := time.Since(start)
if options.logger != nil {
options.logger.Debug("query analysis completed",
"sql", sql,
"dialect", d.Name(),
"recommendation_count", len(analyzer.recommendations),
"duration", duration)
}
// Convert recommendations map to slice
recommendations := make([]IndexRecommendation, 0, len(analyzer.recommendations))
for _, rec := range analyzer.recommendations {
recommendations = append(recommendations, *rec)
}
return sql, recommendations, nil
}
// analyzeVisit recursively walks the AST to track index-worthy patterns
func (a *analysisConverter) analyzeVisit(expr *exprpb.Expr) error {
if expr == nil {
return nil
}
// Track patterns based on expression type
switch expr.ExprKind.(type) {
case *exprpb.Expr_CallExpr:
if err := a.analyzeCall(expr); err != nil {
return err
}
// Recursively analyze call arguments
c := expr.GetCallExpr()
if c.GetTarget() != nil {
if err := a.analyzeVisit(c.GetTarget()); err != nil {
return err
}
}
for _, arg := range c.GetArgs() {
if err := a.analyzeVisit(arg); err != nil {
return err
}
}
case *exprpb.Expr_ComprehensionExpr:
if err := a.analyzeComprehension(expr); err != nil {
return err
}
// Recursively analyze comprehension parts
comp := expr.GetComprehensionExpr()
if err := a.analyzeVisit(comp.GetIterRange()); err != nil {
return err
}
if err := a.analyzeVisit(comp.GetAccuInit()); err != nil {
return err
}
if err := a.analyzeVisit(comp.GetLoopCondition()); err != nil {
return err
}
if err := a.analyzeVisit(comp.GetLoopStep()); err != nil {
return err
}
if err := a.analyzeVisit(comp.GetResult()); err != nil {
return err
}
case *exprpb.Expr_SelectExpr:
if err := a.analyzeSelect(expr); err != nil {
return err
}
// Recursively analyze operand
sel := expr.GetSelectExpr()
if err := a.analyzeVisit(sel.GetOperand()); err != nil {
return err
}
case *exprpb.Expr_ListExpr:
// Recursively analyze list elements
list := expr.GetListExpr()
for _, elem := range list.GetElements() {
if err := a.analyzeVisit(elem); err != nil {
return err
}
}
case *exprpb.Expr_StructExpr:
// Recursively analyze struct entries
st := expr.GetStructExpr()
for _, entry := range st.GetEntries() {
if entry.GetMapKey() != nil {
if err := a.analyzeVisit(entry.GetMapKey()); err != nil {
return err
}
}
if err := a.analyzeVisit(entry.GetValue()); err != nil {
return err
}
}
}
return nil
}
// analyzeCall analyzes function calls for index recommendations
func (a *analysisConverter) analyzeCall(expr *exprpb.Expr) error {
c := expr.GetCallExpr()
fun := c.GetFunction()
switch fun {
case overloads.Matches:
// Regex matching benefits from dialect-specific indexes
a.recommendRegexIndex(expr)
case operators.Equals, operators.NotEquals,
operators.Greater, operators.GreaterEquals,
operators.Less, operators.LessEquals:
// Comparison operations benefit from indexes
a.recommendComparisonIndex(expr)
case operators.In:
// IN operations on arrays benefit from indexes
a.recommendArrayIndex(expr)
}
return nil
}
// analyzeComprehension analyzes comprehension expressions for index recommendations
func (a *analysisConverter) analyzeComprehension(expr *exprpb.Expr) error {
comprehension := expr.GetComprehensionExpr()
if comprehension == nil {
return nil
}
iterRange := comprehension.GetIterRange()
// Check if this is a JSON array comprehension
if a.isJSONArrayField(iterRange) {
if column := a.extractColumnName(iterRange); column != "" {
a.recommendForPattern(column, dialect.PatternJSONArrayComprehension)
}
} else {
// Regular array comprehension
if column := a.extractColumnName(iterRange); column != "" {
a.recommendForPattern(column, dialect.PatternArrayComprehension)
}
}
return nil
}
// analyzeSelect analyzes field selection for index recommendations
func (a *analysisConverter) analyzeSelect(expr *exprpb.Expr) error {
sel := expr.GetSelectExpr()
fieldName := sel.GetField()
operand := sel.GetOperand()
// Check if the operand is a JSON field (e.g., person.metadata in person.metadata.name)
if operandSel := operand.GetSelectExpr(); operandSel != nil {
operandField := operandSel.GetField()
if identExpr := operandSel.GetOperand().GetIdentExpr(); identExpr != nil {
tableName := identExpr.GetName()
// Check if the parent field is JSON
if a.isFieldJSON(tableName, operandField) {
column := tableName + "." + operandField
a.recommendForPattern(column, dialect.PatternJSONAccess)
}
}
}
// Also check if this field itself is JSON
if identExpr := operand.GetIdentExpr(); identExpr != nil {
tableName := identExpr.GetName()
if a.isFieldJSON(tableName, fieldName) {
column := tableName + "." + fieldName
a.recommendForPattern(column, dialect.PatternJSONAccess)
}
// Track column access for potential indexes
fullColumn := tableName + "." + fieldName
a.visitedColumns[fullColumn] = true
}
return nil
}
// recommendRegexIndex recommends an index for regex operations
func (a *analysisConverter) recommendRegexIndex(expr *exprpb.Expr) {
c := expr.GetCallExpr()
target := c.GetTarget()
if target == nil && len(c.GetArgs()) >= 2 {
target = c.GetArgs()[0]
}
if target != nil {
if column := a.extractColumnName(target); column != "" {
a.recommendForPattern(column, dialect.PatternRegexMatch)
}
}
}
// recommendComparisonIndex recommends an index for comparison operations
func (a *analysisConverter) recommendComparisonIndex(expr *exprpb.Expr) {
c := expr.GetCallExpr()
args := c.GetArgs()
if len(args) < 2 {
return
}
lhs := args[0]
// Extract column from left-hand side
if column := a.extractColumnName(lhs); column != "" {
// Check if this is a JSON field (skip comparison recommendation for JSON)
if !a.isJSONField(lhs) {
a.recommendForPattern(column, dialect.PatternComparison)
}
}
}
// recommendArrayIndex recommends an index for array containment operations
func (a *analysisConverter) recommendArrayIndex(expr *exprpb.Expr) {
c := expr.GetCallExpr()
args := c.GetArgs()
if len(args) < 2 {
return
}
rhs := args[1]
// Check if the right-hand side is an array field
if a.isFieldArray(a.extractTableName(rhs), a.extractFieldName(rhs)) {
if column := a.extractColumnName(rhs); column != "" {
a.recommendForPattern(column, dialect.PatternArrayMembership)
}
}
}
// recommendForPattern asks the dialect's IndexAdvisor for a recommendation and stores it.
func (a *analysisConverter) recommendForPattern(column string, pattern dialect.PatternType) {
rec := a.advisor.RecommendIndex(dialect.IndexPattern{
Column: column,
Pattern: pattern,
})
if rec == nil {
return
}
a.addRecommendation(column, &IndexRecommendation{
Column: rec.Column,
IndexType: rec.IndexType,
Expression: rec.Expression,
Reason: rec.Reason,
})
}
// extractColumnName extracts the full column name (table.column) from an expression
func (a *analysisConverter) extractColumnName(expr *exprpb.Expr) string {
if sel := expr.GetSelectExpr(); sel != nil {
fieldName := sel.GetField()
if identExpr := sel.GetOperand().GetIdentExpr(); identExpr != nil {
tableName := identExpr.GetName()
return tableName + "." + fieldName
}
}
if identExpr := expr.GetIdentExpr(); identExpr != nil {
return identExpr.GetName()
}
return ""
}
// extractTableName extracts the table name from an expression
func (a *analysisConverter) extractTableName(expr *exprpb.Expr) string {
if sel := expr.GetSelectExpr(); sel != nil {
if identExpr := sel.GetOperand().GetIdentExpr(); identExpr != nil {
return identExpr.GetName()
}
}
return ""
}
// extractFieldName extracts the field name from an expression
func (a *analysisConverter) extractFieldName(expr *exprpb.Expr) string {
if sel := expr.GetSelectExpr(); sel != nil {
return sel.GetField()
}
return ""
}
// isJSONField checks if an expression refers to a JSON/JSONB field
func (a *analysisConverter) isJSONField(expr *exprpb.Expr) bool {
if sel := expr.GetSelectExpr(); sel != nil {
operand := sel.GetOperand()
fieldName := sel.GetField()
if identExpr := operand.GetIdentExpr(); identExpr != nil {
tableName := identExpr.GetName()
return a.isFieldJSON(tableName, fieldName)
}
}
return false
}
// addRecommendation adds or updates an index recommendation.
// When a more specialized recommendation exists for a column, it takes priority.
func (a *analysisConverter) addRecommendation(column string, rec *IndexRecommendation) {
// Only add if we don't already have a recommendation for this column
// or if the new recommendation is more specific
existing, exists := a.recommendations[column]
if !exists {
a.recommendations[column] = rec
return
}
// More specialized index types take priority over basic B-tree/comparison indexes
if isBasicIndexType(existing.IndexType) && !isBasicIndexType(rec.IndexType) {
a.recommendations[column] = rec
}
}
// isBasicIndexType returns true if the index type is a basic comparison index
// that should be upgraded when a more specialized recommendation is available.
func isBasicIndexType(indexType string) bool {
return indexType == IndexTypeBTree || indexType == "ART" || indexType == "CLUSTERING"
}