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 2023/04/24 23:51:29 UTC

[age] branch master updated: Introduce age_pi and age_rand functions and related updates (#823)

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/age.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e3f8388 Introduce age_pi and age_rand functions and related updates (#823)
2e3f8388 is described below

commit 2e3f8388c2d2ba9b5ce076a5e1d53ae7952cb0f1
Author: mohayu22 <11...@users.noreply.github.com>
AuthorDate: Tue Apr 25 04:51:23 2023 +0500

    Introduce age_pi and age_rand functions and related updates (#823)
    
    * Added SQL for creating age_rand and age_pi functions
    -IMMUTABLE flag was used for age_pi but not for age_rand
    
    * Added regression tests
    -Updated previous regression tests
    -Added new regression tests for age_rand and age_pi
    -For rand, n.i < 1 AND n.i >= 0 logic is used which would return true to pass.
    
    * Added output for regression tests
    -Updated previous regression tests
    -Added new tests for age_rand and age_pi
    -For rand, n.i < 1 AND n.i >= 0 logic is used which would return true to pass.
    
    * Updated previous regres tests for pi() and rand()
    -Changed pg_catalog to ag_catalog
    -Changes pi and rand to age_pi and age_rand
    
    * C implementation for age_rand and age_pi
    -The C implementation uses the postgre drandom and dpi functions, which are located in the float.c file.
    -The DirectFunctionCall1 macro is used to call these functions.
    -The float return value of these functions is then converted to agtype.
    
    * Removed lines from cypher_gram.y that are no more needed
    -In cypher_gram.c, function node (fnode) is created for both age functions and those that directly mapped to postgres functions.
    -However, after adding age_rand and age_pi functions, the code related to mapped functions random and pi is removed.
---
 age--1.2.0.sql                   | 13 +++++++++++++
 regress/expected/cypher_set.out  | 17 +++++++++++++++++
 regress/expected/expr.out        |  4 ++--
 regress/sql/cypher_set.sql       |  4 ++++
 src/backend/parser/cypher_gram.y |  6 +-----
 src/backend/utils/adt/agtype.c   | 32 ++++++++++++++++++++++++++++++++
 6 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/age--1.2.0.sql b/age--1.2.0.sql
index c8d7ecd0..6588eb1d 100644
--- a/age--1.2.0.sql
+++ b/age--1.2.0.sql
@@ -3782,6 +3782,19 @@ IMMUTABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
+CREATE FUNCTION ag_catalog.age_pi()
+RETURNS agtype
+LANGUAGE c
+IMMUTABLE
+PARALLEL SAFE
+AS 'MODULE_PATHNAME';
+
+CREATE FUNCTION ag_catalog.age_rand()
+RETURNS agtype
+LANGUAGE c
+PARALLEL SAFE
+AS 'MODULE_PATHNAME';
+
 CREATE FUNCTION ag_catalog.age_exp(variadic "any")
 RETURNS agtype
 LANGUAGE c
diff --git a/regress/expected/cypher_set.out b/regress/expected/cypher_set.out
index fbc4b631..adb2d28c 100644
--- a/regress/expected/cypher_set.out
+++ b/regress/expected/cypher_set.out
@@ -92,6 +92,23 @@ SELECT * FROM cypher('cypher_set', $$MATCH (n) RETURN n$$) AS (a agtype);
  {"id": 844424930131970, "label": "v", "properties": {"a": 0, "i": 3, "j": 5}}::vertex
 (3 rows)
 
+--Test assigning properties to rand() and pi()
+SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = rand() RETURN n.i < 1 AND n.i >= 0$$) AS (a agtype);
+  a   
+------
+ true
+ true
+ true
+(3 rows)
+
+SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = pi() RETURN n$$) AS (a agtype);
+                                                  a                                                   
+------------------------------------------------------------------------------------------------------
+ {"id": 844424930131969, "label": "v", "properties": {"i": 3.14159265358979}}::vertex
+ {"id": 844424930131971, "label": "v", "properties": {"i": 3.14159265358979}}::vertex
+ {"id": 844424930131970, "label": "v", "properties": {"a": 0, "i": 3.14159265358979, "j": 5}}::vertex
+(3 rows)
+
 --Handle Inheritance
 SELECT * FROM cypher('cypher_set', $$CREATE ()$$) AS (a agtype);
  a 
diff --git a/regress/expected/expr.out b/regress/expected/expr.out
index 7064356a..316ab203 100644
--- a/regress/expected/expr.out
+++ b/regress/expected/expr.out
@@ -4320,14 +4320,14 @@ $$) AS (results agtype);
 SELECT * FROM cypher('expr', $$
     RETURN pi(null)
 $$) AS (results agtype);
