Skip to content

Commit 27f7011

Browse files
committed
Allow agtype_build_map to use AGTYPE keys (apache#1666)
Added the ability for agtype_build_map to use AGTYPE keys. In doing this, age_tostring needed to be broken up and rewritten to handle UNKNOWNOID. This rewrite allows other functions to use the logic in age_tostring without using age_tostring directly. Additionally, rewrote age_tostring as a ("any") type function; it used to be a (variadic "any"). The logic here can be used to re- write other (variadic "any") functions that have a set number of input parameters. Added regression tests.
1 parent 7728ff0 commit 27f7011

File tree

5 files changed

+170
-26
lines changed

5 files changed

+170
-26
lines changed

age--1.5.0--y.y.y.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,13 @@ DEFAULT FOR TYPE agtype USING gin AS
107107
FUNCTION 6 ag_catalog.gin_triconsistent_agtype(internal, int2, agtype, int4,
108108
internal, internal, internal),
109109
STORAGE text;
110+
111+
-- this function went from variadic "any" to just "any" type
112+
CREATE OR REPLACE FUNCTION ag_catalog.age_tostring("any")
113+
RETURNS agtype
114+
LANGUAGE c
115+
IMMUTABLE
116+
RETURNS NULL ON NULL INPUT
117+
PARALLEL SAFE
118+
AS 'MODULE_PATHNAME';
119+

regress/expected/agtype.out

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,7 @@ SELECT agtype_to_int4_array(NULL);
31393139
--
31403140
--Invalid Map Key (should fail)
31413141
SELECT agtype_build_map('[0]'::agtype, null);
3142-
ERROR: key value must be scalar, not array, composite, or json
3142+
ERROR: agtype_build_map_as_agtype_value only supports scalar arguments
31433143
--
31443144
-- Test agtype object/array access operators object.property, object["property"], and array[element]
31453145
-- Note: At this point, object.property and object["property"] are equivalent.
@@ -3907,9 +3907,51 @@ SELECT ag_catalog.agtype_volatile_wrapper(32768::int2);
39073907
ERROR: smallint out of range
39083908
SELECT ag_catalog.agtype_volatile_wrapper(-32768::int2);
39093909
ERROR: smallint out of range
3910+
--
3911+
-- test that age_tostring can handle an UNKNOWNOID type
3912+
--
3913+
SELECT age_tostring('a');
3914+
age_tostring
3915+
--------------
3916+
"a"
3917+
(1 row)
3918+
3919+
--
3920+
-- test agtype_build_map_as_agtype_value via agtype_build_map
3921+
--
3922+
SELECT * FROM create_graph('agtype_build_map');
3923+
NOTICE: graph "agtype_build_map" has been created
3924+
create_graph
3925+
--------------
3926+
3927+
(1 row)
3928+
3929+
SELECT * FROM cypher('agtype_build_map', $$ RETURN ag_catalog.agtype_build_map('1', '1', 2, 2, 3.14, 3.14, 'e', 2.71)
3930+
$$) AS (results agtype);
3931+
results
3932+
---------------------------------------------
3933+
{"1": "1", "2": 2, "e": 2.71, "3.14": 3.14}
3934+
(1 row)
3935+
3936+
SELECT agtype_build_map('1', '1', 2, 2, 3.14, 3.14, 'e', 2.71);
3937+
agtype_build_map
3938+
---------------------------------------------------------------
3939+
{"1": "1", "2": 2, "e": 2.71::numeric, "3.14": 3.14::numeric}
3940+
(1 row)
3941+
39103942
--
39113943
-- Cleanup
39123944
--
3945+
SELECT drop_graph('agtype_build_map', true);
3946+
NOTICE: drop cascades to 2 other objects
3947+
DETAIL: drop cascades to table agtype_build_map._ag_label_vertex
3948+
drop cascades to table agtype_build_map._ag_label_edge
3949+
NOTICE: graph "agtype_build_map" has been dropped
3950+
drop_graph
3951+
------------
3952+
3953+
(1 row)
3954+
39133955
DROP TABLE agtype_table;
39143956
--
39153957
-- End of AGTYPE data type regression tests

regress/sql/agtype.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,25 @@ SELECT ag_catalog.agtype_volatile_wrapper(-32767::int2);
10981098
-- These should fail
10991099
SELECT ag_catalog.agtype_volatile_wrapper(32768::int2);
11001100
SELECT ag_catalog.agtype_volatile_wrapper(-32768::int2);
1101+
1102+
--
1103+
-- test that age_tostring can handle an UNKNOWNOID type
1104+
--
1105+
SELECT age_tostring('a');
1106+
1107+
--
1108+
-- test agtype_build_map_as_agtype_value via agtype_build_map
1109+
--
1110+
SELECT * FROM create_graph('agtype_build_map');
1111+
SELECT * FROM cypher('agtype_build_map', $$ RETURN ag_catalog.agtype_build_map('1', '1', 2, 2, 3.14, 3.14, 'e', 2.71)
1112+
$$) AS (results agtype);
1113+
SELECT agtype_build_map('1', '1', 2, 2, 3.14, 3.14, 'e', 2.71);
1114+
11011115
--
11021116
-- Cleanup
11031117
--
1118+
SELECT drop_graph('agtype_build_map', true);
1119+
11041120
DROP TABLE agtype_table;
11051121

11061122
--

sql/age_scalar.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ RETURNS NULL ON NULL INPUT
149149
PARALLEL SAFE
150150
AS 'MODULE_PATHNAME';
151151

152-
CREATE FUNCTION ag_catalog.age_tostring(variadic "any")
152+
CREATE FUNCTION ag_catalog.age_tostring("any")
153153
RETURNS agtype
154154
LANGUAGE c
155155
IMMUTABLE

src/backend/utils/adt/agtype.c

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ static int extract_variadic_args_min(FunctionCallInfo fcinfo,
174174
int min_num_args);
175175
static agtype_value *agtype_build_map_as_agtype_value(FunctionCallInfo fcinfo);
176176
agtype_value *agtype_composite_to_agtype_value_binary(agtype *a);
177+
static agtype_value *tostring_helper(Datum arg, Oid type, char *msghdr);
177178

178179
/* global storage of OID for agtype and _agtype */
179180
static Oid g_AGTYPEOID = InvalidOid;
@@ -2377,6 +2378,7 @@ static agtype_value *agtype_build_map_as_agtype_value(FunctionCallInfo fcinfo)
23772378
result.res = push_agtype_value(&result.parse_state, WAGT_BEGIN_OBJECT,
23782379
NULL);
23792380

2381+
/* iterate through the arguments and build the object */
23802382
for (i = 0; i < nargs; i += 2)
23812383
{
23822384
/* process key */
@@ -2387,7 +2389,25 @@ static agtype_value *agtype_build_map_as_agtype_value(FunctionCallInfo fcinfo)
23872389
errmsg("argument %d: key must not be null", i + 1)));
23882390
}
23892391

