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

feat: add rounding functions with decimal type args #671

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
141 changes: 140 additions & 1 deletion extensions/functions_rounding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ scalar_functions:
- value: fp64
name: "x"
return: fp64
- args:
- value: decimal<P1,S1>
name: "x"
return: decimal<P1,0>
-
name: "floor"
description: >
Expand All @@ -27,6 +31,10 @@ scalar_functions:
- value: fp64
name: "x"
return: fp64
- args:
- value: decimal<P1,S1>
name: "x"
return: decimal<P1,0>
-
name: "round"
description: >
Expand Down Expand Up @@ -265,6 +273,137 @@ scalar_functions:
- TIE_TO_ODD: round to nearest value; if exactly halfway, tie
to the odd option.
values: [ TIE_TO_EVEN, TIE_AWAY_FROM_ZERO, TRUNCATE, CEILING, FLOOR,
AWAY_FROM_ZERO, TIE_DOWN, TIE_UP, TIE_TOWARDS_ZERO, TIE_TO_ODD ]
AWAY_FROM_ZERO, TIE_DOWN, TIE_UP, TIE_TOWARDS_ZERO, TIE_TO_ODD ]
nullability: DECLARED_OUTPUT
return: fp64?
- args:
- value: fp64
name: "x"
description: >
Numerical expression to be rounded.
- value: decimal<38,0>
name: "s"
description: >
Number of decimal places to be rounded to.

When `s` is a positive number, the rounding
is performed to a `s` number of decimal places.

When `s` is a negative number, the rounding is
performed to the left side of the decimal point
as specified by `s`.
options:
rounding:
description: >
When a boundary is computed to lie somewhere between two values,
and this value cannot be exactly represented, this specifies how
to round it.

- TIE_TO_EVEN: round to nearest value; if exactly halfway, tie
to the even option.
- TIE_AWAY_FROM_ZERO: round to nearest value; if exactly
halfway, tie away from zero.
- TRUNCATE: always round toward zero.
- CEILING: always round toward positive infinity.
- FLOOR: always round toward negative infinity.
- AWAY_FROM_ZERO: round negative values with FLOOR rule, round positive values with CEILING rule
- TIE_DOWN: round ties with FLOOR rule
- TIE_UP: round ties with CEILING rule
- TIE_TOWARDS_ZERO: round ties with TRUNCATE rule
- TIE_TO_ODD: round to nearest value; if exactly halfway, tie
to the odd option.
values: [ TIE_TO_EVEN, TIE_AWAY_FROM_ZERO, TRUNCATE, CEILING, FLOOR,
AWAY_FROM_ZERO, TIE_DOWN, TIE_UP, TIE_TOWARDS_ZERO, TIE_TO_ODD ]
nullability: DECLARED_OUTPUT
return: fp64?
- args:
- value: decimal<P1,S1>
name: "x"
description: >
Numerical expression to be rounded.
- value: decimal<38,0>
name: "s"
description: >
Number of decimal places to be rounded to.

When `s` is a positive number, the rounding
is performed to a `s` number of decimal places.

When `s` is a negative number, the rounding is
performed to the left side of the decimal point
as specified by `s`.
options:
rounding:
description: >
When a boundary is computed to lie somewhere between two values,
and this value cannot be exactly represented, this specifies how
to round it.

- TIE_TO_EVEN: round to nearest value; if exactly halfway, tie
to the even option.
- TIE_AWAY_FROM_ZERO: round to nearest value; if exactly
halfway, tie away from zero.
- TRUNCATE: always round toward zero.
- CEILING: always round toward positive infinity.
- FLOOR: always round toward negative infinity.
- AWAY_FROM_ZERO: round negative values with FLOOR rule, round positive values with CEILING rule
- TIE_DOWN: round ties with FLOOR rule
- TIE_UP: round ties with CEILING rule
- TIE_TOWARDS_ZERO: round ties with TRUNCATE rule
- TIE_TO_ODD: round to nearest value; if exactly halfway, tie
to the odd option.
values: [ TIE_TO_EVEN, TIE_AWAY_FROM_ZERO, TRUNCATE, CEILING, FLOOR,
AWAY_FROM_ZERO, TIE_DOWN, TIE_UP, TIE_TOWARDS_ZERO, TIE_TO_ODD ]
nullability: DECLARED_OUTPUT
return: |-
decimal?<38,S1>
-
name: "fixed_round"
description: >
Rounding the value `x` to `s` decimal places.
impls:
- args:
- value: decimal<P1,S1>
name: "x"
description: >
Numerical expression to be rounded.
- value: decimal<38,0>
constant: true
name: "s"
description: >
Number of decimal places to be rounded to.

When `s` is a positive number, the rounding
is performed to a `s` number of decimal places.

When `s` is a negative number, the rounding is
performed to the left side of the decimal point
as specified by `s`.
options:
rounding:
description: >
When a boundary is computed to lie somewhere between two values,
and this value cannot be exactly represented, this specifies how
to round it.

- TIE_TO_EVEN: round to nearest value; if exactly halfway, tie
to the even option.
- TIE_AWAY_FROM_ZERO: round to nearest value; if exactly
halfway, tie away from zero.
- TRUNCATE: always round toward zero.
- CEILING: always round toward positive infinity.
- FLOOR: always round toward negative infinity.
- AWAY_FROM_ZERO: round negative values with FLOOR rule, round positive values with CEILING rule
- TIE_DOWN: round ties with FLOOR rule
- TIE_UP: round ties with CEILING rule
- TIE_TOWARDS_ZERO: round ties with TRUNCATE rule
- TIE_TO_ODD: round to nearest value; if exactly halfway, tie
to the odd option.
values: [ TIE_TO_EVEN, TIE_AWAY_FROM_ZERO, TRUNCATE, CEILING, FLOOR,
AWAY_FROM_ZERO, TIE_DOWN, TIE_UP, TIE_TOWARDS_ZERO, TIE_TO_ODD ]
nullability: DECLARED_OUTPUT
return: |-
S2 = (s > 0) ? min(s, S1) : 0
P2 = (s > 0) ? ((S1 < s) ? (P1 + 1): P1) : ((P1 + s < S1) ? (S1 - s + 1) : (P1 + 1))
P2 = min(38, P2);
decimal?<P2,S2>
Loading