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 2020/09/21 19:00:55 UTC

[incubator-age] branch master updated: Add openCypher e() function

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 01be6b4  Add openCypher e() function
01be6b4 is described below

commit 01be6b4004f8de70e2ba2c42a3ad69a08226af4a
Author: John Gemignani <jr...@gmail.com>
AuthorDate: Fri Sep 18 16:37:44 2020 -0700

    Add openCypher e() function
    
    Added the openCypher e() function.
    
    Unfortunately, there isn't a constant, like with pi. So, I use the
    builtin dexp exp() with a 1 to get it.
    
    Added regression tests.
---
 age--0.2.0.sql                   |  7 +++++++
 regress/expected/expr.out        | 19 +++++++++++++++++++
 regress/sql/expr.sql             | 10 ++++++++++
 src/backend/parser/cypher_expr.c |  4 +++-
 src/backend/utils/adt/agtype.c   | 17 +++++++++++++++++
 5 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/age--0.2.0.sql b/age--0.2.0.sql
index 76f181d..f9144e7 100644
--- a/age--0.2.0.sql
+++ b/age--0.2.0.sql
@@ -1152,6 +1152,13 @@ STABLE
 PARALLEL SAFE
 AS 'MODULE_PATHNAME';
 
+CREATE FUNCTION ag_e()
+RETURNS agtype
+LANGUAGE c
+STABLE
+PARALLEL SAFE
+AS 'MODULE_PATHNAME';
+
 --
 -- function for typecasting an agtype value to another agtype value
 --
diff --git a/regress/expected/expr.out b/regress/expected/expr.out
index 1ac35e5..e982c31 100644
--- a/regress/expected/expr.out
+++ b/regress/expected/expr.out
@@ -3938,6 +3938,25 @@ ERROR:  invalid number of input parameters for log10()
 LINE 1: SELECT * from cypher('expr', $$
                                       ^
 --
+-- e()
+--
+SELECT * from cypher('expr', $$
+    RETURN e()
+$$) as (result agtype);
+      result      
+------------------
+ 2.71828182845905
+(1 row)
+
+SELECT * from cypher('expr', $$
+    RETURN log(e())
+$$) as (result agtype);
+ result 
+--------
+ 1.0
+(1 row)
+
+--
 -- Cleanup
 --
 SELECT * FROM drop_graph('expr', true);
diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql
index 940161d..fa13865 100644
--- a/regress/sql/expr.sql
+++ b/regress/sql/expr.sql
@@ -1682,6 +1682,16 @@ SELECT * from cypher('expr', $$
 $$) as (result agtype);
 
 --
+-- e()
+--
+SELECT * from cypher('expr', $$
+    RETURN e()
+$$) as (result agtype);
+SELECT * from cypher('expr', $$
+    RETURN log(e())
+$$) as (result agtype);
+
+--
 -- Cleanup
 --
 SELECT * FROM drop_graph('expr', true);
diff --git a/src/backend/parser/cypher_expr.c b/src/backend/parser/cypher_expr.c
index 76ff5e4..e7c2579 100644
--- a/src/backend/parser/cypher_expr.c
+++ b/src/backend/parser/cypher_expr.c
@@ -89,6 +89,7 @@
 #define FUNC_RAND       {"rand",       "random",     0,      0, 0, FLOAT8OID, 0, 0, false, true}
 #define FUNC_LOG        {"log",        "ag_log",     ANYOID, 0, 0, AGTYPEOID, 1, 1, false, false}
 #define FUNC_LOG10      {"log10",      "ag_log10",   ANYOID, 0, 0, AGTYPEOID, 1, 1, false, false}
+#define FUNC_E          {"e",          "ag_e",       0,      0, 0, AGTYPEOID, 0, 0, false, false}
 
 /* supported functions */
 #define SUPPORTED_FUNCTIONS {FUNC_TYPE, FUNC_ENDNODE, FUNC_HEAD, FUNC_ID, \
@@ -103,7 +104,8 @@
                              FUNC_RCOT, FUNC_RASIN, FUNC_RACOS, FUNC_RATAN, \
                              FUNC_RATAN2, FUNC_PI, FUNC_DEGREES, FUNC_RADIANS, \
                              FUNC_ROUND, FUNC_CEIL, FUNC_FLOOR, FUNC_ABS, \
-                             FUNC_SIGN, FUNC_RAND, FUNC_LOG, FUNC_LOG10}
+                             FUNC_SIGN, FUNC_RAND, FUNC_LOG, FUNC_LOG10, \
+                             FUNC_E}
 
 /* structure for supported function signatures */
 typedef struct function_signature
diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c
index 217a9d2..e168c1e 100644
--- a/src/backend/utils/adt/agtype.c
+++ b/src/backend/utils/adt/agtype.c
@@ -6771,3 +6771,20 @@ Datum ag_log10(PG_FUNCTION_ARGS)
 
     PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
 }
+
+PG_FUNCTION_INFO_V1(ag_e);
+
+Datum ag_e(PG_FUNCTION_ARGS)
+{
+    agtype_value agtv_result;
+    float8 float_result;
+
+    /* get e by raising e to 1 - no, they don't have a constant e :/ */
+    float_result = DatumGetFloat8(DirectFunctionCall1(dexp, Float8GetDatum(1)));
+
+    /* build the result */
+    agtv_result.type = AGTV_FLOAT;
+    agtv_result.val.float_value = float_result;
+
+    PG_RETURN_POINTER(agtype_value_to_agtype(&agtv_result));
+}