aboutsummaryrefslogtreecommitdiff
path: root/weed/query/engine/query_parsing_test.go
blob: ffeaadbc5f52a0dbfc364a85814b269cd6f4e9d6 (plain)
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package engine

import (
	"testing"
)

func TestParseSQL_COUNT_Functions(t *testing.T) {
	tests := []struct {
		name     string
		sql      string
		wantErr  bool
		validate func(t *testing.T, stmt Statement)
	}{
		{
			name:    "COUNT(*) basic",
			sql:     "SELECT COUNT(*) FROM test_table",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt, ok := stmt.(*SelectStatement)
				if !ok {
					t.Fatalf("Expected *SelectStatement, got %T", stmt)
				}

				if len(selectStmt.SelectExprs) != 1 {
					t.Fatalf("Expected 1 select expression, got %d", len(selectStmt.SelectExprs))
				}

				aliasedExpr, ok := selectStmt.SelectExprs[0].(*AliasedExpr)
				if !ok {
					t.Fatalf("Expected *AliasedExpr, got %T", selectStmt.SelectExprs[0])
				}

				funcExpr, ok := aliasedExpr.Expr.(*FuncExpr)
				if !ok {
					t.Fatalf("Expected *FuncExpr, got %T", aliasedExpr.Expr)
				}

				if funcExpr.Name.String() != "COUNT" {
					t.Errorf("Expected function name 'COUNT', got '%s'", funcExpr.Name.String())
				}

				if len(funcExpr.Exprs) != 1 {
					t.Fatalf("Expected 1 function argument, got %d", len(funcExpr.Exprs))
				}

				starExpr, ok := funcExpr.Exprs[0].(*StarExpr)
				if !ok {
					t.Errorf("Expected *StarExpr argument, got %T", funcExpr.Exprs[0])
				}
				_ = starExpr // Use the variable to avoid unused variable error
			},
		},
		{
			name:    "COUNT(column_name)",
			sql:     "SELECT COUNT(user_id) FROM users",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt, ok := stmt.(*SelectStatement)
				if !ok {
					t.Fatalf("Expected *SelectStatement, got %T", stmt)
				}

				aliasedExpr := selectStmt.SelectExprs[0].(*AliasedExpr)
				funcExpr := aliasedExpr.Expr.(*FuncExpr)

				if funcExpr.Name.String() != "COUNT" {
					t.Errorf("Expected function name 'COUNT', got '%s'", funcExpr.Name.String())
				}

				if len(funcExpr.Exprs) != 1 {
					t.Fatalf("Expected 1 function argument, got %d", len(funcExpr.Exprs))
				}

				argExpr, ok := funcExpr.Exprs[0].(*AliasedExpr)
				if !ok {
					t.Errorf("Expected *AliasedExpr argument, got %T", funcExpr.Exprs[0])
				}

				colName, ok := argExpr.Expr.(*ColName)
				if !ok {
					t.Errorf("Expected *ColName, got %T", argExpr.Expr)
				}

				if colName.Name.String() != "user_id" {
					t.Errorf("Expected column name 'user_id', got '%s'", colName.Name.String())
				}
			},
		},
		{
			name:    "Multiple aggregate functions",
			sql:     "SELECT COUNT(*), SUM(amount), AVG(score) FROM transactions",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt, ok := stmt.(*SelectStatement)
				if !ok {
					t.Fatalf("Expected *SelectStatement, got %T", stmt)
				}

				if len(selectStmt.SelectExprs) != 3 {
					t.Fatalf("Expected 3 select expressions, got %d", len(selectStmt.SelectExprs))
				}

				// Verify COUNT(*)
				countExpr := selectStmt.SelectExprs[0].(*AliasedExpr)
				countFunc := countExpr.Expr.(*FuncExpr)
				if countFunc.Name.String() != "COUNT" {
					t.Errorf("Expected first function to be COUNT, got %s", countFunc.Name.String())
				}

				// Verify SUM(amount)
				sumExpr := selectStmt.SelectExprs[1].(*AliasedExpr)
				sumFunc := sumExpr.Expr.(*FuncExpr)
				if sumFunc.Name.String() != "SUM" {
					t.Errorf("Expected second function to be SUM, got %s", sumFunc.Name.String())
				}

				// Verify AVG(score)
				avgExpr := selectStmt.SelectExprs[2].(*AliasedExpr)
				avgFunc := avgExpr.Expr.(*FuncExpr)
				if avgFunc.Name.String() != "AVG" {
					t.Errorf("Expected third function to be AVG, got %s", avgFunc.Name.String())
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stmt, err := ParseSQL(tt.sql)

			if tt.wantErr {
				if err == nil {
					t.Errorf("Expected error, but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if tt.validate != nil {
				tt.validate(t, stmt)
			}
		})
	}
}

