Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add access operator (->, ->>) to Agtype #212

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions age--1.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,70 @@ CREATE OPERATOR CLASS agtype_ops_hash
OPERATOR 1 =,
FUNCTION 1 ag_catalog.agtype_hash_cmp(agtype);

--
-- agtype - access operators ( ->, ->> )
--

CREATE FUNCTION ag_catalog.agtype_object_field(agtype, text)
RETURNS agtype
LANGUAGE c
IMMUTABLE
RETURNS NULL ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';

-- get agtype object field
CREATE OPERATOR -> (
LEFTARG = agtype,
RIGHTARG = text,
FUNCTION = ag_catalog.agtype_object_field
);

CREATE FUNCTION ag_catalog.agtype_object_field_text(agtype, text)
RETURNS text
LANGUAGE c
IMMUTABLE
RETURNS NULL ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';

-- get agtype object field as text
CREATE OPERATOR ->> (
LEFTARG = agtype,
RIGHTARG = text,
FUNCTION = ag_catalog.agtype_object_field_text
);

CREATE FUNCTION ag_catalog.agtype_array_element(agtype, int4)
RETURNS agtype
LANGUAGE c
IMMUTABLE
RETURNS NULL ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';

-- get agtype array element
CREATE OPERATOR -> (
LEFTARG = agtype,
RIGHTARG = int4,
FUNCTION = ag_catalog.agtype_array_element
);

CREATE FUNCTION ag_catalog.agtype_array_element_text(agtype, int4)
RETURNS text
LANGUAGE c
IMMUTABLE
RETURNS NULL ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';

-- get agtype array element as text
CREATE OPERATOR ->> (
LEFTARG = agtype,
RIGHTARG = int4,
FUNCTION = ag_catalog.agtype_array_element_text
);

--
-- Contains operators @> <@
--
Expand Down
60 changes: 60 additions & 0 deletions regress/expected/agtype.out
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,66 @@ SELECT agtype_access_operator('{"bool":false, "int":3, "float":3.14}', '2');
ERROR: AGTV_INTEGER is not a valid key type
SELECT agtype_access_operator('{"bool":false, "int":3, "float":3.14}', '2.0');
ERROR: AGTV_FLOAT is not a valid key type
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":true, "array":[1,3,{"bool":false, "int":3, "float":3.14},7], "float":3.14}'::agtype->'array'->2->'float' as i) a;
i | pg_typeof
------+-----------
3.14 | agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":true, "array":[1,3,{"bool":false, "int":3, "float":3.14},7], "float":3.14}'::agtype->'array'->2->>'float' as i) a;
i | pg_typeof
------+-----------
3.14 | text
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{}'::agtype->'array' as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '[]'::agtype->0 as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '[0, 1]'::agtype->2 as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '[0, 1]'::agtype->3 as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true' as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->2 as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true'->2 as i) a;
i | pg_typeof
---+-----------
| agtype
(1 row)

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true'->>2 as i) a;
i | pg_typeof
---+-----------
| text
(1 row)

--
-- Vertex
--
Expand Down
11 changes: 11 additions & 0 deletions regress/sql/agtype.sql
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ SELECT agtype_access_operator('{"bool":false, "int":3, "float":3.14}', 'true');
SELECT agtype_access_operator('{"bool":false, "int":3, "float":3.14}', '2');
SELECT agtype_access_operator('{"bool":false, "int":3, "float":3.14}', '2.0');

SELECT i, pg_typeof(i) FROM (SELECT '{"bool":true, "array":[1,3,{"bool":false, "int":3, "float":3.14},7], "float":3.14}'::agtype->'array'->2->'float' as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":true, "array":[1,3,{"bool":false, "int":3, "float":3.14},7], "float":3.14}'::agtype->'array'->2->>'float' as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{}'::agtype->'array' as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '[]'::agtype->0 as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '[0, 1]'::agtype->2 as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '[0, 1]'::agtype->3 as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true' as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->2 as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true'->2 as i) a;
SELECT i, pg_typeof(i) FROM (SELECT '{"bool":false, "int":3, "float":3.14}'::agtype->'true'->>2 as i) a;

--
-- Vertex
--
Expand Down
Loading