2390-
add_agtype(args[i], false, &result, types[i], true);
2392+
/*
2393+
* If the key is agtype, we need to extract it as an agtype string and
2394+
* push the value.
2395+
*/
2396+
if (types[i] == AGTYPEOID)
2397+
{
2398+
agtype_value *agtv = NULL;
2399+
2400+
agtv = tostring_helper(args[i], types[i],
2401+
"agtype_build_map_as_agtype_value");
2402+
result.res = push_agtype_value(&result.parse_state, WAGT_KEY, agtv);
2403+
2404+
/* free the agtype_value from tostring_helper */
2405+
pfree(agtv);
2406+
}
2407+
else
2408+
{
2409+
add_agtype(args[i], false, &result, types[i], true);
2410+
}
23912411

23922412
/* process value */
23932413
add_agtype(args[i + 1], nulls[i + 1], &result, types[i + 1], false);
@@ -6734,16 +6754,12 @@ PG_FUNCTION_INFO_V1(age_tostring);
67346754
Datum age_tostring(PG_FUNCTION_ARGS)
67356755
{
67366756
int nargs;
6737-
Datum *args;
67386757
Datum arg;
6739-
bool *nulls;
6740-
Oid *types;
6741-
agtype_value agtv_result;
6742-
char *string = NULL;
6743-
Oid type;
6758+
Oid type = InvalidOid;
6759+
agtype *agt = NULL;
6760+
agtype_value *agtv = NULL;
67446761

6745-
/* extract argument values */
6746-
nargs = extract_variadic_args(fcinfo, 0, true, &args, &types, &nulls);
6762+
nargs = PG_NARGS();
67476763

67486764
/* check number of args */
67496765
if (nargs > 1)
@@ -6753,19 +6769,70 @@ Datum age_tostring(PG_FUNCTION_ARGS)
67536769
}
67546770

67556771
/* check for null */
6756-
if (nargs < 0 || nulls[0])
6772+
if (nargs < 1 || PG_ARGISNULL(0))
67576773
{
67586774
PG_RETURN_NULL();
67596775
}
67606776

6777+
/* get the argument and type */
6778+
arg = PG_GETARG_DATUM(0);
6779+
type = get_fn_expr_argtype(fcinfo->flinfo, 0);
6780+
6781+
/* verify that if the type is UNKNOWNOID it can be converted */
6782+
if (type == UNKNOWNOID && !get_fn_expr_arg_stable(fcinfo->flinfo, 0))
6783+
{
6784+
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6785+
errmsg("toString() UNKNOWNOID and not stable")));
6786+
}
6787+
67616788
/*
67626789
* toString() supports integer, float, numeric, text, cstring, boolean,
67636790
* regtype or the agtypes: integer, float, numeric, string, boolean input
67646791
*/
6765-
arg = args[0];
6766-
type = types[0];
6792+
agtv = tostring_helper(arg, type, "toString()");
67676793

6768-
if (type != AGTYPEOID)
6794+
/* if we get a NULL back we need to return NULL */
6795+
if (agtv == NULL)
6796+
{
6797+
PG_RETURN_NULL();
6798+
}
6799+
6800+
/* convert to agtype and free the agtype_value */
6801+
agt = agtype_value_to_agtype(agtv);
6802+
pfree(agtv);
6803+
6804+
PG_RETURN_POINTER(agt);
6805+
}
6806+
6807+
/*
6808+
* Helper function to take any valid type and convert it to an agtype string.
6809+
* Returns NULL for NULL output.
6810+
*/
6811+
static agtype_value *tostring_helper(Datum arg, Oid type, char *msghdr)
6812+
{
6813+
agtype_value *agtv_result = NULL;
6814+
char *string = NULL;
6815+
6816+
agtv_result = palloc0(sizeof(agtype_value));
6817+
6818+
/*
6819+
* toString() supports: unknown, integer, float, numeric, text, cstring,
6820+
* boolean, regtype or the agtypes: integer, float, numeric, string, and
6821+
* boolean input.
6822+
*/
6823+
6824+
/*
6825+
* If the type is UNKNOWNOID convert it to a cstring. Prior to passing an
6826+
* UNKNOWNOID it should be verified to be stable.
6827+
*/
6828+
if (type == UNKNOWNOID)
6829+
{
6830+
char *str = DatumGetPointer(arg);
6831+
6832+
string = pnstrdup(str, strlen(str));
6833+
}
6834+
/* if it is not an AGTYPEOID */
6835+
else if (type != AGTYPEOID)
67696836
{
67706837
if (type == INT2OID)
67716838
{
@@ -6812,11 +6879,12 @@ Datum age_tostring(PG_FUNCTION_ARGS)
68126879
else
68136880
{
68146881
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6815-
errmsg("toString() unsupported argument type %d",
6816-
type)));
6882+
errmsg("%s unsupported argument type %d",
6883+
msghdr, type)));
68176884
}
68186885
}
6819-
else
6886+
/* if it is an AGTYPEOID */
6887+
else if (type == AGTYPEOID)
68206888
{
68216889
agtype *agt_arg;
68226890
agtype_value *agtv_value;
@@ -6827,14 +6895,15 @@ Datum age_tostring(PG_FUNCTION_ARGS)
68276895
if (!AGT_ROOT_IS_SCALAR(agt_arg))
68286896
{
68296897
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6830-
errmsg("toString() only supports scalar arguments")));
6898+
errmsg("%s only supports scalar arguments",
6899+
msghdr)));
68316900
}
68326901

68336902
agtv_value = get_ith_agtype_value_from_container(&agt_arg->root, 0);
68346903

68356904
if (agtv_value->type == AGTV_NULL)
68366905
{
6837-
PG_RETURN_NULL();
6906+
return NULL;
68386907
}
68396908
else if (agtv_value->type == AGTV_INTEGER)
68406909
{
@@ -6863,17 +6932,24 @@ Datum age_tostring(PG_FUNCTION_ARGS)
68636932
else
68646933
{
68656934
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6866-
errmsg("toString() unsupported argument agtype %d",
6867-
agtv_value->type)));
6935+
errmsg("%s unsupported argument agtype %d",
6936+
msghdr, agtv_value->type)));
68686937
}
68696938
}
6939+
/* it is an unknown type */
6940+
else
6941+
{
6942+
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6943+
errmsg("%s unknown argument agtype %d",
6944+
msghdr, type)));
6945+
}
68706946

68716947
/* build the result */
6872-
agtv_result.type = AGTV_STRING;
6873-
agtv_result.val.string.val = string;
6874-
agtv_result.val.string.len = strlen(string);
6948+
agtv_result->type = AGTV_STRING;
6949+
agtv_result->val.string.val = string;
6950+
agtv_result->val.string.len = strlen(string);
68756951

6876-
PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
6952+
return agtv_result;
68776953
}
68786954

68796955
PG_FUNCTION_INFO_V1(age_tostringlist);

0 commit comments

Comments
 (0)