func TestParseSQL_SELECT_Expressions(t *testing.T) {
	tests := []struct {
		name     string
		sql      string
		wantErr  bool
		validate func(t *testing.T, stmt Statement)
	}{
		{
			name:    "SELECT * FROM table",
			sql:     "SELECT * FROM users",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if len(selectStmt.SelectExprs) != 1 {
					t.Fatalf("Expected 1 select expression, got %d", len(selectStmt.SelectExprs))
				}

				_, ok := selectStmt.SelectExprs[0].(*StarExpr)
				if !ok {
					t.Errorf("Expected *StarExpr, got %T", selectStmt.SelectExprs[0])
				}
			},
		},
		{
			name:    "SELECT column FROM table",
			sql:     "SELECT user_id FROM users",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if len(selectStmt.SelectExprs) != 1 {
					t.Fatalf("Expected 1 select expression, got %d", len(selectStmt.SelectExprs))
				}

				aliasedExpr, ok := selectStmt.SelectExprs[0].(*AliasedExpr)
				if !ok {
					t.Fatalf("Expected *AliasedExpr, got %T", selectStmt.SelectExprs[0])
				}

				colName, ok := aliasedExpr.Expr.(*ColName)
				if !ok {
					t.Fatalf("Expected *ColName, got %T", aliasedExpr.Expr)
				}

				if colName.Name.String() != "user_id" {
					t.Errorf("Expected column name 'user_id', got '%s'", colName.Name.String())
				}
			},
		},
		{
			name:    "SELECT multiple columns",
			sql:     "SELECT user_id, name, email FROM users",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if len(selectStmt.SelectExprs) != 3 {
					t.Fatalf("Expected 3 select expressions, got %d", len(selectStmt.SelectExprs))
				}

				expectedColumns := []string{"user_id", "name", "email"}
				for i, expected := range expectedColumns {
					aliasedExpr := selectStmt.SelectExprs[i].(*AliasedExpr)
					colName := aliasedExpr.Expr.(*ColName)
					if colName.Name.String() != expected {
						t.Errorf("Expected column %d to be '%s', got '%s'", i, expected, colName.Name.String())
					}
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stmt, err := ParseSQL(tt.sql)

			if tt.wantErr {
				if err == nil {
					t.Errorf("Expected error, but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if tt.validate != nil {
				tt.validate(t, stmt)
			}
		})
	}
}

func TestParseSQL_WHERE_Clauses(t *testing.T) {
	tests := []struct {
		name     string
		sql      string
		wantErr  bool
		validate func(t *testing.T, stmt Statement)
	}{
		{
			name:    "WHERE with simple comparison",
			sql:     "SELECT * FROM users WHERE age > 18",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Where == nil {
					t.Fatal("Expected WHERE clause, got nil")
				}

				// Just verify we have a WHERE clause with an expression
				if selectStmt.Where.Expr == nil {
					t.Error("Expected WHERE expression, got nil")
				}
			},
		},
		{
			name:    "WHERE with AND condition",
			sql:     "SELECT * FROM users WHERE age > 18 AND status = 'active'",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Where == nil {
					t.Fatal("Expected WHERE clause, got nil")
				}

				// Verify we have an AND expression
				andExpr, ok := selectStmt.Where.Expr.(*AndExpr)
				if !ok {
					t.Errorf("Expected *AndExpr, got %T", selectStmt.Where.Expr)
				}
				_ = andExpr // Use variable to avoid unused error
			},
		},
		{
			name:    "WHERE with OR condition",
			sql:     "SELECT * FROM users WHERE age < 18 OR age > 65",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Where == nil {
					t.Fatal("Expected WHERE clause, got nil")
				}

				// Verify we have an OR expression
				orExpr, ok := selectStmt.Where.Expr.(*OrExpr)
				if !ok {
					t.Errorf("Expected *OrExpr, got %T", selectStmt.Where.Expr)
				}
				_ = orExpr // Use variable to avoid unused error
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stmt, err := ParseSQL(tt.sql)

			if tt.wantErr {
				if err == nil {
					t.Errorf("Expected error, but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if tt.validate != nil {
				tt.validate(t, stmt)
			}
		})
	}
}

