You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@age.apache.org by jg...@apache.org on 2021/02/26 00:09:13 UTC

[incubator-age] branch master updated: Add fixes to typecast functions

This is an automated email from the ASF dual-hosted git repository.

jgemignani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-age.git


The following commit(s) were added to refs/heads/master by this push:
     new 60734d7  Add fixes to typecast functions
60734d7 is described below

commit 60734d7dc55c8ab260ffc96cc06955c5c23874d4
Author: John Gemignani <jr...@gmail.com>
AuthorDate: Thu Feb 25 12:10:12 2021 -0800

    Add fixes to typecast functions
    
    Added fixes to the agtype typecast functions -
    
        agtype_typecast_numeric
        agtype_typecast_float
        agtype_typecast_vertex
        agtype_typecast_edge
        agtype_typecast_path
    
    These fixes allow the functions to accept "any" input.
    
    Adjusted the sql definitions to reflect this.
    
    Adjusted the transform logic to look for the correct functions.
    
    No regression tests needed to be adjusted.
---
 age--0.3.0.sql                   | 10 +++----
 src/backend/parser/cypher_expr.c | 10 +++----
 src/backend/utils/adt/agtype.c   | 65 ++++++++++++++--------------------------
 3 files changed, 32 insertions(+), 53 deletions(-)

diff --git a/age--0.3.0.sql b/age--0.3.0.sql
index 832a02f..123d57d 100644
--- a/age--0.3.0.sql
+++ b/age--0.3.0.sql
@@ -1389,35 +1389,35 @@ CREATE AGGREGATE ag_catalog.age_collect(variadic "any")
 --
 -- function for typecasting an agtype value to another agtype value
 --
-CREATE FUNCTION ag_catalog.agtype_typecast_numeric(agtype)
+CREATE FUNCTION ag_catalog.agtype_typecast_numeric(variadic "any")
 RETURNS agtype
 LANGUAGE c
 STABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
-CREATE FUNCTION ag_catalog.agtype_typecast_float(agtype)
+CREATE FUNCTION ag_catalog.agtype_typecast_float(variadic "any")
 RETURNS agtype
 LANGUAGE c
 STABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
-CREATE FUNCTION ag_catalog.agtype_typecast_vertex(agtype)
+CREATE FUNCTION ag_catalog.agtype_typecast_vertex(variadic "any")
 RETURNS agtype
 LANGUAGE c
 STABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
-CREATE FUNCTION ag_catalog.agtype_typecast_edge(agtype)
+CREATE FUNCTION ag_catalog.agtype_typecast_edge(variadic "any")
 RETURNS agtype
 LANGUAGE c
 STABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
-CREATE FUNCTION ag_catalog.agtype_typecast_path(agtype)
+CREATE FUNCTION ag_catalog.agtype_typecast_path(variadic "any")
 RETURNS agtype
 LANGUAGE c
 STABLE
diff --git a/src/backend/parser/cypher_expr.c b/src/backend/parser/cypher_expr.c
index 1d61d73..39a6fb8 100644
--- a/src/backend/parser/cypher_expr.c
+++ b/src/backend/parser/cypher_expr.c
@@ -650,27 +650,27 @@ static Node *transform_cypher_typecast(cypher_parsestate *cpstate,
     if (pg_strcasecmp(ctypecast->typecast, "edge") == 0)
     {
         func_agtype_typecast_operator_oid =
-            get_ag_func_oid("agtype_typecast_edge", 1, AGTYPEOID);
+            get_ag_func_oid("agtype_typecast_edge", 1, ANYOID);
     }
     else if (pg_strcasecmp(ctypecast->typecast, "path") == 0)
     {
         func_agtype_typecast_operator_oid =
-            get_ag_func_oid("agtype_typecast_path", 1, AGTYPEOID);
+            get_ag_func_oid("agtype_typecast_path", 1, ANYOID);
     }
     else if (pg_strcasecmp(ctypecast->typecast, "vertex") == 0)
     {
         func_agtype_typecast_operator_oid =
-            get_ag_func_oid("agtype_typecast_vertex", 1, AGTYPEOID);
+            get_ag_func_oid("agtype_typecast_vertex", 1, ANYOID);
     }
     else if (pg_strcasecmp(ctypecast->typecast, "numeric") == 0)
     {
         func_agtype_typecast_operator_oid =
-            get_ag_func_oid("agtype_typecast_numeric", 1, AGTYPEOID);
+            get_ag_func_oid("agtype_typecast_numeric", 1, ANYOID);
     }
     else if (pg_strcasecmp(ctypecast->typecast, "float") == 0)
     {
         func_agtype_typecast_operator_oid =
-            get_ag_func_oid("agtype_typecast_float", 1, AGTYPEOID);
+            get_ag_func_oid("agtype_typecast_float", 1, ANYOID);
     }
     /* if none was found, error out */
     else
diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c
index 3212608..52cc3cb 100644
--- a/src/backend/utils/adt/agtype.c
+++ b/src/backend/utils/adt/agtype.c
@@ -137,7 +137,6 @@ static bool is_object_vertex(agtype_value *agtv);
 static bool is_object_edge(agtype_value *agtv);
 static bool is_array_path(agtype_value *agtv);
 /* helper functions */
-static bool is_agtype_null(agtype *agt);
 static uint64 get_edge_uniqueness_value(Datum d, Oid type, bool is_null,
                                         int index);
 /* graph entity retrieval */
@@ -2696,21 +2695,6 @@ Datum agtype_string_match_contains(PG_FUNCTION_ARGS)
                     errmsg("agtype string values expected")));
 }
 
