Skip to content

Address issues with arithmetic, mathematical functions for money/smallmoney #3797

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

Open
wants to merge 21 commits into
base: BABEL_5_X_DEV
Choose a base branch
from

Conversation

ayushdsh
Copy link

@ayushdsh ayushdsh commented May 30, 2025

Description

Handling overflow and incorrect outputs in case of certain money and smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue #1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue #2: Unhandled overflow cases leading to incorrect output.
Issue #3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

In this commit, we have added new functions for all smallmoney operators and fixed certain overflows with money (fixeddecimal).

Issues Resolved

BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Test Scenarios Covered

Note: testcases are imported from PR-3568

  • Use case based -

  • All arithmetic and math functions for money and smallmoney

    • with all types of operands and vice-versa
  • Boundary conditions -

  • All boundary conditions for overflow

  • Arbitrary inputs -

  • Certain arbritrary input tests added

  • Negative test cases -

  • Overflow conditions checked which were previously causing protocol hang.

  • Minor version upgrade tests -

  • NA

  • Major version upgrade tests -

  • NA

  • Performance tests -

  • NA

  • Tooling impact -

  • NA

  • Client tests -

  • NA

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@ayushdsh ayushdsh changed the title Address issues with arithmetic, mathematical and aggregate functions for money/smallmoney Address issues with arithmetic, mathematical functions for money/smallmoney May 31, 2025
@coveralls
Copy link
Collaborator

coveralls commented Jun 1, 2025

Pull Request Test Coverage Report for Build 15497840812

Details

  • 285 of 322 (88.51%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 75.58%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contrib/babelfishpg_money/fixeddecimal.c 38 41 92.68%
contrib/babelfishpg_common/src/bit.c 6 12 50.0%
contrib/babelfishpg_money/smallmoney.c 241 269 89.59%
Totals Coverage Status
Change from base Build 15464616681: 0.1%
Covered Lines: 49098
Relevant Lines: 64962

💛 - Coveralls

@ayushdsh ayushdsh marked this pull request as ready for review June 2, 2025 05:43
Comment on lines +1834 to +1852
if (adder != (int64) adder)
{
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("fixeddecimal out of range")));
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
PG_RETURN_NULL();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition and the below condition where we check overflow are required in the following scenario -
When arg2 = INT64_MAX, adder itself overflows and the existing overflow check condition assumes that the adder was always negative. Hence, we first check if the adder overflows and terminate if it does.

Comment on lines +1878 to +1896
if (subtractor != (int64) subtractor)
{
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("fixeddecimal out of range")));
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
PG_RETURN_NULL();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SImilarly, if arg2 = INT64_MAX, this leads to subtractor overflow and becomes -ve and the overflow check is not correct because it assumes that the arg2 is -ve.

Comment on lines +2138 to +2148
/*
division with values > FIXEDDECIMAL_MAX lead to overflow
in T-SQL
*/
if (arg1 > FIXEDDECIMAL_MAX)
{
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("fixeddecimal out of range")));
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
PG_RETURN_NULL();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a TSQL behaviour which we did not respect previously. This condition takes care of the same.

Comment on lines -1165 to +1166
numeric#!#datetime#!#datetime#!#bigint#!#int#!#smallint#!#smallint#!#money#!#datetime#!#varchar#!#datetime#!#bigint
100.50#!#1900-04-11 12:00:00.0#!#1900-04-11 12:00:00.0#!#1000#!#100#!#10#!#1#!#100.5000#!#1900-01-01 00:00:00.0#!#0A1A2A3A4-B1B2-C1C2-D1D2-E1E2E3E4E5E6#!#<NULL>#!#0
numeric#!#datetime#!#smallmoney#!#bigint#!#int#!#smallint#!#smallint#!#money#!#bit#!#varchar#!#datetime#!#bigint
100.50#!#1900-04-11 12:00:00.0#!#100.5000#!#1000#!#100#!#10#!#1#!#100.5000#!#0#!#0A1A2A3A4-B1B2-C1C2-D1D2-E1E2E3E4E5E6#!#<NULL>#!#0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the changes are because we introduced new operators for bit. Now, bit + smallmoney results into smallmoney, which previously resulted into datetime and bit + bit results into bit which previously resulted into datetime.

@@ -0,0 +1,2845 @@
-- Create test tables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will also need to create prepare and verify test file for this. Upgrade tests should include

  1. Create views in prepare to cover all operators we are fixing.
  2. Performance - create a table in prepare with money small money datatype. in verify ensure that index scan is picked for queries on these tables when the predicate has this operator. We can also keep changing the type on one end of the operator.
create table t (id int small money)
create index idx on t(id)

SELECT * FROM t WHERE id (some_operator) (different data types)

ayushdsh added 17 commits June 6, 2025 11:19
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
@ayushdsh ayushdsh closed this Jun 6, 2025
@ayushdsh ayushdsh force-pushed the smallmoney-issues-fix branch from 62ee33e to de3d86e Compare June 6, 2025 16:00
@ayushdsh ayushdsh reopened this Jun 6, 2025
ayushdsh added 4 commits June 6, 2025 16:27
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
…smallmoney arithmetic and math functions.

There were certain issues found both w.r.t overflow handling and certain functions and operations giving incorrect result for money and smallmoney datatype. This commit addresses those issues.

Issue babelfish-for-postgresql#1: Overflow of smallmoney arithmetic leading to TDS hang.
Issue babelfish-for-postgresql#2: Unhandled overflow cases leading to incorrect output.
Issue babelfish-for-postgresql#3: Fixeddecimal operators being resolved for smallmoney functions and operations leading to incorrect results.

Task: BABEL-5745, BABEL-5757, BABEL-5756, BABEL-5747, BABEL-5754

Signed-off-by: Ayush Shah <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants