Skip to content
Permalink
Browse files
Add to age_round function so that it accepts 2 parameters and returne…
…d a rounded number with precision passed as 2nd parameter (#159)
  • Loading branch information
quocvietvuong committed Dec 22, 2021
1 parent 0e640b1 commit a04059fa7d9a74ab9687cd29802d71d6e626a61d
Showing 3 changed files with 100 additions and 6 deletions.
@@ -4285,6 +4285,62 @@ $$) AS (results agtype);
-5.0
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(7.4163, 3)
$$) AS (results agtype);
results
---------
7.416
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(7.416343479, 8)
$$) AS (results agtype);
results
------------
7.41634348
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(7.416343479, NULL)
$$) AS (results agtype);
results
---------
7.0
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(NULL, 7)
$$) AS (results agtype);
results
---------

(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(7, 2)
$$) AS (results agtype);
results
---------
7.0
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(7.4342, 2.1123)
$$) AS (results agtype);
results
---------
7.43
(1 row)

SELECT * FROM cypher('expr', $$
RETURN round(NULL, NULL)
$$) AS (results agtype);
results
---------

(1 row)

SELECT * FROM cypher('expr', $$
RETURN sign(10)
$$) AS (results agtype);
@@ -1830,6 +1830,27 @@ $$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(-4.5)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(7.4163, 3)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(7.416343479, 8)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(7.416343479, NULL)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(NULL, 7)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(7, 2)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(7.4342, 2.1123)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN round(NULL, NULL)
$$) AS (results agtype);
SELECT * FROM cypher('expr', $$
RETURN sign(10)
$$) AS (results agtype);
@@ -6967,16 +6967,17 @@ Datum age_round(PG_FUNCTION_ARGS)
bool *nulls;
Oid *types;
agtype_value agtv_result;
Numeric arg;
Numeric arg, arg_precision;
Numeric numeric_result;
float8 float_result;
bool is_null = true;
int precision = 0;

/* extract argument values */
nargs = extract_variadic_args(fcinfo, 0, true, &args, &types, &nulls);

/* check number of args */
if (nargs != 1)
if (nargs != 1 && nargs != 2)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("round() invalid number of arguments")));

@@ -6996,12 +6997,28 @@ Datum age_round(PG_FUNCTION_ARGS)
PG_RETURN_NULL();

/* We need the input as a numeric so that we can pass it off to PG */
numeric_result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
NumericGetDatum(arg),
Int32GetDatum(0)));
if (nargs == 2 && !nulls[1])
{
arg_precision = get_numeric_compatible_arg(args[1], types[1], "round",
&is_null, NULL);
if (!is_null)
{
precision = DatumGetInt64(DirectFunctionCall1(numeric_int8,
NumericGetDatum(arg_precision)));
numeric_result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
NumericGetDatum(arg),
Int32GetDatum(precision)));
}
}
else
{
numeric_result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
NumericGetDatum(arg),
Int32GetDatum(0)));
}

float_result = DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow,
NumericGetDatum(numeric_result)));
NumericGetDatum(numeric_result)));
/* build the result */
agtv_result.type = AGTV_FLOAT;
agtv_result.val.float_value = float_result;

0 comments on commit a04059f

Please sign in to comment.