Newer
Older
-- test-run result file version 2
remote = require('net.box')
| ---
| ...
test_run = require('test_run').new()
| ---
| ...
fiber = require('fiber')
| ---
| ...
-- Wrappers to make remote and local execution interface return
-- same result pattern.
--
is_remote = test_run:get_cfg('remote') == 'true'
prepare = nil
| ---
| ...
test_run:cmd("setopt delimiter ';'")
if is_remote then
box.schema.user.grant('guest','read, write, execute', 'universe')
box.schema.user.grant('guest', 'create', 'space')
cn = remote.connect(box.cfg.listen)
execute = function(...) return cn:execute(...) end
prepare = function(...) return cn:prepare(...) end
unprepare = function(...) return cn:unprepare(...) end
else
execute = function(...)
local res, err = box.execute(...)
if err ~= nil then
error(err)
end
return res
end
prepare = function(...)
local res, err = box.prepare(...)
if err ~= nil then
error(err)
end
return res
end
unprepare = function(...)
local res, err = box.unprepare(...)
if err ~= nil then
error(err)
end
return res
end
end;
| ---
| ...
test_run:cmd("setopt delimiter ''");
| ---
| - true
| ...
-- Check default cache statistics.
--
box.info.sql()
| ---
| - cache:
| size: 0
| stmt_count: 0
| ...
box.info:sql()
| ---
| - cache:
| size: 0
| stmt_count: 0
| ...
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
-- Test local interface and basic capabilities of prepared statements.
--
execute('CREATE TABLE test (id INT PRIMARY KEY, a NUMBER, b TEXT)')
| ---
| - row_count: 1
| ...
space = box.space.TEST
| ---
| ...
space:replace{1, 2, '3'}
| ---
| - [1, 2, '3']
| ...
space:replace{4, 5, '6'}
| ---
| - [4, 5, '6']
| ...
space:replace{7, 8.5, '9'}
| ---
| - [7, 8.5, '9']
| ...
s, e = prepare("SELECT * FROM test WHERE id = ? AND a = ?;")
| ---
| ...
assert(e == nil)
| ---
| - true
| ...
assert(s ~= nil)
| ---
| - true
| ...
s.stmt_id
| ---
| - 3603193623
| ...
s.metadata
| ---
| - - name: ID
| type: integer
| - name: A
| type: number
| - name: B
| type: string
| ...
s.params
| ---
| - - name: '?'
| type: ANY
| - name: '?'
| type: ANY
| ...
s.param_count
| ---
| - 2
| ...
execute(s.stmt_id, {1, 2})
| ---
| - metadata:
| - name: ID
| type: integer
| - name: A
| type: number
| - name: B
| type: string
| rows:
| - [1, 2, '3']
| ...
execute(s.stmt_id, {1, 3})
| ---
| - metadata:
| - name: ID
| type: integer
| - name: A
| type: number
| - name: B
| type: string
| rows: []
| ...
assert(box.info.sql().cache.stmt_count ~= 0)
| ---
| - true
| ...
assert(box.info.sql().cache.size ~= 0)
| ---
| - true
| ...
test_run:cmd("setopt delimiter ';'")
if not is_remote then
res = s:execute({1, 2})
assert(res ~= nil)
res = s:execute({1, 3})
assert(res ~= nil)
end;
test_run:cmd("setopt delimiter ''");
| ---
| - true
| ...
unprepare(s.stmt_id)
assert(box.info.sql().cache.stmt_count == 0)
| ---
| - true
| ...
assert(box.info.sql().cache.size == 0)
| ---
| - true
| ...
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
-- Test preparation of different types of queries.
-- Let's start from DDL. It doesn't make much sense since
-- any prepared DDL statement can be executed once, but
-- anyway make sure that no crashes occur.
--
s = prepare("CREATE INDEX i1 ON test(a)")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("DROP INDEX i1 ON test;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("CREATE VIEW v AS SELECT * FROM test;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("DROP VIEW v;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("ALTER TABLE test RENAME TO test1")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 0
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
box.execute("CREATE TABLE test2 (id INT PRIMARY KEY);")
| ---
| - row_count: 1
| ...
s = prepare("ALTER TABLE test2 ADD CONSTRAINT fk1 FOREIGN KEY (id) REFERENCES test2")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
box.space.TEST2:drop()
| ---
| ...
s = prepare("CREATE TRIGGER tr1 INSERT ON test1 FOR EACH ROW BEGIN DELETE FROM test1; END;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("DROP TRIGGER tr1;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("DROP TABLE test1;")
| ---
| ...
execute(s.stmt_id)
| ---
| - row_count: 1
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
-- DQL
--
execute('CREATE TABLE test (id INT PRIMARY KEY, a NUMBER, b TEXT)')
| ---
| - row_count: 1
| ...
space = box.space.TEST
| ---
| ...
space:replace{1, 2, '3'}
| ---
| - [1, 2, '3']
| ...
space:replace{4, 5, '6'}
| ---
| - [4, 5, '6']
| ...
space:replace{7, 8.5, '9'}
| ---
| - [7, 8.5, '9']
| ...
_ = prepare("SELECT a FROM test WHERE b = '3';")
| ---
| ...
s = prepare("SELECT a FROM test WHERE b = '3';")
| ---
| ...
execute(s.stmt_id)
| ---
| - metadata:
| - name: A
| type: number
| rows:
| - [2]
| ...
execute(s.stmt_id)
| ---
| - metadata:
| - name: A
| type: number
| rows:
| - [2]
| ...
test_run:cmd("setopt delimiter ';'")
if not is_remote then
res = s:execute()
assert(res ~= nil)
res = s:execute()
assert(res ~= nil)
end;
| ...
test_run:cmd("setopt delimiter ''");
| ---
| - true
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
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("SELECT count(*), count(a - 3), max(b), abs(id) FROM test WHERE b = '3';")
| ---
| ...
execute(s.stmt_id)
| ---
| - metadata:
| - name: count(*)
| type: integer
| - name: count(a - 3)
| type: integer
| - name: max(b)
| type: scalar
| - name: abs(id)
| type: number
| rows:
| - [1, 1, '3', 1]
| ...
execute(s.stmt_id)
| ---
| - metadata:
| - name: count(*)
| type: integer
| - name: count(a - 3)
| type: integer
| - name: max(b)
| type: scalar
| - name: abs(id)
| type: number
| rows:
| - [1, 1, '3', 1]
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
-- Let's try something a bit more complicated. For instance recursive
-- query displaying Mandelbrot set.
--
s = prepare([[WITH RECURSIVE \
xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2), \
yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0), \
m(iter, cx, cy, x, y) AS ( \
SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis \
UNION ALL \
SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \
WHERE (x*x + y*y) < 4.0 AND iter<28), \
m2(iter, cx, cy) AS ( \
SELECT max(iter), cx, cy FROM m GROUP BY cx, cy), \
a(t) AS ( \
SELECT group_concat( substr(' .+*#', 1+LEAST(iter/7,4), 1), '') \
FROM m2 GROUP BY cy) \
SELECT group_concat(TRIM(TRAILING FROM t),x'0a') FROM a;]])
| ---
| ...
res = execute(s.stmt_id)
| ---
| ...
res.metadata
| ---
| - - name: group_concat(TRIM(TRAILING FROM t),x'0a')
| type: string
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
-- Workflow with bindings is still the same.
--
s = prepare("SELECT a FROM test WHERE b = ?;")
| ---
| ...
execute(s.stmt_id, {'6'})
| ---
| - metadata:
| - name: A
| type: number
| rows:
| - [5]
| ...
execute(s.stmt_id, {'9'})
| ---
| - metadata:
| - name: A
| type: number
| rows:
| - [8.5]
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
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
-- gh-4760: make sure that names of all bindings are parsed correctly.
--
s = prepare("SELECT a FROM test WHERE id = :id AND b = :name")
| ---
| ...
s.params[1]
| ---
| - name: :id
| type: ANY
| ...
s.params[2]
| ---
| - name: :name
| type: ANY
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
s = prepare("SELECT ?, :id, :name, ?, @name2, ?")
| ---
| ...
s.params[1]
| ---
| - name: '?'
| type: ANY
| ...
s.params[2]
| ---
| - name: :id
| type: ANY
| ...
s.params[3]
| ---
| - name: :name
| type: ANY
| ...
s.params[4]
| ---
| - name: '?'
| type: ANY
| ...
s.params[5]
| ---
| - name: '@name2'
| type: ANY
| ...
s.params[6]
| ---
| - name: '?'
| type: ANY
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
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
-- DML
s = prepare("INSERT INTO test VALUES (?, ?, ?);")
| ---
| ...
execute(s.stmt_id, {5, 6, '7'})
| ---
| - row_count: 1
| ...
execute(s.stmt_id, {6, 10, '7'})
| ---
| - row_count: 1
| ...
execute(s.stmt_id, {9, 11, '7'})
| ---
| - row_count: 1
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
-- EXPLAIN works fine.
--
s1 = prepare("EXPLAIN SELECT a FROM test WHERE b = '3';")
| ---
| ...
res = execute(s1.stmt_id)
| ---
| ...
res.metadata
| ---
| - - name: addr
| type: integer
| - name: opcode
| type: text
| - name: p1
| type: integer
| - name: p2
| type: integer
| - name: p3
| type: integer
| - name: p4
| type: text
| - name: p5
| type: text
| - name: comment
| type: text
| ...
assert(res.rows ~= nil)
| ---
| - true
| ...
s2 = prepare("EXPLAIN QUERY PLAN SELECT a FROM test WHERE b = '3';")
| ---
| ...
res = execute(s2.stmt_id)
| ---
| ...
res.metadata
| ---
| - - name: selectid
| type: integer
| - name: order
| type: integer
| - name: from
| type: integer
| - name: detail
| type: text
| ...
assert(res.rows ~= nil)
| ---
| - true
| ...
unprepare(s2.stmt_id)
| ---
| - null
| ...
unprepare(s1.stmt_id)
| ---
| - null
| ...
-- Prepare call re-compiles statement if it is expired
-- after schema change.
--
s = prepare("SELECT a FROM test WHERE b = ?;")
| ---
| ...
sp = box.schema.create_space("s")
| ---
| ...
sp:drop()
| ---
| ...
execute(s.stmt_id)
| ---
| - error: 'Failed to execute SQL statement: statement has expired'
| ...
_ = prepare("SELECT a FROM test WHERE b = ?;")
| ---
| ...
execute(s.stmt_id)
| ---
| - metadata:
| - name: A
| type: number
| rows: []
| ...
unprepare(s.stmt_id)
| ---
| - null
| ...
-- Setting cache size to 0 is possible only in case if
-- there's no any prepared statements right now .
--
box.cfg{sql_cache_size = 0 }
| ---
| ...
assert(box.info.sql().cache.stmt_count == 0)
| - true
| ...
assert(box.info.sql().cache.size == 0)
| ---
| - true
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
| ...
prepare("SELECT a FROM test;")
| ---
| - error: 'Failed to prepare SQL statement: Memory limit for SQL prepared statements
| has been reached. Please, deallocate active statements or increase SQL cache size.'
| ...
box.cfg{sql_cache_size = 0}
| ---
| ...
-- Still with small size everything should work.
--
box.cfg{sql_cache_size = 1500}
| ---
| ...
test_run:cmd("setopt delimiter ';'");
| ---
| - true
| ...
ok = nil
res = nil
_ = fiber.create(function()
for i = 1, 5 do
pcall(prepare, string.format("SELECT * FROM test WHERE a = %d;", i))
end
ok, res = pcall(prepare, "SELECT * FROM test WHERE b = '6';")
end);
| ---
| ...
while ok == nil do fiber.sleep(0.00001) end;
| ---
| ...
assert(ok == false);
| ---
| - true
| ...
res;
| ---
| - 'Failed to prepare SQL statement: Memory limit for SQL prepared statements has been
| reached. Please, deallocate active statements or increase SQL cache size.'
| ...
-- Check that after fiber is dead, its session gets rid of
-- all prepared statements.
--
if is_remote then
cn:close()
cn = remote.connect(box.cfg.listen)
end;
| ---
| ...
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
box.cfg{sql_cache_size = 0};
| ---
| ...
box.cfg{sql_cache_size = 3000};
| ---
| ...
-- Make sure that if prepared statement is busy (is executed
-- right now), prepared statement is not used, i.e. statement
-- is compiled from scratch, executed and finilized.
--
box.schema.func.create('SLEEP', {language = 'Lua',
body = 'function () fiber.sleep(0.3) return 1 end',
exports = {'LUA', 'SQL'}});
| ---
| ...
s = prepare("SELECT id, SLEEP() FROM test;");
| ---
| ...
assert(s ~= nil);
| ---
| - true
| ...
function implicit_yield()
s = prepare("SELECT id, SLEEP() FROM test;")
execute(s.stmt_id)
end;
| ---
| ...
f1 = fiber.new(implicit_yield)
f2 = fiber.new(implicit_yield)
f1:set_joinable(true)
f2:set_joinable(true)
f1:join();
| ---
| ...
f2:join();
| ---
| - true
| ...
unprepare(s.stmt_id);
| ---
| - null
| ...
-- Now during execution of one prepared statement, in another
-- session schema is invalidated and statement is re-compiled.
--
function invalidate_schema_and_prepare()
sp = box.schema.create_space("s")
sp:drop()
s = prepare("SELECT id, SLEEP() FROM test;")
assert(s ~= nil)
unprepare(s.stmt_id)
end;
| ---
| ...
f1 = fiber.new(implicit_yield)
f2 = fiber.new(invalidate_schema_and_prepare)
f1:set_joinable(true)
f2:set_joinable(true)
f1:join();
| ---
| ...
f2:join();
| ---
| - true
| ...
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
-- gh-4825: make sure that values to be bound are erased after
-- execution, so that they don't appear in the next statement
-- execution.
--
s = prepare('SELECT :a, :b, :c');
| ---
| ...
execute(s.stmt_id, {{[':a'] = 1}, {[':b'] = 2}, {[':c'] = 3}});
| ---
| - metadata:
| - name: :a
| type: integer
| - name: :b
| type: integer
| - name: :c
| type: integer
| rows:
| - [1, 2, 3]
| ...
execute(s.stmt_id, {{[':a'] = 1}, {[':b'] = 2}});
| ---
| - metadata:
| - name: :a
| type: integer
| - name: :b
| type: integer
| - name: :c
| type: boolean
| rows:
| - [1, 2, null]
| ...
execute(s.stmt_id);
| ---
| - metadata:
| - name: :a
| type: boolean
| - name: :b
| type: boolean
| - name: :c
| type: boolean
| rows:
| - [null, null, null]
| ...
unprepare(s.stmt_id);
| ---
| - null
| ...
if is_remote then
cn:close()
box.schema.user.revoke('guest', 'read, write, execute', 'universe')
box.schema.user.revoke('guest', 'create', 'space')
end;
| ---
| ...
test_run:cmd("setopt delimiter ''");
| ---
| - true
| ...
box.cfg{sql_cache_size = 5 * 1024 * 1024}
| ---
| ...
box.space.TEST:drop()
| ---
| ...
box.schema.func.drop('SLEEP')
| ---
| ...