Skip to content

Commit

Permalink
Adds tests for RECORD
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySievert committed Nov 11, 2024
1 parent e3c5c80 commit 4a618be
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
51 changes: 51 additions & 0 deletions expected/function.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,54 @@ SELECT polymorphic(ARRAY[10, 11]), polymorphic(ARRAY['foo', 'bar']);
10 | foo
(1 row)

-- RECORD TYPES
CREATE TYPE rec AS (i integer, t text);
CREATE FUNCTION scalar_to_record(i integer, t text) RETURNS rec AS
$$
return { "i": i, "t": t };
$$
LANGUAGE pljs;
SELECT scalar_to_record(1, 'a');
scalar_to_record
------------------
(1,a)
(1 row)

CREATE FUNCTION record_to_text(x rec) RETURNS text AS
$$
return JSON.stringify(x);
$$
LANGUAGE pljs;
SELECT record_to_text('(1,a)'::rec);
record_to_text
-----------------
{"i":1,"t":"a"}
(1 row)

CREATE FUNCTION return_record(i integer, t text) RETURNS record AS
$$
return { "i": i, "t": t };
$$
LANGUAGE pljs;
SELECT * FROM return_record(1, 'a');
ERROR: a column definition list is required for functions returning "record"
LINE 1: SELECT * FROM return_record(1, 'a');
^
SELECT * FROM return_record(1, 'a') AS t(j integer, s text);
j | s
---+---
|
(1 row)

SELECT * FROM return_record(1, 'a') AS t(x text, y text);
x | y
---+---
|
(1 row)

SELECT * FROM return_record(1, 'a') AS t(i integer, t text);
i | t
---+---
1 | a
(1 row)

26 changes: 26 additions & 0 deletions sql/function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,29 @@ $$
$$
LANGUAGE pljs;
SELECT polymorphic(ARRAY[10, 11]), polymorphic(ARRAY['foo', 'bar']);

-- RECORD TYPES
CREATE TYPE rec AS (i integer, t text);
CREATE FUNCTION scalar_to_record(i integer, t text) RETURNS rec AS
$$
return { "i": i, "t": t };
$$
LANGUAGE pljs;
SELECT scalar_to_record(1, 'a');

CREATE FUNCTION record_to_text(x rec) RETURNS text AS
$$
return JSON.stringify(x);
$$
LANGUAGE pljs;
SELECT record_to_text('(1,a)'::rec);

CREATE FUNCTION return_record(i integer, t text) RETURNS record AS
$$
return { "i": i, "t": t };
$$
LANGUAGE pljs;
SELECT * FROM return_record(1, 'a');
SELECT * FROM return_record(1, 'a') AS t(j integer, s text);
SELECT * FROM return_record(1, 'a') AS t(x text, y text);
SELECT * FROM return_record(1, 'a') AS t(i integer, t text);

0 comments on commit 4a618be

Please sign in to comment.