Add test for Set Returning Functions
authorPallavi Sontakke <[email protected]>
Fri, 14 Sep 2018 07:18:32 +0000 (12:48 +0530)
committerPavan Deolasee <[email protected]>
Wed, 19 Sep 2018 05:00:11 +0000 (10:30 +0530)
Fixes #204

src/test/regress/expected/xl_set_returning_functions.out [new file with mode: 0644]
src/test/regress/parallel_schedule
src/test/regress/serial_schedule
src/test/regress/sql/xl_set_returning_functions.sql [new file with mode: 0644]

diff --git a/src/test/regress/expected/xl_set_returning_functions.out b/src/test/regress/expected/xl_set_returning_functions.out
new file mode 100644 (file)
index 0000000..9b8d81d
--- /dev/null
@@ -0,0 +1,65 @@
+create schema if not exists xl_srf;
+create table xl_srf.allsets(data jsonb);
+create table xl_srf.cards
+          as
+      select jsonb_array_elements(value->'cards') as data
+        from xl_srf.allsets, jsonb_each(data);
+create index on xl_srf.cards using gin(data jsonb_path_ops);
+begin;
+create table xl_srf.sets
+    as
+select key as name, value - 'cards' as data
+  from xl_srf.allsets, jsonb_each(data);
+drop table xl_srf.cards;
+create table xl_srf.cards
+    as
+  with collection as
+  (
+     select key as set,
+            value->'cards' as data
+       from xl_srf.allsets,
+            lateral jsonb_each(data)
+  )
+  select set, jsonb_array_elements(data) as data
+    from collection;
+commit;
+Insert into xl_srf.cards values ('a', '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
+Insert into xl_srf.cards values ('b', '{"a":[["b",{"x":2}],["b",{"x":3}]],"c":4}');
+Insert into xl_srf.cards values ('c', '{"a":[["b",{"x":3}],["b",{"x":4}]],"c":5}');
+select case jsonb_typeof(booster)
+              when 'array'
+              then initcap(jsonb_array_elements_text(booster))
+              else initcap(booster #>> '{}')
+          end
+         as rarity,
+         count(*)
+    from xl_srf.sets,
+         jsonb_array_elements(data->'booster') booster
+group by rarity
+order by count desc;
+ERROR:  set-returning functions are not allowed in CASE
+LINE 3:               then initcap(jsonb_array_elements_text(booster...
+                                   ^
+HINT:  You might be able to move the set-returning function into a LATERAL FROM item.
+with booster(rarity_js) as (
+  select case jsonb_typeof(booster)
+              when 'array'
+              then booster
+              else jsonb_build_array(booster)
+          end
+    from xl_srf.sets,
+         jsonb_array_elements(data->'booster') as booster
+)
+  select initcap(rarity) as rarity, count(*)
+    from booster,
+         jsonb_array_elements_text(rarity_js) as t(rarity)
+group by rarity
+order by count desc;
+ rarity | count 
+--------+-------
+(0 rows)
+
+drop table xl_srf.cards;
+drop table xl_srf.sets;
+drop table xl_srf.allsets;
+drop schema xl_srf;
index c532598ffc8801418dc2f5da6774aff794216ba3..a76c2a23cc682df8d5abd73c02042b5f8e69fe88 100644 (file)
@@ -157,3 +157,5 @@ test: xl_misc xl_primary_key xl_foreign_key xl_distribution_column_types xl_alte
 # results. So it should not run parallel to other tests.
 # ----------
 test: xl_sanity_check
+
+test: xl_set_returning_functions
index c0156d8584e539e052f23a9a624e1631b842bad3..cc6604250e3503a9ac56accd1bc38e3e9686f7ad 100644 (file)
@@ -208,3 +208,4 @@ test: xl_user_defined_functions
 test: xl_join
 test: xl_distributed_xact
 test: xl_create_table
+test: xl_set_returning_functions
diff --git a/src/test/regress/sql/xl_set_returning_functions.sql b/src/test/regress/sql/xl_set_returning_functions.sql
new file mode 100644 (file)
index 0000000..875bd71
--- /dev/null
@@ -0,0 +1,72 @@
+
+create schema if not exists xl_srf;
+create table xl_srf.allsets(data jsonb);
+
+create table xl_srf.cards
+          as
+      select jsonb_array_elements(value->'cards') as data
+        from xl_srf.allsets, jsonb_each(data);
+
+create index on xl_srf.cards using gin(data jsonb_path_ops);
+
+
+begin;
+
+create table xl_srf.sets
+    as
+select key as name, value - 'cards' as data
+  from xl_srf.allsets, jsonb_each(data);
+
+drop table xl_srf.cards;
+
+create table xl_srf.cards
+    as
+  with collection as
+  (
+     select key as set,
+            value->'cards' as data
+       from xl_srf.allsets,
+            lateral jsonb_each(data)
+  )
+  select set, jsonb_array_elements(data) as data
+    from collection;
+
+commit;
+
+Insert into xl_srf.cards values ('a', '{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
+Insert into xl_srf.cards values ('b', '{"a":[["b",{"x":2}],["b",{"x":3}]],"c":4}');
+Insert into xl_srf.cards values ('c', '{"a":[["b",{"x":3}],["b",{"x":4}]],"c":5}');
+
+
+select case jsonb_typeof(booster)
+              when 'array'
+              then initcap(jsonb_array_elements_text(booster))
+              else initcap(booster #>> '{}')
+          end
+         as rarity,
+         count(*)
+    from xl_srf.sets,
+         jsonb_array_elements(data->'booster') booster
+group by rarity
+order by count desc;
+
+
+with booster(rarity_js) as (
+  select case jsonb_typeof(booster)
+              when 'array'
+              then booster
+              else jsonb_build_array(booster)
+          end
+    from xl_srf.sets,
+         jsonb_array_elements(data->'booster') as booster
+)
+  select initcap(rarity) as rarity, count(*)
+    from booster,
+         jsonb_array_elements_text(rarity_js) as t(rarity)
+group by rarity
+order by count desc;
+
+drop table xl_srf.cards;
+drop table xl_srf.sets;
+drop table xl_srf.allsets;
+drop schema xl_srf;