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 d0323f96 Cast bool to pg_bigint (#827)
d0323f96 is described below

commit d0323f96ddd19a5bb203b449f892591200d680b9
Author: Zainab-Saad <[email protected]>
AuthorDate: Thu Apr 27 05:55:13 2023 +0500

    Cast bool to pg_bigint (#827)
    
    * Modify agtype.c
    - Make changes in the conditional statements to implement the casting 
functionality
    
    * Modify agtype.sql and agtype.out
    - Add regression tests only in the agtype_to_int8 function
    
    * Fix&Add regression tests in expr.sql and test results in expr.out and 
agtype.out
    - Add more regression tests and remove some regression tests that test the 
casting from bool to pg_bigint inside the cypher queries
---
 regress/expected/agtype.out    | 15 +++++++++++++++
 regress/expected/expr.out      | 20 ++++++++++++++++----
 regress/sql/agtype.sql         |  6 ++++++
 regress/sql/expr.sql           | 10 +++++++---
 src/backend/utils/adt/agtype.c |  6 +++++-
 5 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/regress/expected/agtype.out b/regress/expected/agtype.out
index 2d53f3c3..03e1a656 100644
--- a/regress/expected/agtype.out
+++ b/regress/expected/agtype.out
@@ -2123,6 +2123,21 @@ SELECT bool_to_agtype(true) <> bool_to_agtype(false);
  t
 (1 row)
 
+--
+-- Test boolean to pg_bigint cast
+--
+SELECT agtype_to_int8(agtype_in('true'));
+ agtype_to_int8 
+----------------
+              1
+(1 row)
+
+SELECT agtype_to_int8(agtype_in('false'));
+ agtype_to_int8 
+----------------
+              0
+(1 row)
+
 --
 -- Test agtype to int[]
 --
diff --git a/regress/expected/expr.out b/regress/expected/expr.out
index 316ab203..645ab96a 100644
--- a/regress/expected/expr.out
+++ b/regress/expected/expr.out
@@ -1139,6 +1139,14 @@ $$) AS (i bigint);
  1
 (1 row)
 
+SELECT * FROM cypher('type_coercion', $$
+       RETURN true
+$$) AS (i bigint);
+ i 
+---
+ 1
+(1 row)
+
 --Invalid String Format
 SELECT * FROM cypher('type_coercion', $$
        RETURN '1.0'
@@ -1154,10 +1162,6 @@ SELECT * FROM cypher('type_coercion', $$
 $$) AS (i int);
 ERROR:  integer out of range
 --Invalid types
-SELECT * FROM cypher('type_coercion', $$
-       RETURN true
-$$) AS (i bigint);
-ERROR:  cannot cast agtype boolean to type int
 SELECT * FROM cypher('type_coercion', $$
        RETURN {key: 1}
 $$) AS (i bigint);
@@ -5816,6 +5820,14 @@ SELECT * FROM cypher('opt_forms', $$MATCH (u)-->()<--(v) 
RETURN *$$) AS (col1 ag
 (2 rows)
 
 -- Added typecasts ::pg_bigint and ::pg_float8
+SELECT * FROM cypher('expr', $$
+RETURN true::pg_bigint
+$$) AS (result agtype);
+ result 
+--------
+ 1
+(1 row)
+
 SELECT * from cypher('expr', $$
 RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_bigint)))
 $$) as (result agtype);
diff --git a/regress/sql/agtype.sql b/regress/sql/agtype.sql
index 958172ff..6aa03932 100644
--- a/regress/sql/agtype.sql
+++ b/regress/sql/agtype.sql
@@ -540,6 +540,12 @@ SELECT bool_to_agtype(null);
 SELECT bool_to_agtype(true) = bool_to_agtype(true);
 SELECT bool_to_agtype(true) <> bool_to_agtype(false);
 
+--
+-- Test boolean to pg_bigint cast
+--
+SELECT agtype_to_int8(agtype_in('true'));
+SELECT agtype_to_int8(agtype_in('false'));
+
 --
 -- Test agtype to int[]
 --
diff --git a/regress/sql/expr.sql b/regress/sql/expr.sql
index 7fe904fd..da116708 100644
--- a/regress/sql/expr.sql
+++ b/regress/sql/expr.sql
@@ -514,6 +514,10 @@ SELECT * FROM cypher('type_coercion', $$
        RETURN '1'
 $$) AS (i bigint);
 
+SELECT * FROM cypher('type_coercion', $$
+       RETURN true
+$$) AS (i bigint);
+
 --Invalid String Format
 SELECT * FROM cypher('type_coercion', $$
        RETURN '1.0'
@@ -529,9 +533,6 @@ SELECT * FROM cypher('type_coercion', $$
 $$) AS (i int);
 
 --Invalid types
-SELECT * FROM cypher('type_coercion', $$
-       RETURN true
-$$) AS (i bigint);
 
 SELECT * FROM cypher('type_coercion', $$
        RETURN {key: 1}
@@ -2345,6 +2346,9 @@ SELECT * FROM cypher('opt_forms', $$MATCH (u) CREATE 
(u)-[:edge]->() RETURN *$$)
 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 true::pg_bigint
+$$) AS (result agtype);
 SELECT * from cypher('expr', $$
 RETURN pg_catalog.sqrt(pg_catalog.sqrt(pg_catalog.sqrt(256::pg_bigint)))
 $$) as (result agtype);
diff --git a/src/backend/utils/adt/agtype.c b/src/backend/utils/adt/agtype.c
index bc5260d1..8ca893c8 100644
--- a/src/backend/utils/adt/agtype.c
+++ b/src/backend/utils/adt/agtype.c
@@ -2605,7 +2605,8 @@ Datum agtype_to_int8(PG_FUNCTION_ARGS)
         (agtv.type != AGTV_FLOAT &&
          agtv.type != AGTV_INTEGER &&
          agtv.type != AGTV_NUMERIC &&
-         agtv.type != AGTV_STRING))
+         agtv.type != AGTV_STRING &&
+         agtv.type != AGTV_BOOL))
         cannot_cast_agtype_value(agtv.type, "int");
 
     PG_FREE_IF_COPY(agtype_in, 0);
@@ -2621,6 +2622,9 @@ Datum agtype_to_int8(PG_FUNCTION_ARGS)
     else if (agtv.type == AGTV_STRING)
         result = DatumGetInt64(DirectFunctionCall1(int8in,
                            CStringGetDatum(agtv.val.string.val)));
+    else if(agtv.type == AGTV_BOOL)
+        result = DatumGetInt64(DirectFunctionCall1(bool_int4, 
+                      BoolGetDatum(agtv.val.boolean)));
     else
         elog(ERROR, "invalid agtype type: %d", (int)agtv.type);
 

Reply via email to