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/04/02 23:34:59 UTC
[incubator-age] branch master updated: Add typecast from agtype to
PG's bigint and float8
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 9812d50 Add typecast from agtype to PG's bigint and float8
9812d50 is described below
commit 9812d504baac4aa67e4dfcac4713eea103f358eb
Author: John Gemignani <jr...@gmail.com>
AuthorDate: Fri Apr 2 15:52:48 2021 -0700
Add typecast from agtype to PG's bigint and float8
Added the ability to typecast from agtype to PG's bigint and float8.
Added regression tests.
---
regress/expected/expr.out | 17 +++++++++++
regress/sql/expr.sql | 8 ++++++
src/backend/parser/cypher_expr.c | 62 +++++++++++++++++++++++-----------------
src/backend/utils/adt/agtype.c | 2 +-
4 files changed, 61 insertions(+), 28 deletions(-)
diff --git a/regress/expected/expr.out b/regress/expected/expr.out
index d0661c4..90be50b 100644
--- a/regress/expected/expr.out
+++ b/regress/expected/expr.out
@@ -5097,6 +5097,23 @@ SELECT * FROM cypher('opt_forms', $$MATCH (u)-->()<--(v) RETURN *$$) AS (col1 ag
{"id": 281474976710657, "label": "", "properties": {"i": 1}}::vertex | {"id": 281474976710659, "label": "", "properties": {"i": 3}}::vertex
(2 rows)
+-- Added typecasts ::pg_bigint and ::pg_float8
+SELECT * from cypher('expr', $$
+RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_bigint)))
+$$) as (result agtype);
+ result
+--------
+ 2.0
+(1 row)
+
+SELECT * from cypher('expr', $$
+RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_float8)))
+$$) as (result agtype);
+ result
+--------
+ 2.0
+(1 row)
+
-- VLE
SELECT create_graph('VLE');
NOTICE: graph "VLE" has been created
diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql
index 295c2b3..51d3eed 100644
--- a/regress/sql/expr.sql
+++ b/regress/sql/expr.sql
@@ -2097,6 +2097,14 @@ SELECT * FROM cypher('opt_forms', $$MATCH (u)-->()<--(v) RETURN u.i, v.i$$) AS (
SELECT * FROM cypher('opt_forms', $$MATCH (u) CREATE (u)-[:edge]->() RETURN *$$) AS (results agtype);
SELECT * FROM cypher('opt_forms', $$MATCH (u)-->()<--(v) RETURN *$$) AS (col1 agtype, col2 agtype);
+-- Added typecasts ::pg_bigint and ::pg_float8
+SELECT * from cypher('expr', $$
+RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_bigint)))
+$$) as (result agtype);
+SELECT * from cypher('expr', $$
+RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_float8)))
+$$) as (result agtype);
+
-- VLE
SELECT create_graph('VLE');
-- should fail
diff --git a/src/backend/parser/cypher_expr.c b/src/backend/parser/cypher_expr.c
index bd0efb5..cd43874 100644
--- a/src/backend/parser/cypher_expr.c
+++ b/src/backend/parser/cypher_expr.c
@@ -46,6 +46,16 @@
#include "utils/ag_func.h"
#include "utils/agtype.h"
+/* names of typecast functions */
+#define FUNC_AGTYPE_TYPECAST_EDGE "agtype_typecast_edge"
+#define FUNC_AGTYPE_TYPECAST_PATH "agtype_typecast_path"
+#define FUNC_AGTYPE_TYPECAST_VERTEX "agtype_typecast_vertex"
+#define FUNC_AGTYPE_TYPECAST_NUMERIC "agtype_typecast_numeric"
+#define FUNC_AGTYPE_TYPECAST_FLOAT "agtype_typecast_float"
+#define FUNC_AGTYPE_TYPECAST_INT "agtype_typecast_int"
+#define FUNC_AGTYPE_TYPECAST_PG_FLOAT8 "agtype_to_float8"
+#define FUNC_AGTYPE_TYPECAST_PG_BIGINT "agtype_to_int8"
+
static Node *transform_cypher_expr_recurse(cypher_parsestate *cpstate,
Node *expr);
static Node *transform_A_Const(cypher_parsestate *cpstate, A_Const *ac);
@@ -637,46 +647,49 @@ static Node *transform_cypher_string_match(cypher_parsestate *cpstate,
static Node *transform_cypher_typecast(cypher_parsestate *cpstate,
cypher_typecast *ctypecast)
{
- Node *expr;
- FuncExpr *func_expr;
- List *func_args = NIL;
- Oid func_agtype_typecast_operator_oid = InvalidOid;
+ List *fname;
+ FuncCall *fnode;
/* verify input parameter */
Assert (cpstate != NULL);
Assert (ctypecast != NULL);
- /* get the oid of the requested typecast function */
+ /* create the qualified function name, schema first */
+ fname = list_make1(makeString("ag_catalog"));
+
+ /* append the name of the requested typecast function */
if (pg_strcasecmp(ctypecast->typecast, "edge") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_edge", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_EDGE));
}
else if (pg_strcasecmp(ctypecast->typecast, "path") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_path", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_PATH));
}
else if (pg_strcasecmp(ctypecast->typecast, "vertex") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_vertex", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_VERTEX));
}
else if (pg_strcasecmp(ctypecast->typecast, "numeric") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_numeric", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_NUMERIC));
}
else if (pg_strcasecmp(ctypecast->typecast, "float") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_float", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_FLOAT));
}
else if (pg_strcasecmp(ctypecast->typecast, "int") == 0 ||
pg_strcasecmp(ctypecast->typecast, "integer") == 0)
{
- func_agtype_typecast_operator_oid =
- get_ag_func_oid("agtype_typecast_int", 1, ANYOID);
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_INT));
+ }
+ else if (pg_strcasecmp(ctypecast->typecast, "pg_float8") == 0)
+ {
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_PG_FLOAT8));
+ }
+ else if (pg_strcasecmp(ctypecast->typecast, "pg_bigint") == 0)
+ {
+ fname = lappend(fname, makeString(FUNC_AGTYPE_TYPECAST_PG_BIGINT));
}
/* if none was found, error out */
else
@@ -686,17 +699,12 @@ static Node *transform_cypher_typecast(cypher_parsestate *cpstate,
ctypecast->typecast)));
}
- /* transform the expression to be typecast */
- expr = transform_cypher_expr_recurse(cpstate, ctypecast->expr);
+ /* make a function call node */
+ fnode = makeFuncCall(fname, list_make1(ctypecast->expr),
+ ctypecast->location);
- /* append the expression and build the function node */
- func_args = lappend(func_args, expr);
- func_expr = makeFuncExpr(func_agtype_typecast_operator_oid, AGTYPEOID,
- func_args, InvalidOid, InvalidOid,
- COERCE_EXPLICIT_CALL);
- func_expr->location = ctypecast->location;
-
- return (Node *)func_expr;
+ /* return the transformed function */
+ return transform_FuncCall(cpstate, fnode);
}
/*
diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c
index 9a2b8cb..b78c37d 100644
--- a/src/backend/utils/adt/agtype.c
+++ b/src/backend/utils/adt/agtype.c
@@ -2192,7 +2192,7 @@ Datum agtype_to_int8(PG_FUNCTION_ARGS)
{
agtype *agtype_in = AG_GET_ARG_AGTYPE_P(0);
agtype_value agtv;
- int8 result = 0x0;
+ int64 result = 0x0;
agtype *arg_agt;
/* get the agtype equivalence of any convertable input type */