-ERROR:  function pg_catalog.pi(agtype) does not exist
+ERROR:  function ag_catalog.age_pi(agtype) does not exist
 LINE 2:     RETURN pi(null)
                      ^
 HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
 SELECT * FROM cypher('expr', $$
     RETURN pi(1)
 $$) AS (results agtype);
-ERROR:  function pg_catalog.pi(agtype) does not exist
+ERROR:  function ag_catalog.age_pi(agtype) does not exist
 LINE 2:     RETURN pi(1)
                      ^
 HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
diff --git a/regress/sql/cypher_set.sql b/regress/sql/cypher_set.sql
index a2b36a0a..205cf804 100644
--- a/regress/sql/cypher_set.sql
+++ b/regress/sql/cypher_set.sql
@@ -38,6 +38,10 @@ SELECT * FROM cypher('cypher_set', $$MATCH (n) RETURN n$$) AS (a agtype);
 SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = 3 RETURN n$$) AS (a agtype);
 SELECT * FROM cypher('cypher_set', $$MATCH (n) RETURN n$$) AS (a agtype);
 
+--Test assigning properties to rand() and pi()
+SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = rand() RETURN n.i < 1 AND n.i >= 0$$) AS (a agtype);
+SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = pi() RETURN n$$) AS (a agtype);
+
 --Handle Inheritance
 SELECT * FROM cypher('cypher_set', $$CREATE ()$$) AS (a agtype);
 SELECT * FROM cypher('cypher_set', $$MATCH (n) SET n.i = 3 RETURN n$$) AS (a agtype);
diff --git a/src/backend/parser/cypher_gram.y b/src/backend/parser/cypher_gram.y
index 84eb957a..6e124679 100644
--- a/src/backend/parser/cypher_gram.y
+++ b/src/backend/parser/cypher_gram.y
@@ -2139,11 +2139,7 @@ static Node *make_function_expr(List *func_name, List *exprs, int location)
          * functions. We may want to find a better way to do this, as there
          * could be many.
          */
-        if (pg_strcasecmp(name, "rand") == 0)
-            funcname = SystemFuncName("random");
-        else if (pg_strcasecmp(name, "pi") == 0)
-            funcname = SystemFuncName("pi");
-        else if (pg_strcasecmp(name, "count") == 0)
+        if (pg_strcasecmp(name, "count") == 0)
             funcname = SystemFuncName("count");
         else
             /*
diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c
index 388725b4..bc5260d1 100644
--- a/src/backend/utils/adt/agtype.c
+++ b/src/backend/utils/adt/agtype.c
@@ -8197,6 +8197,38 @@ Datum age_e(PG_FUNCTION_ARGS)
     PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
 }
 
+PG_FUNCTION_INFO_V1(age_pi);
+
+Datum age_pi(PG_FUNCTION_ARGS)
+{
+    agtype_value agtv_result;
+    float8 float_result;
+
+    float_result = DatumGetFloat8(DirectFunctionCall1(dpi, 0));
+
+    /* build the result */
+    agtv_result.type = AGTV_FLOAT;
+    agtv_result.val.float_value = float_result;
+
+    PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
+}
+
+PG_FUNCTION_INFO_V1(age_rand);
+
+Datum age_rand(PG_FUNCTION_ARGS)
+{
+    agtype_value agtv_result;
+    float8 float_result;
+
+    float_result = DatumGetFloat8(DirectFunctionCall1(drandom, 0));
+
+    /* build the result */
+    agtv_result.type = AGTV_FLOAT;
+    agtv_result.val.float_value = float_result;
+
+    PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
+}
+
 PG_FUNCTION_INFO_V1(age_exp);
 
 Datum age_exp(PG_FUNCTION_ARGS)