Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_4_X_DEV' into jira-babel…
Browse files Browse the repository at this point in the history
…-5155
  • Loading branch information
ahmed-shameem committed Jan 22, 2025
2 parents 6294946 + 08e8898 commit a88935b
Show file tree
Hide file tree
Showing 174 changed files with 3,013 additions and 1,339 deletions.
2 changes: 1 addition & 1 deletion .github/composite-actions/upload-coredump/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runs:


- name: Upload Coredumps
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: coredumps
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ jobs:
lcov --list lcov.info
- name: Upload Coverage Report for babelfishpg_tsql extension
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage_tsql_${{github.ref_name}}
path: contrib/babelfishpg_tsql/coverage/

- name: Upload Coverage Report for babelfishpg_tds extension
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage_tds_${{github.ref_name}}
path: contrib/babelfishpg_tds/coverage/

- name: Upload Coverage Report for babelfishpg_common extension
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage_common_${{github.ref_name}}
path: contrib/babelfishpg_common/coverage/

- name: Upload Coverage Report for babelfishpg_money extension
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage_money_${{github.ref_name}}
path: contrib/babelfishpg_money/coverage/
Expand All @@ -163,7 +163,7 @@ jobs:

- name: Upload CSV report with latest coverage numbers
if: (github.event_name == 'schedule')
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: csv_${{github.ref_name}}
path: contrib/${{github.ref_name}}.csv
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Upload Coverage Report for Babelfish Extensions
if: always() && steps.code-coverage-summary.outcome == 'success'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-babelfish-extensions-dotnet
path: contrib/dotnet-lcov.info
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/isolation-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
- name: Upload Coverage Report for Babelfish Extensions
if: always() && steps.code-coverage-summary.outcome == 'success'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-babelfish-extensions-isolation
path: contrib/isolation-lcov.info
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jdbc-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
- name: Upload Coverage Report for Babelfish Extensions
if: always() && steps.code-coverage-summary.outcome == 'success'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-babelfish-extensions-jdbc
path: contrib/jdbc-lcov.info
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/odbc-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Upload Coverage Report for Babelfish Extensions
if: always() && steps.code-coverage-summary.outcome == 'success'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-babelfish-extensions-odbc
path: contrib/odbc-lcov.info
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/pr-code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ jobs:
run: |
sudo apt-get install lcov
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
id: download-jdbc-coverage
with:
name:  coverage-babelfish-extensions-jdbc
path: contrib/

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
id: download-dotnet-coverage
with:
name: coverage-babelfish-extensions-dotnet
path: contrib/

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
id: download-odbc-coverage
with:
name: coverage-babelfish-extensions-odbc
path: contrib/

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
id: download-python-coverage
with:
name: coverage-babelfish-extensions-python
path: contrib/

- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
id: download-isolation-coverage
with:
name: coverage-babelfish-extensions-isolation
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Upload Coverage Report for Babelfish Extensions
if: always() && steps.code-coverage-summary.outcome == 'success'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-babelfish-extensions-python
path: contrib/python-lcov.info
Expand Down
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 @@ -67,5 +67,74 @@ FOR TYPE sys.SMALLDATETIME USING btree FAMILY smalldatetime_ops AS
END IF;
END $$;

CREATE OR REPLACE FUNCTION sys.fixeddecimal2varchar(sys.FIXEDDECIMAL, integer, BOOLEAN)
RETURNS sys.VARCHAR
AS 'babelfishpg_common', 'fixeddecimal2varchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.fixeddecimal2pgvarchar(sys.FIXEDDECIMAL, integer, BOOLEAN)
RETURNS pg_catalog.VARCHAR
AS 'babelfishpg_common', 'fixeddecimal2varchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN)
RETURNS sys.BPCHAR
AS 'babelfishpg_common', 'fixeddecimal2bpchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DO $$
DECLARE
exception_message text;
BEGIN
CREATE CAST (sys.FIXEDDECIMAL AS sys.VARCHAR)
WITH FUNCTION sys.fixeddecimal2varchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS pg_catalog.VARCHAR)
WITH FUNCTION sys.fixeddecimal2pgvarchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS sys.BPCHAR)
WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS pg_catalog.BPCHAR)
WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;
EXCEPTION WHEN duplicate_object THEN
GET STACKED DIAGNOSTICS
exception_message = MESSAGE_TEXT;
RAISE WARNING '%', exception_message;
END;
$$;

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);
22 changes: 22 additions & 0 deletions contrib/babelfishpg_common/sql/varchar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,28 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (sys.VARCHAR AS pg_catalog.NUMERIC)
WITH FUNCTION sys.varchar2numeric(sys.VARCHAR) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.fixeddecimal2varchar(sys.FIXEDDECIMAL, integer, BOOLEAN)
RETURNS sys.VARCHAR
AS 'babelfishpg_common', 'fixeddecimal2varchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN)
RETURNS sys.BPCHAR
AS 'babelfishpg_common', 'fixeddecimal2bpchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE CAST (sys.FIXEDDECIMAL AS sys.VARCHAR)
WITH FUNCTION sys.fixeddecimal2varchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS pg_catalog.VARCHAR)
WITH FUNCTION sys.fixeddecimal2varchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS sys.BPCHAR)
WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE CAST (sys.FIXEDDECIMAL AS pg_catalog.BPCHAR)
WITH FUNCTION sys.fixeddecimal2bpchar(sys.FIXEDDECIMAL, integer, BOOLEAN) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.nvarchar_larger(sys.NVARCHAR, sys.NVARCHAR)
RETURNS sys.NVARCHAR
AS 'text_larger'
Expand Down
86 changes: 86 additions & 0 deletions contrib/babelfishpg_common/src/varchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ static inline int varcharTruelen(VarChar *arg);

