Skip to content
Permalink
Browse files
Add access operator (->, ->>) to Agtype (#212)
* Add access operator (`->`, `->>`) to Agtype

Added the `->`, `->>` operator to agtype for approaching like Json(b).
  • Loading branch information
emotionbug committed May 6, 2022
1 parent 379983b commit 08d75d85d571a36ff6175b5cf5ae8468b1a164c0
Showing 4 changed files with 327 additions and 43 deletions.
@@ -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 @> <@
--
@@ -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
--
@@ -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
--

0 comments on commit 08d75d8

Please sign in to comment.