forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_func.slt
1316 lines (1080 loc) · 27.8 KB
/
table_func.slt
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
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
#
# This file is derived from the logic test suite in CockroachDB. The
# original file was retrieved on June 10, 2019 from:
#
# The original source code is subject to the terms of the Apache
# 2.0 license, a copy of which can be found in the LICENSE file at the
# root of this repository.
mode cockroach
statement ok
CREATE TABLE y (a JSONB)
# Ensure this does not panic.
query TTTT
SELECT * FROM y a, y b, jsonb_each(a.a);
----
query I rowsort
SELECT generate_series FROM generate_series(1, 3)
----
1
2
3
query I rowsort
SELECT generate_series FROM generate_series(2, 4)
----
2
3
4
query II colnames,rowsort
SELECT * FROM generate_series(2, 4) WITH ORDINALITY
----
generate_series ordinality
2 1
3 2
4 3
query I rowsort
SELECT generate_series FROM generate_series(-2, 2)
----
0
1
2
-2
-1
query I rowsort
SELECT generate_series FROM generate_series(-2::bigint, 2)
----
0
1
2
-2
-1
query I
SELECT generate_series FROM generate_series(null, 1)
----
query I
SELECT generate_series FROM generate_series(1, null)
----
query error unable to determine which implementation to use
SELECT generate_series FROM generate_series(null, null)
----
statement error invalid input syntax for type integer: invalid digit found in string: "foo"
SELECT generate_series FROM generate_series('foo', 2)
statement error invalid input syntax for type integer: invalid digit found in string: "foo"
SELECT generate_series FROM generate_series(1, 'foo')
statement error arguments cannot be implicitly cast to any implementation's parameters
SELECT generate_series FROM generate_series(2)
query T multiline
EXPLAIN RAW PLAN FOR SELECT generate_series FROM generate_series(-2, 2)
----
%0 =
| CallTable generate_series(-2, 2, 1)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT generate_series FROM generate_series(-2, 2)
----
%0 =
| Constant (0) (1) (2) (-2) (-1)
EOF
query I colnames
SELECT x FROM generate_series(1, 3) x
----
x
1
2
3
# generate_series with lateral joins.
statement ok
CREATE TABLE x (a INT PRIMARY KEY, b INT)
statement ok
INSERT INTO x VALUES (1, 2), (2, 3), (3, 4)
query III
SELECT * FROM x, generate_series(1, a)
----
1 2 1
2 3 1
2 3 2
3 4 1
3 4 2
3 4 3
# Both from the first one.
query IIIII
SELECT * FROM x x1, x x2, generate_series(x1.a, x1.b) WHERE x1.b = x2.b
----
1 2 1 2 1
1 2 1 2 2
2 3 2 3 2
2 3 2 3 3
3 4 3 4 3
3 4 3 4 4
# Both from the second one.
query IIIII
SELECT * FROM x x1, x x2, generate_series(x2.a, x2.b) WHERE x1.b = x2.b
----
1 2 1 2 1
1 2 1 2 2
2 3 2 3 2
2 3 2 3 3
3 4 3 4 3
3 4 3 4 4
# One from each.
query IIIII
SELECT * FROM x x1, x x2, generate_series(x1.a, x2.a) WHERE x1.b = x2.b
----
1 2 1 2 1
2 3 2 3 2
3 4 3 4 3
# Regression test for #3877: a table function as the first FROM item inside of
# a LATERAL subquery should not miscount outer scope depth.
query II
SELECT x.a, generate_series FROM x, LATERAL (SELECT * FROM generate_series(1, x.a))
----
1 1
2 1
2 2
3 1
3 2
3 3
# Regression test for #5520: crash when a filter references an output column of
# a table function
query IIIII
SELECT * FROM x x1, x x2, generate_series(x1.a, x2.b) AS x3(b) WHERE x1.b = x2.b AND x1.a = x3.b
----
1 2 1 2 1
2 3 2 3 2
3 4 3 4 3
query T multiline
EXPLAIN RAW PLAN FOR SELECT * FROM x, generate_series(1, a)
----
%0 =
| Get materialize.public.x (u3)
%1 =
| CallTable generate_series(1, #^0, 1)
%2 =
| InnerJoin %0 %1 on true
EOF
query T multiline
EXPLAIN RAW PLAN FOR SELECT * FROM x, generate_series(100::bigint, a)
----
%0 =
| Get materialize.public.x (u3)
%1 =
| CallTable generate_series(i32toi64(100), i32toi64(#^0), 1)
%2 =
| InnerJoin %0 %1 on true
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM x, generate_series(1, 10)
----
%0 =
| Get materialize.public.x (u3)
| ArrangeBy ()
%1 =
| Constant (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
%2 =
| Join %0 %1
| | implementation = Differential %1 %0.()
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM x, generate_series(1, a)
----
%0 =
| Get materialize.public.x (u3)
| FlatMap generate_series(1, #0, 1)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM x x1, x x2, generate_series(x1.a, x2.a) WHERE x1.b = x2.b
----
%0 = Let l0 =
| Get materialize.public.x (u3)
| Filter !(isnull(#1))
%1 =
| Get %0 (l0)
| ArrangeBy (#1)
%2 =
| Join %1 %0 (= #1 #3)
| | implementation = Differential %0 %1.(#1)
| Project (#0..#2)
| FlatMap generate_series(#0, #2, 1)
| Project (#0..#2, #1, #3)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT * FROM x x1, x x2, generate_series(x1.a, x2.a) WHERE x1.b = x2.b
----
%0 = Let l0 =
| Get materialize.public.x (u3)
| Filter !(isnull(#1))
%1 =
| Get %0 (l0)
| ArrangeBy (#1)
%2 =
| Join %1 %0 (= #1 #3)
| | implementation = Differential %0 %1.(#1)
| Project (#0..#2)
| FlatMap generate_series(#0, #2, 1)
| Project (#0..#2, #1, #3)
EOF
# Regression test for #5520: crash when a filter references an output column of
# a table function around a join
query T multiline
EXPLAIN PLAN FOR SELECT * FROM x x1, x x2, generate_series(x1.a, x2.b) AS x3(b) WHERE x1.b = x2.b AND x1.a = x3.b
----
%0 = Let l0 =
| Get materialize.public.x (u3)
| Filter !(isnull(#1))
%1 =
| Get %0 (l0)
| ArrangeBy (#1)
%2 =
| Join %1 %0 (= #1 #3)
| | implementation = Differential %0 %1.(#1)
| Project (#0..#2)
| FlatMap generate_series(#0, #1, 1)
| Filter (#0 = #3)
| Project (#0..#2, #1, #3)
EOF
query I
SELECT * FROM generate_series(0,3), repeat_row(generate_series);
----
1
2
2
3
3
3
query I
SELECT abs(generate_series) FROM generate_series(-1, 2), repeat_row(generate_series);
----
2
2
statement error Negative multiplicity in constant result: -1
SELECT * FROM (values ('a')), repeat_row(-1)
statement error constant folding encountered reduce on collection with non-positive multiplicities
SELECT (SELECT 1 FROM repeat_row(-1))
query T
SELECT generate_series FROM generate_series(null, null, null)
----
query I
SELECT generate_series FROM generate_series(1, 3, 1)
----
1
2
3
query error step size cannot equal zero
SELECT generate_series FROM generate_series(1, 100, 0)
----
query error step size cannot equal zero
SELECT generate_series FROM generate_series(1::bigint, 100::bigint, 0::bigint)
----
query I
SELECT generate_series FROM generate_series(1, 10, 11)
----
1
query I
SELECT generate_series FROM generate_series(1::bigint, 10::bigint, 11::bigint)
----
1
query I
SELECT generate_series FROM generate_series(3, 1, -1)
----
1
2
3
query I
SELECT generate_series FROM generate_series(3::bigint, 1::bigint, -1::bigint)
----
1
2
3
query I
SELECT generate_series FROM generate_series(1, 10, -1)
----
query I
SELECT generate_series FROM generate_series(1::bigint, 10::bigint, -1::bigint)
----
query I
SELECT generate_series FROM generate_series(2, 4, 2)
----
2
4
query I
SELECT generate_series FROM generate_series(2::bigint, 4::bigint, 2::bigint)
----
2
4
query I
SELECT generate_series FROM generate_series(-2, 2, 3)
----
1
-2
query I
SELECT generate_series FROM generate_series(-2::bigint, 2::bigint, 3::bigint)
----
1
-2
query I
SELECT generate_series FROM generate_series(-2::bigint, 2, 1)
----
0
1
2
-2
-1
query I
SELECT generate_series FROM generate_series(null, 1, 1)
----
query I
SELECT generate_series FROM generate_series(null, 1::bigint, 1::bigint)
----
query I
SELECT generate_series FROM generate_series(1, null, 1)
----
query I
SELECT generate_series FROM generate_series(1::bigint, null, 1::bigint)
----
query T multiline
EXPLAIN RAW PLAN FOR SELECT generate_series FROM generate_series(-2, 2, 1)
----
%0 =
| CallTable generate_series(-2, 2, 1)
EOF
query T multiline
EXPLAIN RAW PLAN FOR SELECT generate_series FROM generate_series(-2::bigint, 2::bigint, 1::bigint)
----
%0 =
| CallTable generate_series(i32toi64(-2), i32toi64(2), i32toi64(1))
EOF
query T multiline
EXPLAIN PLAN FOR SELECT generate_series FROM generate_series(-2, 2, 2)
----
%0 =
| Constant (0) (2) (-2)
EOF
query T multiline
EXPLAIN PLAN FOR SELECT generate_series FROM generate_series(-2::bigint, 2::bigint, 2::bigint)
----
%0 =
| Constant (0) (2) (-2)
EOF
# information_schema._pg_expandarray
query error arguments cannot be implicitly cast
SELECT information_schema._pg_expandarray()
query error arguments cannot be implicitly cast
SELECT * FROM information_schema._pg_expandarray()
query error cannot determine type of empty array
SELECT information_schema._pg_expandarray(ARRAY[])
query error cannot determine type of empty array
SELECT * FROM information_schema._pg_expandarray(ARRAY[])
query error arguments cannot be implicitly cast
SELECT * FROM information_schema._pg_expandarray(NULL)
query error arguments cannot be implicitly cast
SELECT information_schema._pg_expandarray(NULL)
query T colnames
SELECT information_schema._pg_expandarray(ARRAY[]::int[])
----
_pg_expandarray
query TI colnames
SELECT * FROM information_schema._pg_expandarray(ARRAY[]::int[])
----
x n
query T colnames
SELECT information_schema._pg_expandarray(ARRAY[100])
----
_pg_expandarray
(100,1)
query TI
SELECT * FROM information_schema._pg_expandarray(ARRAY[100])
----
100 1
query T
SELECT information_schema._pg_expandarray(ARRAY[2, 1]) ORDER BY 1
----
(1,2)
(2,1)
query II
SELECT * FROM information_schema._pg_expandarray(ARRAY[2, 1]) ORDER BY x
----
1 2
2 1
query T
SELECT information_schema._pg_expandarray(ARRAY[3, 2, 1]) ORDER BY 1
----
(1,3)
(2,2)
(3,1)
query II
SELECT * FROM information_schema._pg_expandarray(ARRAY[3, 2, 1]) ORDER BY x
----
1 3
2 2
3 1
query T
SELECT information_schema._pg_expandarray(ARRAY['a'])
----
(a,1)
query TI
SELECT * FROM information_schema._pg_expandarray(ARRAY['a'])
----
a 1
query T
SELECT information_schema._pg_expandarray(ARRAY['b', 'a']) ORDER BY 1
----
(a,2)
(b,1)
query TI
SELECT * FROM information_schema._pg_expandarray(ARRAY['b', 'a']) ORDER BY x
----
a 2
b 1
query T
SELECT information_schema._pg_expandarray(ARRAY['c', 'b', 'a']) ORDER BY 1
----
(a,3)
(b,2)
(c,1)
query TI
SELECT * FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a']) ORDER BY x
----
a 3
b 2
c 1
# Test table and column naming for table functions that return 1 column.
query T colnames
SELECT generate_series.* FROM generate_series(1, 1)
----
generate_series
1
query T
SELECT generate_series.generate_series FROM generate_series(1, 1)
----
1
query T colnames
SELECT g FROM generate_series(1, 1) AS g
----
g
1
query T
SELECT g.g FROM generate_series(1, 1) AS g
----
1
query T colnames
SELECT g.* FROM generate_series(1, 1) AS g
----
g
1
query T colnames
SELECT g FROM generate_series(1, 1) AS g(a)
----
g
1
query T colnames
SELECT g FROM ROWS FROM (generate_series(1, 1)) AS g(a)
----
g
1
query T colnames
SELECT jsonb_array_elements FROM ROWS FROM (jsonb_array_elements('[1]'), generate_series(1, 1));
----
jsonb_array_elements
(1,1)
query T colnames
SELECT jsonb_array_elements.generate_series FROM ROWS FROM (jsonb_array_elements('[1]'), generate_series(1, 1));
----
generate_series
1
query T colnames
SELECT repeat_row FROM ROWS FROM (repeat_row(2), generate_series(1, 1));
----
repeat_row
()
(1)
query T colnames
SELECT g.a FROM generate_series(1, 1) AS g(a)
----
a
1
query T colnames
SELECT g.* FROM generate_series(1, 1) AS g(a)
----
a
1
statement error column "g.g" does not exist
SELECT g.g FROM generate_series(1, 1) AS g(a)
statement error column "generate_series" does not exist
SELECT generate_series FROM generate_series(1, 1) AS g
statement error column "g.generate_series" does not exist
SELECT g.generate_series FROM generate_series(1, 1) AS g
statement error column "generate_series.g" does not exist
SELECT generate_series.g FROM generate_series(1, 1) AS g
# Test table and column naming for set functions that return more than 1 column.
query T colnames
SELECT g FROM information_schema._pg_expandarray(ARRAY[100]) AS g
----
g
(100,1)
query TT colnames
SELECT _pg_expandarray.* FROM information_schema._pg_expandarray(ARRAY[100])
----
x n
100 1
query T
SELECT _pg_expandarray.x FROM information_schema._pg_expandarray(ARRAY[100])
----
100
query T
SELECT g FROM information_schema._pg_expandarray(ARRAY[100]) AS g
----
(100,1)
query T
SELECT g.x FROM information_schema._pg_expandarray(ARRAY[100]) AS g
----
100
query TT
SELECT g.* FROM information_schema._pg_expandarray(ARRAY[100]) AS g
----
100 1
query T colnames
SELECT information_schema._pg_expandarray(ARRAY['a']) AS x
----
x
(a,1)
# Test aliasing table functions and using named columns of the results.
query T rowsort
SELECT jsonb_array_elements.value->>'a' FROM jsonb_array_elements('[{"a":1},{"a":2},{"a":3}]')
----
1
2
3
query T rowsort
SELECT js.value->>'a' FROM jsonb_array_elements('[{"a":1},{"a":2},{"a":3}]') js
----
1
2
3
# Test more table/set functions in select projections.
statement ok
CREATE TABLE t (i int)
statement ok
INSERT INTO t VALUES (1), (2)
query II colnames
SELECT t.i, g.g FROM t, LATERAL generate_series(3,4) g(g) ORDER BY i, g
----
i g
1 3
1 4
2 3
2 4
# This should be identical to the above.
query II colnames
SELECT t.i, generate_series(3,4) g FROM t ORDER BY i, g
----
i g
1 3
1 4
2 3
2 4
query T colnames
SELECT jsonb_each('{"3":4,"1":2}'::JSONB) ORDER BY 1
----
jsonb_each
(1,2)
(3,4)
query ITT colnames
SELECT 1, jsonb_object_keys('{"1":2,"3":4}'::JSONB), jsonb_object_keys('{"1":2,"3":4,"5":6}'::JSONB) ORDER BY 3
----
?column? jsonb_object_keys jsonb_object_keys
1 1 1
1 3 3
1 NULL 5
query T colnames
SELECT jsonb_build_object(jsonb_object_keys('{"a":2, "b":3}'), 1, 'c', 3) ORDER BY 1
----
jsonb_build_object
{"a":1,"c":3}
{"b":1,"c":3}
query TT
SELECT jsonb_build_object(jsonb_object_keys('{"a":2, "b":3}'), 1, 'c', 3), jsonb_build_object(jsonb_object_keys('{"a":2, "b":3}'), 1, 'c', 3) ORDER BY 1
----
{"a":1,"c":3} {"a":1,"c":3}
{"b":1,"c":3} {"b":1,"c":3}
# TODO: query error argument 1: key must not be null
query TT
SELECT jsonb_build_object(jsonb_object_keys('{"a":2, "b":3}'), 1, 'c', 3), jsonb_build_object(jsonb_object_keys('{"a":2}'), 1, 'c', 3)
----
{"b":1,"c":3} NULL
{"a":1,"c":3} {"a":1,"c":3}
query error table functions are not allowed in other table functions
SELECT jsonb_array_elements(jsonb_array_elements('[[1,2],[3,4]]')), jsonb_array_elements(jsonb_array_elements('[[1],[3,4,5]]')) ORDER BY 1
query error table functions are not allowed in other table functions
SELECT jsonb_array_elements(jsonb_array_elements('[[1,2],[3,4]]')), jsonb_array_elements('[7,8,9]') ORDER BY 1
# Postgres explicitly disallows table funcs (although it uses "set-returning
# functions") in CASE and COALESCE.
query error table functions are not allowed in CASE
SELECT i, CASE WHEN i > 0 THEN generate_series(1, 5) ELSE 0 END FROM t
query error table functions are not allowed in COALESCE
SELECT COALESCE(1, generate_series(1, 1))
query error table functions are not allowed in aggregate function calls
SELECT array_agg(generate_series(1, 2))
# Subqueries avoid the CASE errors.
query I
SELECT COALESCE(1, (SELECT generate_series(1, 1)))
----
1
query error table functions are not allowed in WHERE clause
SELECT 1 WHERE generate_series(1, 1)
# timestamp-based generate series
query T
SELECT * FROM generate_series('2021-01-01 00:00:00'::TIMESTAMP, '2021-01-01 02:00:00'::TIMESTAMP, '1 hour') ORDER BY 1
----
2021-01-01 00:00:00
2021-01-01 01:00:00
2021-01-01 02:00:00
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMP, '2021-01-01 00:00:00'::TIMESTAMP, '-1 hour') ORDER BY 1
----
2021-01-01 00:00:00
2021-01-01 01:00:00
2021-01-01 02:00:00
2021-01-01 03:00:00
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMP, '2021-01-03 00:00:00'::TIMESTAMP, '1 day') ORDER BY 1
----
2021-01-01 03:00:00
2021-01-02 03:00:00
query T
SELECT * FROM generate_series('2021-01-03 00:00:00'::TIMESTAMP, '2021-01-01 03:00:00'::TIMESTAMP, '1 day') ORDER BY 1
----
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMP, '2021-01-03 00:00:00'::TIMESTAMP, '-1 day') ORDER BY 1
----
query T
SELECT * FROM generate_series('2021-01-31 03:00:00'::TIMESTAMP, '2022-01-31 00:00:00'::TIMESTAMP, '1 month') ORDER BY 1
----
2021-01-31 03:00:00
2021-02-28 03:00:00
2021-03-28 03:00:00
2021-04-28 03:00:00
2021-05-28 03:00:00
2021-06-28 03:00:00
2021-07-28 03:00:00
2021-08-28 03:00:00
2021-09-28 03:00:00
2021-10-28 03:00:00
2021-11-28 03:00:00
2021-12-28 03:00:00
2022-01-28 03:00:00
query T
SELECT * FROM generate_series('2021-01-31 03:00:00'::TIMESTAMP, '2021-04-30 00:00:00'::TIMESTAMP, '1 month') ORDER BY 1
----
2021-01-31 03:00:00
2021-02-28 03:00:00
2021-03-28 03:00:00
2021-04-28 03:00:00
# Leap years
query T
SELECT * FROM generate_series('2020-01-31 03:00:00'::TIMESTAMP, '2020-04-30 00:00:00'::TIMESTAMP, '1 month') ORDER BY 1
----
2020-01-31 03:00:00
2020-02-29 03:00:00
2020-03-29 03:00:00
2020-04-29 03:00:00
query T
SELECT * FROM generate_series('2020-01-31 03:00:00'::TIMESTAMP, '2020-11-30 00:00:00'::TIMESTAMP, '2 month') ORDER BY 1
----
2020-01-31 03:00:00
2020-03-31 03:00:00
2020-05-31 03:00:00
2020-07-31 03:00:00
2020-09-30 03:00:00
query T
SELECT * FROM generate_series('2020-01-01 22:00:00'::TIMESTAMP, '2021-01-01 00:00:00'::TIMESTAMP, '1 month 1 day 1 hour') ORDER BY 1
----
2020-01-01 22:00:00
2020-02-02 23:00:00
2020-03-04 00:00:00
2020-04-05 01:00:00
2020-05-06 02:00:00
2020-06-07 03:00:00
2020-07-08 04:00:00
2020-08-09 05:00:00
2020-09-10 06:00:00
2020-10-11 07:00:00
2020-11-12 08:00:00
2020-12-13 09:00:00
query error step size cannot equal zero
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMP, '2021-01-03 00:00:00'::TIMESTAMP, '0 day');
# timestamptz-based generate series
query T
SELECT * FROM generate_series('2021-01-01 00:00:00'::TIMESTAMPTZ, '2021-01-01 02:00:00'::TIMESTAMPTZ, '1 hour') ORDER BY 1
----
2021-01-01 00:00:00+00
2021-01-01 01:00:00+00
2021-01-01 02:00:00+00
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMPTZ, '2021-01-01 00:00:00'::TIMESTAMPTZ, '-1 hour') ORDER BY 1
----
2021-01-01 00:00:00+00
2021-01-01 01:00:00+00
2021-01-01 02:00:00+00
2021-01-01 03:00:00+00
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMPTZ, '2021-01-03 00:00:00'::TIMESTAMPTZ, '1 day') ORDER BY 1
----
2021-01-01 03:00:00+00
2021-01-02 03:00:00+00
query T
SELECT * FROM generate_series('2021-01-03 00:00:00'::TIMESTAMPTZ, '2021-01-01 03:00:00'::TIMESTAMPTZ, '1 day') ORDER BY 1
----
query T
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMPTZ, '2021-01-03 00:00:00'::TIMESTAMPTZ, '-1 day') ORDER BY 1
----
query T
SELECT * FROM generate_series('2021-01-31 03:00:00'::TIMESTAMPTZ, '2022-01-31 00:00:00'::TIMESTAMPTZ, '1 month') ORDER BY 1
----
2021-01-31 03:00:00+00
2021-02-28 03:00:00+00
2021-03-28 03:00:00+00
2021-04-28 03:00:00+00
2021-05-28 03:00:00+00
2021-06-28 03:00:00+00
2021-07-28 03:00:00+00
2021-08-28 03:00:00+00
2021-09-28 03:00:00+00
2021-10-28 03:00:00+00
2021-11-28 03:00:00+00
2021-12-28 03:00:00+00
2022-01-28 03:00:00+00
query T
SELECT * FROM generate_series('2021-01-31 03:00:00'::TIMESTAMPTZ, '2021-04-30 00:00:00'::TIMESTAMPTZ, '1 month') ORDER BY 1
----
2021-01-31 03:00:00+00
2021-02-28 03:00:00+00
2021-03-28 03:00:00+00
2021-04-28 03:00:00+00
# Leap years
query T
SELECT * FROM generate_series('2020-01-31 03:00:00'::TIMESTAMPTZ, '2020-04-30 00:00:00'::TIMESTAMPTZ, '1 month') ORDER BY 1
----
2020-01-31 03:00:00+00
2020-02-29 03:00:00+00
2020-03-29 03:00:00+00
2020-04-29 03:00:00+00
query T
SELECT * FROM generate_series('2020-01-31 03:00:00'::TIMESTAMPTZ, '2020-11-30 00:00:00'::TIMESTAMPTZ, '2 month') ORDER BY 1
----
2020-01-31 03:00:00+00
2020-03-31 03:00:00+00
2020-05-31 03:00:00+00
2020-07-31 03:00:00+00
2020-09-30 03:00:00+00
query T
SELECT * FROM generate_series('2020-01-01 22:00:00'::TIMESTAMPTZ, '2021-01-01 00:00:00'::TIMESTAMPTZ, '1 month 1 day 1 hour') ORDER BY 1
----
2020-01-01 22:00:00+00
2020-02-02 23:00:00+00
2020-03-04 00:00:00+00
2020-04-05 01:00:00+00
2020-05-06 02:00:00+00
2020-06-07 03:00:00+00
2020-07-08 04:00:00+00
2020-08-09 05:00:00+00
2020-09-10 06:00:00+00
2020-10-11 07:00:00+00
2020-11-12 08:00:00+00
2020-12-13 09:00:00+00
query error step size cannot equal zero
SELECT * FROM generate_series('2021-01-01 03:00:00'::TIMESTAMPTZ, '2021-01-03 00:00:00'::TIMESTAMPTZ, '0 day');
# Test ROWS FROM
query I colnames
SELECT * FROM LATERAL ROWS FROM (generate_series(1,1));
----
generate_series
1
query II colnames
SELECT * FROM LATERAL ROWS FROM (generate_series(1, 2), generate_series(3, 6)) ORDER BY 2;
----
generate_series generate_series
1 3
2 4
NULL 5
NULL 6
query III colnames
SELECT * FROM LATERAL ROWS FROM (generate_series(1, 2), generate_series(3, 6)) WITH ORDINALITY ORDER BY 3;
----
generate_series generate_series ordinality
1 3 1
2 4 2
NULL 5 3
NULL 6 4
query IIII colnames
SELECT * FROM ROWS FROM (generate_series(1, 3), generate_series(1, 6), generate_series(1, 9), generate_series(1, 12)) ORDER BY 4;
----
generate_series generate_series generate_series generate_series
1 1 1 1