#define DEFAULT_LCID 1033

/* Linkage to function in fixeddecimal module */
typedef char *(*fixeddecimal2str_t) (int64 val, char *buffer,
int64 fixeddecimal_multiplier,
int64 fixeddecimal_scale);
static fixeddecimal2str_t fixeddecimal2str_p = NULL;

/*
* is_basetype_nchar_nvarchar - given datatype is nvarchar or nchar
* or created over nvarchar or nchar.
Expand Down Expand Up @@ -554,6 +560,8 @@ PG_FUNCTION_INFO_V1(varchar2date);
PG_FUNCTION_INFO_V1(varchar2time);
PG_FUNCTION_INFO_V1(varchar2money);
PG_FUNCTION_INFO_V1(varchar2numeric);
PG_FUNCTION_INFO_V1(fixeddecimal2varchar);
PG_FUNCTION_INFO_V1(fixeddecimal2bpchar);

/*****************************************************************************
* varchar - varchar(n)
Expand Down Expand Up @@ -1102,6 +1110,84 @@ varchar2numeric(PG_FUNCTION_ARGS)
PG_RETURN_NUMERIC(result);
}

#define FIXEDDECIMAL_2_VARCHAR_MULTIPLIER 100LL
#define FIXEDDECIMAL_2_VARCHAR_SCALE 2
Datum
fixeddecimal2varchar(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
int32 maxByteLen = PG_GETARG_INT32(1);
char buf[MAXINT8LEN + 1];
char *end;
int32 len;
Datum res;

/* fetch function pointer for cross-module calls. */
if (fixeddecimal2str_p == NULL)
fixeddecimal2str_p = (fixeddecimal2str_t)
load_external_function("$libdir/babelfishpg_money", "fixeddecimal2str", true, NULL);

end = fixeddecimal2str_p(val, buf, FIXEDDECIMAL_2_VARCHAR_MULTIPLIER, FIXEDDECIMAL_2_VARCHAR_SCALE);
len = (end - buf);

if (maxByteLen < 0)
maxByteLen = len + VARHDRSZ;
maxByteLen -= VARHDRSZ;
if (len > maxByteLen)
ereport(ERROR,
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
errmsg("There is insufficient result space to convert a money/smallmoney value to varchar/nvarchar.")));

res = DirectFunctionCall3(varcharin,
CStringGetDatum(buf),
ObjectIdGetDatum(0),
Int32GetDatum(-1));

PG_RETURN_DATUM(res);
}

Datum
fixeddecimal2bpchar(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
int32 maxByteLen = PG_GETARG_INT32(1);
char buf[MAXINT8LEN + 1];
char *buf_padded;
char *end;
int32 len;
Datum res;

/* fetch function pointer for cross-module calls. */
if (fixeddecimal2str_p == NULL)
fixeddecimal2str_p = (fixeddecimal2str_t)
load_external_function("$libdir/babelfishpg_money", "fixeddecimal2str", true, NULL);

end = fixeddecimal2str_p(val, buf, FIXEDDECIMAL_2_VARCHAR_MULTIPLIER, FIXEDDECIMAL_2_VARCHAR_SCALE);
len = (end - buf);

if (maxByteLen < 0)
maxByteLen = len + VARHDRSZ;
maxByteLen -= VARHDRSZ;
if (len > maxByteLen)
ereport(ERROR,
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
errmsg("There is insufficient result space to convert a money/smallmoney value to varchar/nvarchar.")));

/* Left pad money value with the spaces */
buf_padded = (char *) palloc(maxByteLen + 1);
memset(buf_padded, ' ', maxByteLen - len);
memcpy(buf_padded + maxByteLen - len, buf, len);
buf_padded[maxByteLen] = '\0';
res = DirectFunctionCall3(bpcharin,
CStringGetDatum(buf_padded),
ObjectIdGetDatum(0),
Int32GetDatum(-1));

PG_RETURN_DATUM(res);
}
#undef FIXEDDECIMAL_2_VARCHAR_MULTIPLIER
#undef FIXEDDECIMAL_2_VARCHAR_SCALE

/*****************************************************************************
* bpchar - char() *
*****************************************************************************/
Expand Down
Loading

0 comments on commit a88935b

Please sign in to comment.