func TestParseSQL_LIMIT_Clauses(t *testing.T) {
	tests := []struct {
		name     string
		sql      string
		wantErr  bool
		validate func(t *testing.T, stmt Statement)
	}{
		{
			name:    "LIMIT with number",
			sql:     "SELECT * FROM users LIMIT 10",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Limit == nil {
					t.Fatal("Expected LIMIT clause, got nil")
				}

				if selectStmt.Limit.Rowcount == nil {
					t.Error("Expected LIMIT rowcount, got nil")
				}

				// Verify no OFFSET is set
				if selectStmt.Limit.Offset != nil {
					t.Error("Expected OFFSET to be nil for LIMIT-only query")
				}

				sqlVal, ok := selectStmt.Limit.Rowcount.(*SQLVal)
				if !ok {
					t.Errorf("Expected *SQLVal, got %T", selectStmt.Limit.Rowcount)
				}

				if sqlVal.Type != IntVal {
					t.Errorf("Expected IntVal type, got %d", sqlVal.Type)
				}

				if string(sqlVal.Val) != "10" {
					t.Errorf("Expected limit value '10', got '%s'", string(sqlVal.Val))
				}
			},
		},
		{
			name:    "LIMIT with OFFSET",
			sql:     "SELECT * FROM users LIMIT 10 OFFSET 5",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Limit == nil {
					t.Fatal("Expected LIMIT clause, got nil")
				}

				// Verify LIMIT value
				if selectStmt.Limit.Rowcount == nil {
					t.Error("Expected LIMIT rowcount, got nil")
				}

				limitVal, ok := selectStmt.Limit.Rowcount.(*SQLVal)
				if !ok {
					t.Errorf("Expected *SQLVal for LIMIT, got %T", selectStmt.Limit.Rowcount)
				}

				if limitVal.Type != IntVal {
					t.Errorf("Expected IntVal type for LIMIT, got %d", limitVal.Type)
				}

				if string(limitVal.Val) != "10" {
					t.Errorf("Expected limit value '10', got '%s'", string(limitVal.Val))
				}

				// Verify OFFSET value
				if selectStmt.Limit.Offset == nil {
					t.Fatal("Expected OFFSET clause, got nil")
				}

				offsetVal, ok := selectStmt.Limit.Offset.(*SQLVal)
				if !ok {
					t.Errorf("Expected *SQLVal for OFFSET, got %T", selectStmt.Limit.Offset)
				}

				if offsetVal.Type != IntVal {
					t.Errorf("Expected IntVal type for OFFSET, got %d", offsetVal.Type)
				}

				if string(offsetVal.Val) != "5" {
					t.Errorf("Expected offset value '5', got '%s'", string(offsetVal.Val))
				}
			},
		},
		{
			name:    "LIMIT with OFFSET zero",
			sql:     "SELECT * FROM users LIMIT 5 OFFSET 0",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Limit == nil {
					t.Fatal("Expected LIMIT clause, got nil")
				}

				// Verify OFFSET is 0
				if selectStmt.Limit.Offset == nil {
					t.Fatal("Expected OFFSET clause, got nil")
				}

				offsetVal, ok := selectStmt.Limit.Offset.(*SQLVal)
				if !ok {
					t.Errorf("Expected *SQLVal for OFFSET, got %T", selectStmt.Limit.Offset)
				}

				if string(offsetVal.Val) != "0" {
					t.Errorf("Expected offset value '0', got '%s'", string(offsetVal.Val))
				}
			},
		},
		{
			name:    "LIMIT with large OFFSET",
			sql:     "SELECT * FROM users LIMIT 100 OFFSET 1000",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				selectStmt := stmt.(*SelectStatement)
				if selectStmt.Limit == nil {
					t.Fatal("Expected LIMIT clause, got nil")
				}

				// Verify large OFFSET value
				offsetVal, ok := selectStmt.Limit.Offset.(*SQLVal)
				if !ok {
					t.Errorf("Expected *SQLVal for OFFSET, got %T", selectStmt.Limit.Offset)
				}

				if string(offsetVal.Val) != "1000" {
					t.Errorf("Expected offset value '1000', got '%s'", string(offsetVal.Val))
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stmt, err := ParseSQL(tt.sql)

			if tt.wantErr {
				if err == nil {
					t.Errorf("Expected error, but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if tt.validate != nil {
				tt.validate(t, stmt)
			}
		})
	}
}

func TestParseSQL_SHOW_Statements(t *testing.T) {
	tests := []struct {
		name     string
		sql      string
		wantErr  bool
		validate func(t *testing.T, stmt Statement)
	}{
		{
			name:    "SHOW DATABASES",
			sql:     "SHOW DATABASES",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				showStmt, ok := stmt.(*ShowStatement)
				if !ok {
					t.Fatalf("Expected *ShowStatement, got %T", stmt)
				}

				if showStmt.Type != "databases" {
					t.Errorf("Expected type 'databases', got '%s'", showStmt.Type)
				}
			},
		},
		{
			name:    "SHOW TABLES",
			sql:     "SHOW TABLES",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				showStmt, ok := stmt.(*ShowStatement)
				if !ok {
					t.Fatalf("Expected *ShowStatement, got %T", stmt)
				}

				if showStmt.Type != "tables" {
					t.Errorf("Expected type 'tables', got '%s'", showStmt.Type)
				}
			},
		},
		{
			name:    "SHOW TABLES FROM database",
			sql:     "SHOW TABLES FROM \"test_db\"",
			wantErr: false,
			validate: func(t *testing.T, stmt Statement) {
				showStmt, ok := stmt.(*ShowStatement)
				if !ok {
					t.Fatalf("Expected *ShowStatement, got %T", stmt)
				}

				if showStmt.Type != "tables" {
					t.Errorf("Expected type 'tables', got '%s'", showStmt.Type)
				}

				if showStmt.Schema != "test_db" {
					t.Errorf("Expected schema 'test_db', got '%s'", showStmt.Schema)
				}
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			stmt, err := ParseSQL(tt.sql)

			if tt.wantErr {
				if err == nil {
					t.Errorf("Expected error, but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("Unexpected error: %v", err)
				return
			}

			if tt.validate != nil {
				tt.validate(t, stmt)
			}
		})
	}
}