-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconn_test.go
More file actions
3992 lines (3702 loc) · 117 KB
/
conn_test.go
File metadata and controls
3992 lines (3702 loc) · 117 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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package server
import (
"bufio"
"bytes"
"context"
"database/sql"
"encoding/binary"
"encoding/csv"
"errors"
"fmt"
"io"
"math"
"net"
"slices"
"strings"
"testing"
pg_query "github.com/pganalyze/pg_query_go/v6"
"github.com/posthog/duckgres/duckdbservice/arrowmap"
"github.com/posthog/duckgres/server/wire"
)
func TestIsEmptyQuery(t *testing.T) {
tests := []struct {
name string
query string
expected bool
}{
{"empty string", "", true},
{"single semicolon", ";", true},
{"multiple semicolons", ";;;", true},
{"semicolons with spaces", "; ; ;", true},
{"semicolons with tabs", ";\t;\t;", true},
{"semicolons with newlines", ";\n;\n;", true},
{"only whitespace", " \t\n", true},
{"line comment only", "-- ping", true},
{"line comment with newline", "-- ping\n", true},
{"block comment only", "/* comment */", true},
{"block comment then semicolons", "/* comment */;", true},
{"comment then query", "-- comment\nSELECT 1", false},
{"block comment then query", "/* comment */SELECT 1", false},
{"SELECT query", "SELECT 1", false},
{"SELECT with semicolon", "SELECT 1;", false},
{"semicolon then query", ";SELECT 1", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isEmptyQuery(tt.query)
if result != tt.expected {
t.Errorf("isEmptyQuery(%q) = %v, want %v", tt.query, result, tt.expected)
}
})
}
}
func TestStripLeadingComments(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "no comments",
input: "SELECT * FROM users",
expected: "SELECT * FROM users",
},
{
name: "block comment at start",
input: "/*Fivetran*/CREATE SCHEMA test",
expected: "CREATE SCHEMA test",
},
{
name: "block comment with spaces",
input: "/* comment */ SELECT 1",
expected: "SELECT 1",
},
{
name: "multiple block comments",
input: "/* first */ /* second */ INSERT INTO t",
expected: "INSERT INTO t",
},
{
name: "line comment at start",
input: "-- comment\nSELECT 1",
expected: "SELECT 1",
},
{
name: "mixed comments",
input: "/* block */ -- line\nUPDATE t SET x=1",
expected: "UPDATE t SET x=1",
},
{
name: "whitespace before comment",
input: " /* comment */ DELETE FROM t",
expected: "DELETE FROM t",
},
{
name: "unclosed block comment",
input: "/* unclosed SELECT",
expected: "/* unclosed SELECT",
},
{
name: "line comment without newline",
input: "-- only comment",
expected: "",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "only whitespace",
input: " ",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := stripLeadingComments(tt.input)
if result != tt.expected {
t.Errorf("stripLeadingComments(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestStripLeadingNoise(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"no noise", "SELECT 1", "SELECT 1"},
{"leading comment", "/* comment */ SELECT 1", "SELECT 1"},
{"leading paren", "(SELECT 1)", "SELECT 1)"},
{"paren then comment", "(/* comment */ SELECT 1)", "SELECT 1)"},
{"comment then paren", "/* comment */ (SELECT 1)", "SELECT 1)"},
{"nested parens and comments", "( ( /* comment */ SELECT 1 ) )", "SELECT 1 ) )"},
{"paren comment paren", "(/* c1 */(/* c2 */ SELECT 1))", "SELECT 1))"},
{"line comment then paren", "-- comment\n(SELECT 1)", "SELECT 1)"},
{"paren then line comment", "( -- comment\nSELECT 1)", "SELECT 1)"},
{"only noise", "( /* comment */ )", ")"},
{"paren then newline", "(\nSELECT 1)", "SELECT 1)"},
{"paren then tab", "(\tSELECT 1)", "SELECT 1)"},
{"paren then CRLF", "(\r\nSELECT 1)", "SELECT 1)"},
{"empty", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := stripLeadingNoise(tt.input)
if result != tt.expected {
t.Errorf("stripLeadingNoise(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestGetCommandType(t *testing.T) {
// Create a minimal clientConn for testing
c := &clientConn{}
tests := []struct {
name string
query string
expected string
}{
// Basic commands without comments
{
name: "SELECT",
query: "SELECT * FROM users",
expected: "SELECT",
},
{
name: "INSERT",
query: "INSERT INTO users VALUES (1)",
expected: "INSERT",
},
{
name: "UPDATE",
query: "UPDATE users SET name='test'",
expected: "UPDATE",
},
{
name: "DELETE",
query: "DELETE FROM users",
expected: "DELETE",
},
{
name: "CREATE TABLE",
query: "CREATE TABLE users (id INT)",
expected: "CREATE TABLE",
},
{
name: "CREATE TEMPORARY TABLE",
query: "CREATE TEMPORARY TABLE temp_users (id INT)",
expected: "CREATE TABLE",
},
{
name: "CREATE TEMP TABLE",
query: "CREATE TEMP TABLE temp_users (id INT)",
expected: "CREATE TABLE",
},
{
name: "CREATE TEMPORARY TABLE with Fivetran comment",
query: "/*Fivetran*/CREATE TEMPORARY TABLE temp_users (id INT)",
expected: "CREATE TABLE",
},
{
name: "CREATE SCHEMA",
query: "CREATE SCHEMA myschema",
expected: "CREATE SCHEMA",
},
{
name: "DROP TABLE",
query: "DROP TABLE users",
expected: "DROP TABLE",
},
{
name: "DROP SCHEMA",
query: "DROP SCHEMA myschema",
expected: "DROP SCHEMA",
},
{
name: "DROP SCHEMA IF EXISTS CASCADE",
query: "DROP SCHEMA IF EXISTS myschema CASCADE",
expected: "DROP SCHEMA",
},
// Commands with Fivetran-style comments
{
name: "CREATE SCHEMA with Fivetran comment",
query: "/*Fivetran*/CREATE SCHEMA test_schema",
expected: "CREATE SCHEMA",
},
{
name: "DROP SCHEMA with Fivetran comment",
query: "/*Fivetran*/DROP SCHEMA IF EXISTS test_schema CASCADE",
expected: "DROP SCHEMA",
},
{
name: "CREATE TABLE with Fivetran comment",
query: "/*Fivetran*/CREATE TABLE test_table (id INT)",
expected: "CREATE TABLE",
},
{
name: "INSERT with Fivetran comment",
query: "/*Fivetran*/INSERT INTO test_table VALUES (1)",
expected: "INSERT",
},
// Commands with other comment styles
{
name: "SELECT with block comment",
query: "/* query */ SELECT 1",
expected: "SELECT",
},
{
name: "UPDATE with line comment",
query: "-- update query\nUPDATE t SET x=1",
expected: "UPDATE",
},
// Transaction commands
{
name: "BEGIN",
query: "BEGIN",
expected: "BEGIN",
},
{
name: "COMMIT",
query: "COMMIT",
expected: "COMMIT",
},
{
name: "ROLLBACK",
query: "ROLLBACK",
expected: "ROLLBACK",
},
// Other commands
{
name: "SET",
query: "SET search_path TO myschema",
expected: "SET",
},
{
name: "TRUNCATE",
query: "TRUNCATE TABLE users",
expected: "TRUNCATE TABLE",
},
{
name: "ALTER",
query: "ALTER TABLE users ADD COLUMN name TEXT",
expected: "ALTER TABLE",
},
// Edge cases
{
name: "lowercase command",
query: "select * from users",
expected: "SELECT",
},
{
name: "mixed case with comment",
query: "/*Test*/Select * From Users",
expected: "SELECT",
},
// WITH (CTE) queries — command type from outer statement
{
name: "WITH + SELECT",
query: "WITH CTE AS (SELECT 1) SELECT * FROM CTE",
expected: "SELECT",
},
{
name: "WITH + INSERT",
query: "WITH CTE AS (SELECT 1) INSERT INTO T SELECT * FROM CTE",
expected: "INSERT",
},
{
name: "WITH + UPDATE",
query: "WITH CTE AS (SELECT 1) UPDATE T SET X = CTE.X FROM CTE",
expected: "UPDATE",
},
{
name: "WITH + DELETE",
query: "WITH CTE AS (SELECT 1) DELETE FROM T USING CTE",
expected: "DELETE",
},
{
name: "WITH + INSERT RETURNING",
query: "WITH CTE AS (SELECT 1) INSERT INTO T VALUES (1) RETURNING *",
expected: "INSERT",
},
{
name: "WITH RECURSIVE + SELECT",
query: "WITH RECURSIVE CTE AS (SELECT 1 UNION ALL SELECT N+1 FROM CTE WHERE N < 10) SELECT * FROM CTE",
expected: "SELECT",
},
{
name: "WITH RECURSIVE + DELETE",
query: "WITH RECURSIVE CTE AS (SELECT 1) DELETE FROM T USING CTE",
expected: "DELETE",
},
{
name: "WITH multiple CTEs + INSERT",
query: "WITH A AS (SELECT 1), B AS (SELECT 2) INSERT INTO T SELECT * FROM A, B",
expected: "INSERT",
},
{
name: "WITH + comment before",
query: "/* batch */ WITH CTE AS (SELECT 1) DELETE FROM T",
expected: "DELETE",
},
{
name: "WITH column aliases + INSERT",
query: "WITH CTE (COL1, COL2) AS (SELECT 1, 2) INSERT INTO T SELECT * FROM CTE",
expected: "INSERT",
},
{
name: "WITH column aliases + SELECT",
query: "WITH CTE (COL1, COL2) AS (SELECT 1, 2) SELECT * FROM CTE",
expected: "SELECT",
},
{
name: "WITH comment between name and AS",
query: "WITH CTE /* HI */ AS (SELECT 1) INSERT INTO T SELECT 1",
expected: "INSERT",
},
{
name: "WITH RECURSIVE + column aliases + INSERT",
query: "WITH RECURSIVE CTE (N) AS (SELECT 1 UNION ALL SELECT N+1 FROM CTE WHERE N < 5) INSERT INTO T SELECT * FROM CTE",
expected: "INSERT",
},
// WITH word boundary — must not match identifiers starting with WITH
{
name: "WITHOUT falls through to SELECT",
query: "WITHOUT SOMETHING",
expected: "SELECT",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// getCommandType expects uppercase input
result := c.getCommandType(tt.query)
if result != tt.expected {
t.Errorf("getCommandType(%q) = %q, want %q", tt.query, result, tt.expected)
}
})
}
}
func TestRedactConnectionString(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "postgres connection string with password",
input: "postgres:host=localhost user=postgres password=secretpass dbname=ducklake",
expected: "postgres:host=localhost user=postgres password=[REDACTED] dbname=ducklake",
},
{
name: "connection string with password= format",
input: "host=localhost password=mysecret user=admin",
expected: "host=localhost password=[REDACTED] user=admin",
},
{
name: "connection string with PASSWORD uppercase",
input: "host=localhost PASSWORD=mysecret user=admin",
expected: "host=localhost PASSWORD=[REDACTED] user=admin",
},
{
name: "connection string without password",
input: "host=localhost user=postgres dbname=test",
expected: "host=localhost user=postgres dbname=test",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "password with special characters",
input: "host=localhost password=p@ss!word123 user=admin",
expected: "host=localhost password=[REDACTED] user=admin",
},
{
name: "password at end of quoted DSN",
input: `"host=localhost password=secret"`,
expected: `"host=localhost password=[REDACTED]"`,
},
{
name: "quoted password value",
input: `host=localhost password="my secret" dbname=test`,
expected: `host=localhost password=[REDACTED] dbname=test`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := redactConnectionString(tt.input)
if result != tt.expected {
t.Errorf("redactConnectionString(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestTransactionStatusTracking(t *testing.T) {
c := &clientConn{txStatus: txStatusIdle}
// Initially should be idle
if c.txStatus != txStatusIdle {
t.Errorf("initial txStatus = %c, want %c", c.txStatus, txStatusIdle)
}
// BEGIN should set to transaction
c.updateTxStatus("BEGIN")
if c.txStatus != txStatusTransaction {
t.Errorf("after BEGIN txStatus = %c, want %c", c.txStatus, txStatusTransaction)
}
// SELECT should not change status
c.updateTxStatus("SELECT")
if c.txStatus != txStatusTransaction {
t.Errorf("after SELECT txStatus = %c, want %c", c.txStatus, txStatusTransaction)
}
// COMMIT should set back to idle
c.updateTxStatus("COMMIT")
if c.txStatus != txStatusIdle {
t.Errorf("after COMMIT txStatus = %c, want %c", c.txStatus, txStatusIdle)
}
// Test ROLLBACK path
c.updateTxStatus("BEGIN")
if c.txStatus != txStatusTransaction {
t.Errorf("after second BEGIN txStatus = %c, want %c", c.txStatus, txStatusTransaction)
}
c.updateTxStatus("ROLLBACK")
if c.txStatus != txStatusIdle {
t.Errorf("after ROLLBACK txStatus = %c, want %c", c.txStatus, txStatusIdle)
}
}
func TestTransactionErrorStatus(t *testing.T) {
c := &clientConn{txStatus: txStatusIdle}
// Error outside transaction should not change status
c.setTxError()
if c.txStatus != txStatusIdle {
t.Errorf("error outside transaction txStatus = %c, want %c", c.txStatus, txStatusIdle)
}
// Error inside transaction should set to error
c.updateTxStatus("BEGIN")
c.setTxError()
if c.txStatus != txStatusError {
t.Errorf("error inside transaction txStatus = %c, want %c", c.txStatus, txStatusError)
}
// ROLLBACK should recover from error state
c.updateTxStatus("ROLLBACK")
if c.txStatus != txStatusIdle {
t.Errorf("after ROLLBACK from error txStatus = %c, want %c", c.txStatus, txStatusIdle)
}
}
func TestNestedBeginDetection(t *testing.T) {
// Test that we can detect when a nested BEGIN would occur
// The actual warning is sent in handleQuery, but we test the detection logic here
c := &clientConn{txStatus: txStatusIdle}
// First BEGIN should work normally
c.updateTxStatus("BEGIN")
if c.txStatus != txStatusTransaction {
t.Errorf("after first BEGIN txStatus = %c, want %c", c.txStatus, txStatusTransaction)
}
// At this point, a second BEGIN should trigger warning behavior
// In handleQuery, when cmdType == "BEGIN" && c.txStatus == txStatusTransaction,
// we send a warning and return success without calling DuckDB
isNestedBegin := c.txStatus == txStatusTransaction
if !isNestedBegin {
t.Error("expected nested BEGIN to be detected")
}
// Transaction status should remain 'T' (not change to 'I' or 'E')
// The warning is sent but the transaction continues
if c.txStatus != txStatusTransaction {
t.Errorf("txStatus should still be %c after nested BEGIN detection, got %c", txStatusTransaction, c.txStatus)
}
}
func TestIsConnectionBroken(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "SSL connection closed",
err: fmt.Errorf("SSL connection has been closed unexpectedly"),
expected: true,
},
{
name: "connection refused",
err: fmt.Errorf("dial tcp: connection refused"),
expected: true,
},
{
name: "broken pipe",
err: fmt.Errorf("write: broken pipe"),
expected: true,
},
{
name: "connection reset",
err: fmt.Errorf("read: connection reset by peer"),
expected: true,
},
{
name: "network unreachable",
err: fmt.Errorf("network is unreachable"),
expected: true,
},
{
name: "no route to host",
err: fmt.Errorf("no route to host"),
expected: true,
},
{
name: "regular query error",
err: fmt.Errorf("table does not exist"),
expected: false,
},
{
name: "syntax error",
err: fmt.Errorf("Parser Error: syntax error"),
expected: false,
},
{
name: "DuckLake SSL error from logs",
err: fmt.Errorf(`Failed to execute query "ROLLBACK": SSL connection has been closed unexpectedly`),
expected: true,
},
{
name: "i/o timeout",
err: fmt.Errorf("read tcp: i/o timeout"),
expected: true,
},
{
name: "use of closed network connection",
err: fmt.Errorf("use of closed network connection"),
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isConnectionBroken(tt.err)
if result != tt.expected {
t.Errorf("isConnectionBroken(%v) = %v, want %v", tt.err, result, tt.expected)
}
})
}
}
func TestQueryReturnsResults(t *testing.T) {
tests := []struct {
name string
query string
expected bool
}{
// SELECT queries
{
name: "simple SELECT",
query: "SELECT * FROM users",
expected: true,
},
{
name: "SELECT with comment",
query: "/*Fivetran*/ SELECT * FROM users",
expected: true,
},
{
name: "SELECT with block and line comment",
query: "/* comment */ -- line\nSELECT 1",
expected: true,
},
// WITH/CTE queries
{
name: "WITH clause",
query: "WITH cte AS (SELECT 1) SELECT * FROM cte",
expected: true,
},
{
name: "WITH clause with comment",
query: "/*Fivetran*/ WITH cte AS (SELECT 1) SELECT * FROM cte",
expected: true,
},
// VALUES
{
name: "VALUES",
query: "VALUES (1, 2), (3, 4)",
expected: true,
},
// SHOW
{
name: "SHOW",
query: "SHOW TABLES",
expected: true,
},
// TABLE
{
name: "TABLE command",
query: "TABLE users",
expected: true,
},
// EXPLAIN
{
name: "EXPLAIN",
query: "EXPLAIN SELECT * FROM users",
expected: true,
},
// DESCRIBE
{
name: "DESCRIBE",
query: "DESCRIBE users",
expected: true,
},
// Non-result queries
{
name: "INSERT",
query: "INSERT INTO users VALUES (1)",
expected: false,
},
{
name: "UPDATE",
query: "UPDATE users SET name = 'test'",
expected: false,
},
{
name: "DELETE",
query: "DELETE FROM users",
expected: false,
},
{
name: "CREATE TABLE",
query: "CREATE TABLE test (id INT)",
expected: false,
},
{
name: "CREATE TABLE with comment",
query: "/*Fivetran*/ CREATE TABLE test (id INT)",
expected: false,
},
{
name: "DROP TABLE",
query: "DROP TABLE users",
expected: false,
},
{
name: "BEGIN",
query: "BEGIN",
expected: false,
},
{
name: "COMMIT",
query: "COMMIT",
expected: false,
},
{
name: "ROLLBACK",
query: "ROLLBACK",
expected: false,
},
{
name: "SET",
query: "SET search_path = public",
expected: false,
},
// DML with RETURNING clause
{
name: "INSERT RETURNING star",
query: "INSERT INTO t VALUES (1) RETURNING *",
expected: true,
},
{
name: "INSERT RETURNING columns",
query: "INSERT INTO t VALUES (1) RETURNING id, name",
expected: true,
},
{
name: "UPDATE RETURNING star",
query: "UPDATE t SET x = 1 RETURNING *",
expected: true,
},
{
name: "DELETE RETURNING star",
query: "DELETE FROM t WHERE id = 1 RETURNING *",
expected: true,
},
{
name: "comment before INSERT RETURNING",
query: "/* comment */ INSERT INTO t VALUES (1) RETURNING *",
expected: true,
},
// Parenthesized queries
{
name: "parenthesized SELECT",
query: "(SELECT 1 UNION SELECT 2)",
expected: true,
},
{
name: "nested parenthesized SELECT",
query: "((SELECT 1))",
expected: true,
},
{
name: "parenthesized SELECT with spaces",
query: "( SELECT 1 )",
expected: true,
},
{
name: "parenthesized SELECT with newline",
query: "(\nSELECT 1)",
expected: true,
},
{
name: "parenthesized SELECT with tab",
query: "(\tSELECT 1)",
expected: true,
},
{
name: "parenthesized SELECT with CRLF",
query: "(\r\nSELECT 1)",
expected: true,
},
// Case sensitivity
{
name: "lowercase insert returning",
query: "insert into t values (1) returning *",
expected: true,
},
{
name: "mixed case Insert Returning",
query: "Insert Into t Values (1) Returning *",
expected: true,
},
{
name: "lowercase select",
query: "select 1",
expected: true,
},
// Multi-line DML RETURNING
{
name: "multi-line INSERT RETURNING",
query: "INSERT INTO t\nVALUES (1)\nRETURNING *",
expected: true,
},
// RETURNING in non-clause positions — correctly rejected
{
name: "RETURNING in string literal (not false positive)",
query: "INSERT INTO t (col) VALUES ('RETURNING')",
expected: false, // inside string literal, skipped by scanner
},
{
name: "RETURNING as column name (not false positive)",
query: "INSERT INTO t (returning) VALUES (1)",
expected: false, // inside parentheses (depth > 0)
},
// Queries that should NOT match RETURNING
{
name: "INSERT with RETURNING-like substring no space",
query: "INSERT INTO treturning VALUES (1)",
expected: false, // no space before RETURNING
},
{
name: "plain CREATE TABLE",
query: "CREATE TABLE returning_results (id INT)",
expected: false, // not INSERT/UPDATE/DELETE prefix
},
// Edge cases
{
name: "empty string",
query: "",
expected: false,
},
{
name: "only whitespace",
query: " ",
expected: false,
},
{
name: "only comment",
query: "-- just a comment",
expected: false,
},
{
name: "SUMMARIZE (DuckDB-specific)",
query: "SUMMARIZE users",
expected: true,
},
{
name: "FROM-first syntax",
query: "FROM users SELECT *",
expected: true,
},
{
name: "EXECUTE",
query: "EXECUTE my_stmt",
expected: true,
},
// WITH + DML RETURNING (WITH prefix matches first, correct)
{
name: "CTE with INSERT RETURNING",
query: "WITH cte AS (SELECT 1) INSERT INTO t SELECT * FROM cte RETURNING *",
expected: true,
},
// ALTER, TRUNCATE — should not return results
{
name: "ALTER TABLE",
query: "ALTER TABLE t ADD COLUMN x INT",
expected: false,
},
{
name: "TRUNCATE",
query: "TRUNCATE t",
expected: false,
},
// DELETE with RETURNING in subquery — correctly rejected (depth > 0)
{
name: "DELETE with RETURNING in subquery",
query: "DELETE FROM t WHERE id IN (SELECT returning FROM s)",
expected: false,
},
// Tab before RETURNING
{
name: "INSERT with tab before RETURNING",
query: "INSERT INTO t VALUES (1)\tRETURNING *",
expected: true,
},
// Carriage return before RETURNING
{
name: "INSERT with CR before RETURNING",
query: "INSERT INTO t VALUES (1)\rRETURNING *",
expected: true,
},
// RETURNING with no preceding whitespace — not a clause
{
name: "INSERT NORETURNING (no space)",
query: "INSERT INTO tRETURNING VALUES (1)",
expected: false,
},
// Multiple RETURNING occurrences — first in parens, second real
{
name: "INSERT with RETURNING in subselect and real RETURNING",
query: "INSERT INTO t SELECT (RETURNING) FROM s RETURNING *",
expected: true,
},
// WITH + DML (no RETURNING) — queryReturnsResults returns true because
// of the WITH prefix, but these don't actually return results.
// The isWithDML guard in handleDescribe prevents mutation during probing.
{
name: "WITH + INSERT (no RETURNING)",
query: "WITH cte AS (SELECT 1) INSERT INTO t SELECT * FROM cte",
expected: true, // WITH prefix → true (isWithDML handles this in Describe)
},
{
name: "WITH + DELETE (no RETURNING)",
query: "WITH cte AS (SELECT 1) DELETE FROM t WHERE id IN (SELECT * FROM cte)",
expected: true, // WITH prefix → true (isWithDML handles this in Describe)
},
{
name: "WITHOUT is not WITH",
query: "WITHOUT SOMETHING",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := queryReturnsResults(tt.query)
if result != tt.expected {
t.Errorf("queryReturnsResults(%q) = %v, want %v", tt.query, result, tt.expected)
}
})
}
}
// TestQueryReturnsResultsWithComments verifies that queries with leading comments
// are correctly identified as result-returning queries.
func TestQueryReturnsResultsWithComments(t *testing.T) {
tests := []struct {
name string
query string
expected bool
}{
// Queries with leading comments that return results
{"block comment before SELECT", "/* comment */ SELECT * FROM users", true},
{"block comment before SELECT no space", "/*comment*/SELECT 1", true},
{"block comment before WITH", "/* query */ WITH cte AS (SELECT 1) SELECT * FROM cte", true},
{"line comment before SELECT", "-- comment\nSELECT * FROM users", true},
{"multiple block comments", "/* first */ /* second */ SELECT 1", true},
{"block comment before SHOW", "/* comment */ SHOW TABLES", true},
{"block comment before VALUES", "/* comment */ VALUES (1, 2)", true},
// Queries with leading comments that return results (DML RETURNING)
{"block comment before INSERT RETURNING", "/* comment */ INSERT INTO t VALUES (1) RETURNING *", true},
{"block comment before UPDATE RETURNING", "/* comment */ UPDATE t SET x = 1 RETURNING *", true},
{"block comment before DELETE RETURNING", "/* comment */ DELETE FROM t RETURNING *", true},
// Parentheses interleaved with comments
{"paren then comment then SELECT", "(/* comment */ SELECT 1)", true},
{"comment then paren then SELECT", "/* comment */ (SELECT 1)", true},
{"nested parens and comments", "( ( /* comment */ SELECT 1 ) )", true},
{"paren then line comment then SELECT", "( -- comment\nSELECT 1)", true},
{"paren comment paren SELECT", "(/* c1 */(/* c2 */ SELECT 1))", true},
{"paren then comment then INSERT RETURNING", "(/* comment */ INSERT INTO t VALUES (1) RETURNING *)", true},
// Queries with leading comments that don't return results
{"block comment before INSERT", "/* comment */ INSERT INTO t VALUES (1)", false},
{"block comment before CREATE", "/* comment */ CREATE TABLE t (id INT)", false},
{"block comment before DROP", "/* comment */ DROP TABLE t", false},
{"block comment before BEGIN", "/* comment */ BEGIN", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := queryReturnsResults(tt.query)
if result != tt.expected {
t.Errorf("queryReturnsResults(%q) = %v, want %v", tt.query, result, tt.expected)
}
})
}
}
// TestContainsReturning tests the low-level scanner directly.
// Input must be pre-uppercased (matching how callers invoke it).
func TestContainsReturning(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
// --- Basic positive cases ---