| -- ----------------------------------------------------------------- |
| -- Test function variadic parameters |
| -- |
| -- PostgreSQL has tests for variadic parameters in 'polymorphism' |
| -- and 'plpgsql' tests. This file contains a few extra ones. |
| -- ----------------------------------------------------------------- |
| -- deparse view |
| create or replace function concat(text, variadic anyarray) returns text as $$ |
| select array_to_string($2, $1); |
| $$ language sql immutable strict; |
| create table people (id int, fname varchar, lname varchar); |
| NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. |
| HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. |
| insert into people values (770,'John','Woo'); |
| insert into people values (771,'Jim','Ng'); |
| insert into people values (778,'Jerry','Lau'); |
| insert into people values (790,'Jen','Smith'); |
| create view print_name as select concat(' ', fname, lname) from people where id < 790; |
| select * from print_name; |
| concat |
| ----------- |
| John Woo |
| Jerry Lau |
| Jim Ng |
| (3 rows) |
| |
| select pg_get_viewdef('print_name'); |
| pg_get_viewdef |
| --------------------------------------------------------------------------------- |
| SELECT concat(' '::text, VARIADIC ARRAY[people.fname, people.lname]) AS concat+ |
| FROM people + |
| WHERE (people.id < 790); |
| (1 row) |
| |
| select pg_get_viewdef('print_name', true); |
| pg_get_viewdef |
| --------------------------------------------------------------------------------- |
| SELECT concat(' '::text, VARIADIC ARRAY[people.fname, people.lname]) AS concat+ |
| FROM people + |
| WHERE people.id < 790; |
| (1 row) |
| |
| drop view print_name; |
| drop function concat(text, anyarray); |
| -- PLPGSQL |
| -- table function |
| create or replace function tfunc(variadic char[]) returns table (id int, tx char) as |
| $$ select id, unnest($1) || ', ' || lname || '.' || fname from people order by 2 |
| $$ language sql strict; |
| select * from tfunc ('hello', 'morning'); |
| id | tx |
| -----+-------------------- |
| 778 | hello, Lau.Jerry |
| 771 | hello, Ng.Jim |
| 790 | hello, Smith.Jen |
| 770 | hello, Woo.John |
| 778 | morning, Lau.Jerry |
| 771 | morning, Ng.Jim |
| 790 | morning, Smith.Jen |
| 770 | morning, Woo.John |
| (8 rows) |
| |
| drop table people; |
| drop function tfunc(variadic char[]); |
| set optimizer_trace_fallback to on; |
| drop table if exists foo; |
| NOTICE: table "foo" does not exist, skipping |
| create table foo (a int,b int, c_json json); |
| NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Apache Cloudberry data distribution key for this table. |
| HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. |
| insert into foo values (1,1,'{"1":"10"}'); |
| insert into foo values (2,2,'{"2":"20"}'); |
| insert into foo values (3,3,'{"3":"30"}'); |
| select *, (json_extract_path_text(c_json, '1')) AS jsonValues from foo; |
| a | b | c_json | jsonvalues |
| ---+---+------------+------------ |
| 2 | 2 | {"2":"20"} | |
| 3 | 3 | {"3":"30"} | |
| 1 | 1 | {"1":"10"} | 10 |
| (3 rows) |
| |
| select json_build_array(VARIADIC '{a,b,c}'::text[]); |
| json_build_array |
| ------------------ |
| ["a", "b", "c"] |
| (1 row) |
| |
| drop table if exists foo; |
| set optimizer_trace_fallback to off; |