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

Implement modulo operator for MONEY/SMALLMONEY types #3401

Open
wants to merge 1 commit into
base: BABEL_5_X_DEV
Choose a base branch
from

Conversation

hopebo
Copy link
Contributor

@hopebo hopebo commented Jan 13, 2025

Description

Due to the missing modulo operator for MONEY/SMALLMONEY types, it will choose the other Postgres internal alternatives like int4mod and numeric_mod, which will involve additional implicit CASTing between MONEY/SMALLMONEY types and other types. These CASTings could cause loss of precision and wrong results. What's more, the result of calculation won't be MONEY/SMALLMONEY types any more.

Fix

Implement the modulo function and operator for MONEY/SMALLMONEY types. The reason why we don't need to implement the functions between MONEY/SMALLMONEY types and interger types is that it will choose the MONEY-MONEY/SMALLMONEY-SMALLMONEY functions to calculate. And the result type is expected.

Task: BABEL-5480

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.

@coveralls
Copy link
Collaborator

coveralls commented Jan 13, 2025

Pull Request Test Coverage Report for Build 12757649408

Details

  • 8 of 9 (88.89%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.006%) to 74.963%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contrib/babelfishpg_money/fixeddecimal.c 8 9 88.89%
Totals Coverage Status
Change from base Build 12742860948: 0.006%
Covered Lines: 46988
Relevant Lines: 62682

💛 - Coveralls

@hopebo hopebo force-pushed the babel_5480_dev17 branch 3 times, most recently from 5c824a3 to a428399 Compare January 13, 2025 21:32
Description
-----------
Due to the missing modulo operator for MONEY/SMALLMONEY types, it will choose
the other Postgres internal alternatives like int4mod and numeric_mod, which will
involve additional implicit CASTing between MONEY/SMALLMONEY types and other types.
These CASTings could cause loss of precision and wrong results. What's more, the result
of calculation won't be MONEY/SMALLMONEY types any more.

Fix
---
Implement the modulo function and operator for MONEY/SMALLMONEY types. The reason why we don't
need to implement the functions between MONEY/SMALLMONEY types and interger types is that it
will choose the MONEY-MONEY/SMALLMONEY-SMALLMONEY functions to calculate. And the result type is
expected.

Task: BABEL-5480
Signed-off-by: Bo Li <[email protected]>
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DROP OPERATOR IF EXISTS sys.% (sys.MONEY, sys.MONEY);
Copy link
Contributor

Choose a reason for hiding this comment

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

there could be user defined objects depend on this. so lets create try..catch.. block where we only create operator if it does not exist.

DO $$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_catalog.pg_operator WHERE oprleft = 'numeric'::pg_catalog.regtype and oprright = 'int4'::pg_catalog.regtype and oprnamespace = 'sys'::regnamespace and oprname = '<' and oprresult != 0) THEN
CREATE OPERATOR sys.< (
    LEFTARG = numeric,
    RIGHTARG = int4,
    FUNCTION = sys.numeric_int4_lt,
    COMMUTATOR = >,
    NEGATOR = >=,
    RESTRICT = scalarltsel,
    JOIN = scalarltjoinsel
);
END IF;
END $$;

@@ -542,3 +542,292 @@ smallmoney
1.8000
~~END~~


Copy link
Contributor

Choose a reason for hiding this comment

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

we should have upgrade test cases, prepare/verify/cleanup format

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.

4 participants