forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursors.pt
568 lines (531 loc) · 14.7 KB
/
cursors.pt
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
# Verify we can pgwire Bind, Execute, then SQL FETCH and CLOSE, then
# pgwire Execute fails (due to the close). This tests that pgwire portals
# and SQL cursors are the same thing.
send
Query {"query": "BEGIN"}
Parse {"name": "s", "query": "VALUES (2), (4), (6)"}
Bind {"portal": "p", "statement": "s"}
Execute {"portal": "p", "max_rows": 1}
Sync
Query {"query": "FETCH 1 p"}
Query {"query": "CLOSE \"p\""}
Execute {"portal": "p", "max_rows": 1}
Sync
Query {"query": "ROLLBACK"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
ParseComplete
BindComplete
DataRow {"fields":["2"]}
PortalSuspended
ReadyForQuery {"status":"T"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["4"]}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"CLOSE CURSOR"}
ReadyForQuery {"status":"T"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"34000"},{"typ":"M","value":"portal \"p\" does not exist"}]}
ReadyForQuery {"status":"E"}
CommandComplete {"tag":"ROLLBACK"}
ReadyForQuery {"status":"I"}
# Ensure FETCH with no count returns 1 row.
send
Query {"query": "BEGIN"}
Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6)"}
Query {"query": "FETCH c"}
Query {"query": "CLOSE c"}
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"DECLARE CURSOR"}
ReadyForQuery {"status":"T"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["2"]}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"CLOSE CURSOR"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# FETCH with a high count.
send
Query {"query": "BEGIN"}
Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6)"}
Query {"query": "FETCH 2000 c"}
Query {"query": "CLOSE c"}
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"DECLARE CURSOR"}
ReadyForQuery {"status":"T"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["2"]}
DataRow {"fields":["4"]}
DataRow {"fields":["6"]}
CommandComplete {"tag":"FETCH 3"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"CLOSE CURSOR"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# DECLARE outside a transaction should fail. A test like:
# Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6)"}
# would trigger this but we have slightly different transaction semantics
# than Postgres that would require special casing this situation, and
# it's safe to ignore, so we don't implement or test it.
# Surprisingly, the same statement but with a SELECT added on passes. This
# is because Postgres has some different transaction states. For a Query
# message containing a single statement, it uses the DEFAULT state. If
# there are multiple statements it uses INPROGRESS_IMPLICIT, which acts
# similar to a BEGIN. The SELECT here triggers that, which then causes
# DECLARE to error. Since we have fewer transaction states our DECLARE
# doesn't detect this for the single statement case.
send
Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6); SELECT 1"}
----
until
ReadyForQuery
----
CommandComplete {"tag":"DECLARE CURSOR"}
RowDescription {"fields":[{"name":"?column?"}]}
DataRow {"fields":["1"]}
CommandComplete {"tag":"SELECT 1"}
ReadyForQuery {"status":"I"}
send
Query {"query": "CLOSE c"}
----
until err_field_typs=M
ReadyForQuery
----
ErrorResponse {"fields":[{"typ":"M","value":"cursor \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
send
Query {"query": "FETCH c"}
----
until err_field_typs=M
ReadyForQuery
----
ErrorResponse {"fields":[{"typ":"M","value":"cursor \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
# Verify that cursor and portal close messages differ.
send
Execute {"portal": "c"}
Sync
----
until err_field_typs=M
ReadyForQuery
----
ErrorResponse {"fields":[{"typ":"M","value":"portal \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
# Verify that Describe will fail before and after a transaction, but work within one.
send
Describe {"variant": "P", "name": "c"}
Sync
Query {"query": "BEGIN"}
Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6)"}
Describe {"variant": "P", "name": "c"}
Query {"query": "COMMIT"}
Describe {"variant": "P", "name": "c"}
Sync
----
until
ErrorResponse
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"34000"},{"typ":"M","value":"portal \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"DECLARE CURSOR"}
ReadyForQuery {"status":"T"}
RowDescription {"fields":[{"name":"column1"}]}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"34000"},{"typ":"M","value":"portal \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
# Verify that a single Query message can declare and fetch from a
# portal. This tests that, even though DECLARE cannot be used outside
# of a transaction, a single Query message with multiple statements is
# a transaction. Also verify that it doesn't exist afterward.
send
Query {"query": "DECLARE c CURSOR FOR VALUES (2), (4), (6); FETCH 2 c; FETCH 10 c;"}
Describe {"variant": "P", "name": "c"}
Sync
----
until
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"DECLARE CURSOR"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["2"]}
DataRow {"fields":["4"]}
CommandComplete {"tag":"FETCH 2"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["6"]}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"I"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"34000"},{"typ":"M","value":"portal \"c\" does not exist"}]}
ReadyForQuery {"status":"I"}
# Test cursors in extended protocol.
send
Query {"query": "BEGIN"}
Parse {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3)"}
Describe
Bind
Execute
Parse {"query": "FETCH c"}
Describe
Bind
Execute
Execute {"portal": "c"}
Parse {"query": "CLOSE c"}
Describe
Bind
Execute
Sync
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
ParseComplete
ParameterDescription {"parameters":[]}
NoData
BindComplete
CommandComplete {"tag":"DECLARE CURSOR"}
ParseComplete
ParameterDescription {"parameters":[]}
RowDescription {"fields":[{"name":"column1"}]}
BindComplete
DataRow {"fields":["1"]}
CommandComplete {"tag":"FETCH 1"}
DataRow {"fields":["2"]}
DataRow {"fields":["3"]}
CommandComplete {"tag":"SELECT 2"}
ParseComplete
ParameterDescription {"parameters":[]}
NoData
BindComplete
CommandComplete {"tag":"CLOSE CURSOR"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# Here's a tricky one. Create a portal ("c") with 6 rows in it. Create
# and bind another portal ("a") that fetches 2 rows from "c", but don't
# execute "a" at all. Execute the empty portal pulling 2 rows from c
# (1, 2). Execute portal "a" but only request 1 row (3). At this point
# "a" has also cached row 4 because it's a 2 row FETCH. Row 4 is in some
# cache somewhere, and no longer in portal "c". Verify this by pulling
# 2 more rows from "c" (5, 6). Finally pull the remaining rows from "a"
# (4). This test verifies that the first execution of a FETCH will pull
# N rows from its target portal and cache them.
# NOTE: We differ from postgres here so don't actually test this. The
# test and comment are left here so future readers can understand what
# should be happening if we were fully compliant.
#send
#Query {"query": "BEGIN"}
#Query {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3), (4), (5), (6)"}
#Parse {"query": "FETCH 2 c"}
#Bind {"portal": "a"}
#Sync
#Query {"query": "FETCH 2 c"}
#Execute {"portal": "a", "max_rows": 1}
#Sync
#Query {"query": "FETCH 2 c"}
#Execute {"portal": "a"}
#Sync
#Query {"query": "FETCH 2 c"}
#Query {"query": "COMMIT"}
#----
#
#until
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#ReadyForQuery
#----
#CommandComplete {"tag":"BEGIN"}
#ReadyForQuery {"status":"T"}
#CommandComplete {"tag":"DECLARE CURSOR"}
#ReadyForQuery {"status":"T"}
#ParseComplete
#BindComplete
#ReadyForQuery {"status":"T"}
#RowDescription {"fields":[{"name":"column1"}]}
#DataRow {"fields":["1"]}
#DataRow {"fields":["2"]}
#CommandComplete {"tag":"FETCH 2"}
#ReadyForQuery {"status":"T"}
#DataRow {"fields":["3"]}
#PortalSuspended
#ReadyForQuery {"status":"T"}
#RowDescription {"fields":[{"name":"column1"}]}
#DataRow {"fields":["5"]}
#DataRow {"fields":["6"]}
#CommandComplete {"tag":"FETCH 2"}
#ReadyForQuery {"status":"T"}
#DataRow {"fields":["4"]}
#CommandComplete {"tag":"FETCH 2"}
#ReadyForQuery {"status":"T"}
#RowDescription {"fields":[{"name":"column1"}]}
#CommandComplete {"tag":"FETCH 0"}
#ReadyForQuery {"status":"T"}
#CommandComplete {"tag":"COMMIT"}
#ReadyForQuery {"status":"I"}
# Test FETCH 0. This should return 0 rows (not all remaining, like Execute).
send
Query {"query": "BEGIN"}
Query {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3), (4), (5), (6)"}
Query {"query": "FETCH 0 c"}
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"DECLARE CURSOR"}
ReadyForQuery {"status":"T"}
RowDescription {"fields":[{"name":"column1"}]}
CommandComplete {"tag":"FETCH 0"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# Executing a DECLARE twice fails.
send
Query {"query": "BEGIN"}
Parse {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3)"}
Bind
Execute
Execute
Sync
Query {"query": "ROLLBACK"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
ParseComplete
BindComplete
CommandComplete {"tag":"DECLARE CURSOR"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"55000"},{"typ":"M","value":"portal \"\" cannot be run"}]}
ReadyForQuery {"status":"E"}
CommandComplete {"tag":"ROLLBACK"}
ReadyForQuery {"status":"I"}
# Executing a FETCH twice does nothing the second time. It must be
# rebound to get more rows.
# Executing a CLOSE twice fails like DECLARE.
send
Query {"query": "BEGIN"}
Parse {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3)"}
Bind
Execute
Parse {"query": "FETCH c"}
Bind
Execute
Execute
Bind
Execute
Execute
Parse {"query": "CLOSE c"}
Bind
Execute
Execute
Sync
Query {"query": "ROLLBACK"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
ParseComplete
BindComplete
CommandComplete {"tag":"DECLARE CURSOR"}
ParseComplete
BindComplete
DataRow {"fields":["1"]}
CommandComplete {"tag":"FETCH 1"}
CommandComplete {"tag":"FETCH 1"}
BindComplete
DataRow {"fields":["2"]}
CommandComplete {"tag":"FETCH 1"}
CommandComplete {"tag":"FETCH 1"}
ParseComplete
BindComplete
CommandComplete {"tag":"CLOSE CURSOR"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"55000"},{"typ":"M","value":"portal \"\" cannot be run"}]}
ReadyForQuery {"status":"E"}
CommandComplete {"tag":"ROLLBACK"}
ReadyForQuery {"status":"I"}
# Test FETCH with various combinations of row and execute counts. And
# recall that since we don't support Execute with max_rows < FETCH's
# count, we aren't testing that here, but should be if we add it.
send
Query {"query": "BEGIN"}
Parse {"query": "DECLARE c CURSOR FOR VALUES (1), (2), (3), (4), (5), (6), (7), (8)"}
Bind
Execute
Parse {"query": "FETCH 2 c"}
Bind
Execute
Execute
Bind
Execute {"max_rows": 4}
Execute {"max_rows": 4}
Execute
Parse {"query": "FETCH c"}
Bind
Execute {"max_rows": 2}
Execute {"max_rows": 2}
Sync
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
ReadyForQuery {"status":"T"}
ParseComplete
BindComplete
CommandComplete {"tag":"DECLARE CURSOR"}
ParseComplete
BindComplete
DataRow {"fields":["1"]}
DataRow {"fields":["2"]}
CommandComplete {"tag":"FETCH 2"}
CommandComplete {"tag":"FETCH 2"}
BindComplete
DataRow {"fields":["3"]}
DataRow {"fields":["4"]}
CommandComplete {"tag":"FETCH 2"}
CommandComplete {"tag":"FETCH 2"}
CommandComplete {"tag":"FETCH 2"}
ParseComplete
BindComplete
DataRow {"fields":["5"]}
CommandComplete {"tag":"FETCH 1"}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# Verify that the empty portal is removed after Query.
send
Query {"query": "BEGIN; DECLARE c CURSOR FOR VALUES (1), (2); FETCH c;"}
Execute
Sync
Query {"query": "ROLLBACK"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
CommandComplete {"tag":"DECLARE CURSOR"}
RowDescription {"fields":[{"name":"column1"}]}
DataRow {"fields":["1"]}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"T"}
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"34000"},{"typ":"M","value":"portal \"\" does not exist"}]}
ReadyForQuery {"status":"E"}
CommandComplete {"tag":"ROLLBACK"}
ReadyForQuery {"status":"I"}
# Test binary cursors. This tests that a cursor declared over simple query
# (i.e., text result format) can be requested in binary.
send
Query {"query": "BEGIN; DECLARE c CURSOR FOR VALUES (1), (2);"}
Parse {"query": "FETCH c"}
Bind {"result_formats": [1]}
Execute
Sync
Query {"query": "COMMIT"}
----
until
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
CommandComplete {"tag":"BEGIN"}
CommandComplete {"tag":"DECLARE CURSOR"}
ReadyForQuery {"status":"T"}
ParseComplete
BindComplete
DataRow {"fields":["\u0000\u0000\u0000\u0001"]}
CommandComplete {"tag":"FETCH 1"}
ReadyForQuery {"status":"T"}
CommandComplete {"tag":"COMMIT"}
ReadyForQuery {"status":"I"}
# Verify that a complaint is issued if DECLARE is not in an implicit or
# explicit transaction.
send
Query {"query": "DECLARE c CURSOR FOR SELECT 1"}
----
until
ReadyForQuery
----
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"25P01"},{"typ":"M","value":"DECLARE CURSOR can only be used in transaction blocks"}]}
ReadyForQuery {"status":"I"}
send
Parse {"query": "DECLARE c CURSOR FOR SELECT 1"}
Bind
Execute
Sync
----
until
ReadyForQuery
----
ParseComplete
BindComplete
ErrorResponse {"fields":[{"typ":"S","value":"ERROR"},{"typ":"C","value":"25P01"},{"typ":"M","value":"DECLARE CURSOR can only be used in transaction blocks"}]}
ReadyForQuery {"status":"I"}