-static bool is_agtype_null(agtype *agt)
-{
-    if (AGT_ROOT_IS_ARRAY(agt) && AGT_ROOT_IS_SCALAR(agt))
-    {
-        agtype_value *agtv_element;
-
-        agtv_element = get_ith_agtype_value_from_container(&agt->root, 0);
-
-        if (agtv_element->type == AGTV_NULL)
-            return true;
-    }
-
-    return false;
-}
-
 #define LEFT_ROTATE(n, i) ((n << i) | (n >> (64 - i)))
 #define RIGHT_ROTATE(n, i)  ((n >> i) | (n << (64 - i)))
 
@@ -2788,12 +2772,14 @@ Datum agtype_typecast_numeric(PG_FUNCTION_ARGS)
     Datum numd;
     char *string = NULL;
 
-    /* return null if arg is null */
-    if (PG_ARGISNULL(0))
+    /* get the agtype equivalence of any convertable input type */
+    arg_agt = get_one_agtype_from_variadic_args(fcinfo, 0, 1);
+
+    /* Return null if arg_agt is null. This covers SQL and Agtype NULLS */
+    if (arg_agt == NULL)
         PG_RETURN_NULL();
 
     /* check that we have a scalar value */
-    arg_agt = AG_GET_ARG_AGTYPE_P(0);
     if (!AGT_ROOT_IS_SCALAR(arg_agt))
         ereport(ERROR,
                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -2863,12 +2849,14 @@ Datum agtype_typecast_float(PG_FUNCTION_ARGS)
     Datum d;
     char *string = NULL;
 
-    /* return null if arg is null */
-    if (PG_ARGISNULL(0))
+    /* get the agtype equivalence of any convertable input type */
+    arg_agt = get_one_agtype_from_variadic_args(fcinfo, 0, 1);
+
+    /* Return null if arg_agt is null. This covers SQL and Agtype NULLS */
+    if (arg_agt == NULL)
         PG_RETURN_NULL();
 
     /* check that we have a scalar value */
-    arg_agt = AG_GET_ARG_AGTYPE_P(0);
     if (!AGT_ROOT_IS_SCALAR(arg_agt))
         ereport(ERROR,
                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@@ -2936,14 +2924,11 @@ Datum agtype_typecast_vertex(PG_FUNCTION_ARGS)
     Datum result;
     int count;
 
-    /* Return null if arg is null */
-    if (PG_ARGISNULL(0))
-        PG_RETURN_NULL();
+    /* get the agtype equivalence of any convertable input type */
+    arg_agt = get_one_agtype_from_variadic_args(fcinfo, 0, 1);
 
-    arg_agt = AG_GET_ARG_AGTYPE_P(0);
-
-    /* Return null if arg is agtype null */
-    if (is_agtype_null(arg_agt))
+    /* Return null if arg_agt is null. This covers SQL and Agtype NULLS */
+    if (arg_agt == NULL)
         PG_RETURN_NULL();
 
     /* A vertex is an object so the arg needs to be one too */
@@ -3014,14 +2999,11 @@ Datum agtype_typecast_edge(PG_FUNCTION_ARGS)
     Datum result;
     int count;
 
-    /* Return null if arg is null */
-    if (PG_ARGISNULL(0))
-        PG_RETURN_NULL();
-
-    arg_agt = AG_GET_ARG_AGTYPE_P(0);
+    /* get the agtype equivalence of any convertable input type */
+    arg_agt = get_one_agtype_from_variadic_args(fcinfo, 0, 1);
 
-    /* Return null if arg is agtype null */
-    if (is_agtype_null(arg_agt))
+    /* Return null if arg_agt is null. This covers SQL and Agtype NULLS */
+    if (arg_agt == NULL)
         PG_RETURN_NULL();
 
     /* An edge is an object, so the arg needs to be one too */
@@ -3111,14 +3093,11 @@ Datum agtype_typecast_path(PG_FUNCTION_ARGS)
     int count = 0;
     int i = 0;
 
-    /* return null if arg is null */
-    if (PG_ARGISNULL(0))
-        PG_RETURN_NULL();
-
-    arg_agt = AG_GET_ARG_AGTYPE_P(0);
+    /* get the agtype equivalence of any convertable input type */
+    arg_agt = get_one_agtype_from_variadic_args(fcinfo, 0, 1);
 
-    /* Return null if arg is agtype null */
-    if (is_agtype_null(arg_agt))
+    /* Return null if arg_agt is null. This covers SQL and Agtype NULLS */
+    if (arg_agt == NULL)
         PG_RETURN_NULL();
 
     /* path needs to be an array */