Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PG16] Added integer conversion in toBoolean functions #1771

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions regress/expected/expr.out
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,22 @@ $$) AS (toBoolean agtype);
false
(1 row)

SELECT * FROM cypher('expr', $$
RETURN toBoolean(1)
$$) AS (toBoolean agtype);
toboolean
-----------
true
(1 row)

SELECT * FROM cypher('expr', $$
RETURN toBoolean(0)
$$) AS (toBoolean agtype);
toboolean
-----------
false
(1 row)

-- should return null
SELECT * FROM cypher('expr', $$
RETURN toBoolean("false_")
Expand All @@ -3095,10 +3111,6 @@ $$) AS (toBoolean agtype);
(1 row)

-- should fail
SELECT * FROM cypher('expr', $$
RETURN toBoolean(1)
$$) AS (toBoolean agtype);
ERROR: toBoolean() unsupported argument agtype 3
SELECT * FROM cypher('expr', $$
RETURN toBoolean()
$$) AS (toBoolean agtype);
Expand Down Expand Up @@ -3131,6 +3143,14 @@ $$) AS (toBooleanList agtype);
[true, false, true]
(1 row)

SELECT * FROM cypher('expr', $$
RETURN toBooleanList([0,1,2,3,4])
$$) AS (toBooleanList agtype);
tobooleanlist
---------------------------------
[false, true, true, true, true]
(1 row)

-- should return null
SELECT * FROM cypher('expr', $$
RETURN toBooleanList([])
Expand Down Expand Up @@ -3164,14 +3184,6 @@ $$) AS (toBooleanList agtype);
[null, null]
(1 row)

SELECT * FROM cypher('expr', $$
RETURN toBooleanList([0,1,2,3,4])
$$) AS (toBooleanList agtype);
tobooleanlist
--------------------------------
[null, null, null, null, null]
(1 row)

-- should fail
SELECT * FROM cypher('expr', $$
RETURN toBooleanList(fail)
Expand Down
17 changes: 10 additions & 7 deletions regress/sql/expr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,12 @@ $$) AS (toBoolean agtype);
SELECT * FROM cypher('expr', $$
RETURN toBoolean("false")
$$) AS (toBoolean agtype);
SELECT * FROM cypher('expr', $$
RETURN toBoolean(1)
$$) AS (toBoolean agtype);
SELECT * FROM cypher('expr', $$
RETURN toBoolean(0)
$$) AS (toBoolean agtype);
-- should return null
SELECT * FROM cypher('expr', $$
RETURN toBoolean("false_")
Expand All @@ -1357,9 +1363,6 @@ SELECT * FROM cypher('expr', $$
RETURN toBoolean(null)
$$) AS (toBoolean agtype);
-- should fail
SELECT * FROM cypher('expr', $$
RETURN toBoolean(1)
$$) AS (toBoolean agtype);
SELECT * FROM cypher('expr', $$
RETURN toBoolean()
$$) AS (toBoolean agtype);
Expand All @@ -1377,6 +1380,10 @@ SELECT * FROM cypher('expr', $$
RETURN toBooleanList(["True", "False", "True"])
$$) AS (toBooleanList agtype);

SELECT * FROM cypher('expr', $$
RETURN toBooleanList([0,1,2,3,4])
$$) AS (toBooleanList agtype);

-- should return null
SELECT * FROM cypher('expr', $$
RETURN toBooleanList([])
Expand All @@ -1394,10 +1401,6 @@ SELECT * FROM cypher('expr', $$
RETURN toBooleanList([["A", "B"], ["C", "D"]])
$$) AS (toBooleanList agtype);

SELECT * FROM cypher('expr', $$
RETURN toBooleanList([0,1,2,3,4])
$$) AS (toBooleanList agtype);

-- should fail
SELECT * FROM cypher('expr', $$
RETURN toBooleanList(fail)
Expand Down
39 changes: 31 additions & 8 deletions src/backend/utils/adt/agtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -5720,8 +5720,8 @@ Datum age_toboolean(PG_FUNCTION_ARGS)
PG_RETURN_NULL();

/*
* toBoolean() supports bool, text, cstring, or the agtype bool, and string
* input.
* toBoolean() supports bool, text, cstring, integer or the agtype bool,
* string and integer input.
*/
arg = args[0];
type = types[0];
Expand All @@ -5744,6 +5744,10 @@ Datum age_toboolean(PG_FUNCTION_ARGS)
else
PG_RETURN_NULL();
}
else if (type == INT2OID || type == INT4OID || type == INT8OID)
{
result = DatumGetBool(DirectFunctionCall1(int4_bool, arg));
}
else
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("toBoolean() unsupported argument type %d",
Expand Down Expand Up @@ -5778,6 +5782,11 @@ Datum age_toboolean(PG_FUNCTION_ARGS)
else
PG_RETURN_NULL();
}
else if (agtv_value->type == AGTV_INTEGER)
{
result = DatumGetBool(DirectFunctionCall1(int4_bool,
Int64GetDatum(agtv_value->val.int_value)));
}
else
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("toBoolean() unsupported argument agtype %d",
Expand Down Expand Up @@ -5832,7 +5841,7 @@ Datum age_tobooleanlist(PG_FUNCTION_ARGS)
/* iterate through the list */
for (i = 0; i < count; i++)
{
// TODO: check element's type, it's value, and convert it to boolean if possible.
// check element's type, it's value, and convert it to boolean if possible.
elem = get_ith_agtype_value_from_container(&agt_arg->root, i);
bool_elem.type = AGTV_BOOL;

Expand All @@ -5845,32 +5854,46 @@ Datum age_tobooleanlist(PG_FUNCTION_ARGS)
if (pg_strcasecmp(string, "true") == 0)
{
bool_elem.val.boolean = true;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &bool_elem);
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);
}
else if (pg_strcasecmp(string, "false") == 0)
{
bool_elem.val.boolean = false;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &bool_elem);
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);
}
else
{
bool_elem.type = AGTV_NULL;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &bool_elem);
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);
}

break;

case AGTV_BOOL:

bool_elem.val.boolean = elem->val.boolean;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &bool_elem);
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);

break;

case AGTV_INTEGER:

bool_elem.val.boolean = DatumGetBool(DirectFunctionCall1(int4_bool,
Int64GetDatum(elem->val.int_value)));
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);

break;

default:

bool_elem.type = AGTV_NULL;
agis_result.res = push_agtype_value(&agis_result.parse_state, WAGT_ELEM, &bool_elem);
agis_result.res = push_agtype_value(&agis_result.parse_state,
WAGT_ELEM, &bool_elem);

break;
}
Expand Down
Loading