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
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ ALTER OPERATOR FAMILY fixeddecimal_ops USING hash ADD
CREATE DOMAIN sys.MONEY as sys.FIXEDDECIMAL CHECK (VALUE >= -922337203685477.5808 AND VALUE <= 922337203685477.5807);
CREATE DOMAIN sys.SMALLMONEY as sys.FIXEDDECIMAL CHECK (VALUE >= -214748.3648 AND VALUE <= 214748.3647);

-- Define modulo operator directly on MONEY type.
-- Otherwise the operator between Integer and SMALLMONEY will tend to choose the fixeddecimal version,
-- which will return the result in MONEY type.
CREATE FUNCTION sys.fixeddecimalmod(sys.MONEY, sys.MONEY)
RETURNS sys.MONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.% (
LEFTARG = sys.MONEY,
RIGHTARG = sys.MONEY,
PROCEDURE = fixeddecimalmod
);

--
-- Cross type operators with int8
--
Expand Down Expand Up @@ -1645,6 +1659,11 @@ RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimaldiv'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.fixeddecimalmod(sys.SMALLMONEY, sys.SMALLMONEY)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.+ (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
Expand Down Expand Up @@ -1676,6 +1695,12 @@ CREATE OPERATOR sys./ (
PROCEDURE = fixeddecimaldiv
);

CREATE OPERATOR sys.% (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
PROCEDURE = fixeddecimalmod
);

CREATE FUNCTION sys.fixeddecimalint8pl(sys.SMALLMONEY, INT8)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalint8pl'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,37 @@ WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMP
CREATE CAST (sys.FIXEDDECIMAL AS pg_catalog.BPCHAR)
WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.fixeddecimalmod(sys.MONEY, sys.MONEY)
RETURNS sys.MONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_operator WHERE oprleft = 'sys.money'::pg_catalog.regtype and oprright = 'sys.money'::pg_catalog.regtype and oprnamespace = 'sys'::regnamespace and oprname = '%' and oprresult != 0) THEN
CREATE OPERATOR sys.% (
LEFTARG = sys.MONEY,
RIGHTARG = sys.MONEY,
PROCEDURE = fixeddecimalmod
);
END IF;
END $$;

CREATE OR REPLACE FUNCTION sys.fixeddecimalmod(sys.SMALLMONEY, sys.SMALLMONEY)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_operator WHERE oprleft = 'sys.smallmoney'::pg_catalog.regtype and oprright = 'sys.smallmoney'::pg_catalog.regtype and oprnamespace = 'sys'::regnamespace and oprname = '%' and oprresult != 0) THEN
CREATE OPERATOR sys.% (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
PROCEDURE = fixeddecimalmod
);
END IF;
END $$;

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);
27 changes: 27 additions & 0 deletions contrib/babelfishpg_money/fixeddecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ PG_FUNCTION_INFO_V1(fixeddecimalpl);
PG_FUNCTION_INFO_V1(fixeddecimalmi);
PG_FUNCTION_INFO_V1(fixeddecimalmul);
PG_FUNCTION_INFO_V1(fixeddecimaldiv);
PG_FUNCTION_INFO_V1(fixeddecimalmod);
PG_FUNCTION_INFO_V1(fixeddecimalabs);
PG_FUNCTION_INFO_V1(fixeddecimallarger);
PG_FUNCTION_INFO_V1(fixeddecimalsmaller);
Expand Down Expand Up @@ -1772,6 +1773,32 @@ fixeddecimaldiv(PG_FUNCTION_ARGS)
PG_RETURN_INT64((int64) result);
}

Datum
fixeddecimalmod(PG_FUNCTION_ARGS)
{
int64 dividend = PG_GETARG_INT64(0);
int64 divisor = PG_GETARG_INT64(1);

if (divisor == 0)
{
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
PG_RETURN_NULL();
}

/*
» * Some machines throw a floating-point exception for INT_MIN % -1, which
» * is a bit silly since the correct answer is perfectly well-defined,
» * namely zero. Refer to function int4mod in Postgres.
» */
if (divisor == -1)
PG_RETURN_INT64(0);

PG_RETURN_INT64(dividend % divisor);
}

/* fixeddecimalabs()
* Absolute value
*/
Expand Down
Loading