Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_5_X_DEV' into pgAudit_5x…
Browse files Browse the repository at this point in the history
…_action_run
  • Loading branch information
pranavJ23 committed Jan 14, 2025
2 parents 8d28337 + cf94423 commit cfc142c
Show file tree
Hide file tree
Showing 370 changed files with 161,783 additions and 11,911 deletions.
12 changes: 12 additions & 0 deletions .github/configuration/upgrade-test-configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,17 @@ upgrade-version: [{
}
],
babelfishpg_tsql_server_collation_name: chinese_prc_ci_as
},
{
upgrade-path: [
{
version: 16.7,
upgrade-type: null
},
{
version: target.latest,
upgrade-type: major
}
]
}
]
10 changes: 10 additions & 0 deletions contrib/babelfishpg_common/sql/binary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (sys.VARCHAR AS sys.BBF_BINARY)
WITH FUNCTION sys.varcharbinary (sys.VARCHAR, integer, boolean) AS ASSIGNMENT;

CREATE OR REPLACE FUNCTION sys.nvarcharbinary(sys.NVARCHAR, integer, boolean)
RETURNS sys.BBF_BINARY
AS 'babelfishpg_common', 'nvarcharbinary'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.varcharbinary(pg_catalog.VARCHAR, integer, boolean)
RETURNS sys.BBF_BINARY
AS 'babelfishpg_common', 'varcharbinary'
Expand Down Expand Up @@ -99,6 +104,11 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (sys.BBF_BINARY AS sys.VARCHAR)
WITH FUNCTION sys.binarysysvarchar (sys.BBF_BINARY, integer, boolean) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.binarysysnvarchar(sys.BBF_BINARY, integer, boolean)
RETURNS sys.NVARCHAR
AS 'babelfishpg_common', 'varbinarynvarchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.binaryvarchar(sys.BBF_BINARY, integer, boolean)
RETURNS pg_catalog.VARCHAR
AS 'babelfishpg_common', 'varbinaryvarchar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,69 @@

SELECT set_config('search_path', 'sys, '||current_setting('search_path'), false);

CREATE OR REPLACE FUNCTION sys.nvarcharvarbinary(sys.NVARCHAR, integer, boolean)
RETURNS sys.BBF_VARBINARY
AS 'babelfishpg_common', 'nvarcharvarbinary'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.varbinarysysnvarchar(sys.BBF_VARBINARY, integer, boolean)
RETURNS sys.NVARCHAR
AS 'babelfishpg_common', 'varbinarynvarchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.binarysysnvarchar(sys.BBF_BINARY, integer, boolean)
RETURNS sys.NVARCHAR
AS 'babelfishpg_common', 'varbinarynvarchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.nvarcharbinary(sys.NVARCHAR, integer, boolean)
RETURNS sys.BBF_BINARY
AS 'babelfishpg_common', 'nvarcharbinary'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;


-- Please add your SQLs here
/*
* Note: These SQL statements may get executed multiple times specially when some features get backpatched.
* So make sure that any SQL statement (DDL/DML) being added here can be executed multiple times without affecting
* final behaviour.
*/
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;
$$;

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);
10 changes: 10 additions & 0 deletions contrib/babelfishpg_common/sql/varbinary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (sys.BBF_VARBINARY AS pg_catalog.BYTEA)
WITH FUNCTION sys.varbinarybytea(sys.BBF_VARBINARY, integer, boolean) AS ASSIGNMENT;

CREATE OR REPLACE FUNCTION sys.nvarcharvarbinary(sys.NVARCHAR, integer, boolean)
RETURNS sys.BBF_VARBINARY
AS 'babelfishpg_common', 'nvarcharvarbinary'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.varcharvarbinary(sys.VARCHAR, integer, boolean)
RETURNS sys.BBF_VARBINARY
AS 'babelfishpg_common', 'varcharvarbinary'
Expand Down Expand Up @@ -111,6 +116,11 @@ LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
CREATE CAST (sys.BBF_VARBINARY AS sys.VARCHAR)
WITH FUNCTION sys.varbinarysysvarchar (sys.BBF_VARBINARY, integer, boolean) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.varbinarysysnvarchar(sys.BBF_VARBINARY, integer, boolean)
RETURNS sys.NVARCHAR
AS 'babelfishpg_common', 'varbinarynvarchar'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.varbinaryvarchar(sys.BBF_VARBINARY, integer, boolean)
RETURNS pg_catalog.VARCHAR
AS 'babelfishpg_common', 'varbinaryvarchar'
Expand Down
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
1 change: 1 addition & 0 deletions contrib/babelfishpg_common/src/babelfishpg_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ get_common_utility_plugin(void)
common_utility_plugin_var.resolve_pg_type_to_tsql = &resolve_pg_type_to_tsql;
common_utility_plugin_var.GetUTF8CodePoint = &GetUTF8CodePoint;
common_utility_plugin_var.TsqlUTF8LengthInUTF16 = &TsqlUTF8LengthInUTF16;
common_utility_plugin_var.TsqlUTF8toUTF16StringInfo = &TsqlUTF8toUTF16StringInfo;
}
return &common_utility_plugin_var;
}
1 change: 1 addition & 0 deletions contrib/babelfishpg_common/src/babelfishpg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ typedef struct common_utility_plugin
const char *(*resolve_pg_type_to_tsql) (Oid oid);
int32_t (*GetUTF8CodePoint) (const unsigned char *in, int len, int *consumed_p);
int (*TsqlUTF8LengthInUTF16) (const void *vin, int len);
void (*TsqlUTF8toUTF16StringInfo) (StringInfo utf16_data, const void *data, size_t len);
} common_utility_plugin;
86 changes: 68 additions & 18 deletions contrib/babelfishpg_common/src/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,8 @@ timestamp_diff(PG_FUNCTION_ARGS)
int32 seconddiff;
int32 millisecdiff;
int32 microsecdiff;
int32 days_in_timestamp1;
int32 days_in_timestamp2;
struct pg_tm tt1,
*tm1 = &tt1;
fsec_t fsec1;
Expand All @@ -1340,16 +1342,16 @@ timestamp_diff(PG_FUNCTION_ARGS)
type = DecodeUnits(0, lowunits, &val);

// Decode units does not handle doy properly
if(strncmp(lowunits, "doy", 3) == 0) {
if(strlen(lowunits) == 3 && strncmp(lowunits, "doy", 3) == 0) {
type = UNITS;
val = DTK_DOY;
}

if(strncmp(lowunits, "nanosecond", 11) == 0) {
if(strlen(lowunits) == 10 && strncmp(lowunits, "nanosecond", 10) == 0) {
type = UNITS;
val = DTK_NANO;
}
if(strncmp(lowunits, "weekday", 7) == 0) {
if(strlen(lowunits) == 7 && strncmp(lowunits, "weekday", 7) == 0) {
type = UNITS;
val = DTK_DAY;
}
Expand All @@ -1364,17 +1366,40 @@ timestamp_diff(PG_FUNCTION_ARGS)
yeardiff = tm2->tm_year - tm1->tm_year;
monthdiff = tm2->tm_mon - tm1->tm_mon;
diff = (yeardiff * 12 + monthdiff) / 3;
/* Calculate if quarter boundary is crossed for the remaining months */
if (monthdiff % 3 > 0)
{
if (yeardiff >= 0 && ((tm1->tm_mon - 1) % 3 > (tm2->tm_mon - 1) % 3))
diff++;
else if (yeardiff < 0 && ((tm1->tm_mon - 1) % 3 < (tm2->tm_mon - 1) % 3))
diff--;
}
else if (monthdiff % 3 < 0)
{
if (yeardiff > 0 && ((tm1->tm_mon - 1) % 3 > (tm2->tm_mon - 1) % 3))
diff++;
else if (yeardiff <= 0 && ((tm1->tm_mon - 1) % 3 < (tm2->tm_mon - 1) % 3))
diff--;
}
break;
case DTK_MONTH:
yeardiff = tm2->tm_year - tm1->tm_year;
monthdiff = tm2->tm_mon - tm1->tm_mon;
diff = yeardiff * 12 + monthdiff;
break;
case DTK_WEEK:
daydiff = days_in_date(tm2->tm_mday, tm2->tm_mon, tm2->tm_year) - days_in_date(tm1->tm_mday, tm1->tm_mon, tm1->tm_year);
days_in_timestamp1 = days_in_date(tm1->tm_mday, tm1->tm_mon, tm1->tm_year);
days_in_timestamp2 = days_in_date(tm2->tm_mday, tm2->tm_mon, tm2->tm_year);
daydiff = days_in_timestamp2 - days_in_timestamp1;
diff = daydiff / 7;
if(daydiff % 7 >= 4)
diff++;
/* Calculate if saturday-sunday boundary is crossed for the remaining days */
if (abs(daydiff) % 7 > ((Max(days_in_timestamp1, days_in_timestamp2) - 1) % 7))
{
if (daydiff < 0)
diff--;
else
diff++;
}
break;
case DTK_DAY:
case DTK_DOY:
Expand Down Expand Up @@ -1462,7 +1487,7 @@ timestamp_diff(PG_FUNCTION_ARGS)
if(overflow) {
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart")));
errmsg("The %s function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use %s with a less precise datepart.", "datediff", "datediff")));
}

PG_RETURN_INT32(diff);
Expand All @@ -1489,6 +1514,8 @@ timestamp_diff_big(PG_FUNCTION_ARGS)
int64 seconddiff;
int64 millisecdiff;
int64 microsecdiff;
int64 days_in_timestamp1;
int64 days_in_timestamp2;
struct pg_tm tt1,
*tm1 = &tt1;
fsec_t fsec1;
Expand All @@ -1511,15 +1538,15 @@ timestamp_diff_big(PG_FUNCTION_ARGS)
type = DecodeUnits(0, lowunits, &val);

// Decode units does not handle doy or nano properly
if(strncmp(lowunits, "doy", 3) == 0) {
if(strlen(lowunits) == 3 && strncmp(lowunits, "doy", 3) == 0) {
type = UNITS;
val = DTK_DOY;
}
if(strncmp(lowunits, "nanosecond", 11) == 0) {
if(strlen(lowunits) == 10 && strncmp(lowunits, "nanosecond", 10) == 0) {
type = UNITS;
val = DTK_NANO;
}
if(strncmp(lowunits, "weekday", 7) == 0) {
if(strlen(lowunits) == 7 && strncmp(lowunits, "weekday", 7) == 0) {
type = UNITS;
val = DTK_DAY;
}
Expand All @@ -1535,17 +1562,40 @@ timestamp_diff_big(PG_FUNCTION_ARGS)
yeardiff = tm2->tm_year - tm1->tm_year;
monthdiff = tm2->tm_mon - tm1->tm_mon;
diff = (yeardiff * 12 + monthdiff) / 3;
/* Calculate if quarter boundary is crossed for the remaining months */
if (monthdiff % 3 > 0)
{
if (yeardiff >= 0 && ((tm1->tm_mon - 1) % 3 > (tm2->tm_mon - 1) % 3))
diff++;
else if (yeardiff < 0 && ((tm1->tm_mon - 1) % 3 < (tm2->tm_mon - 1) % 3))
diff--;
}
else if (monthdiff % 3 < 0)
{
if (yeardiff > 0 && ((tm1->tm_mon - 1) % 3 > (tm2->tm_mon - 1) % 3))
diff++;
else if (yeardiff <= 0 && ((tm1->tm_mon - 1) % 3 < (tm2->tm_mon - 1) % 3))
diff--;
}
break;
case DTK_MONTH:
yeardiff = tm2->tm_year - tm1->tm_year;
monthdiff = tm2->tm_mon - tm1->tm_mon;
diff = yeardiff * 12 + monthdiff;
break;
case DTK_WEEK:
daydiff = days_in_date(tm2->tm_mday, tm2->tm_mon, tm2->tm_year) - days_in_date(tm1->tm_mday, tm1->tm_mon, tm1->tm_year);
days_in_timestamp1 = days_in_date(tm1->tm_mday, tm1->tm_mon, tm1->tm_year);
days_in_timestamp2 = days_in_date(tm2->tm_mday, tm2->tm_mon, tm2->tm_year);
daydiff = days_in_timestamp2 - days_in_timestamp1;
diff = daydiff / 7;
if(daydiff % 7 >= 4)
diff++;
/* Calculate if saturday-sunday boundary is crossed for the remaining days */
if (abs(daydiff) % 7 > ((Max(days_in_timestamp1, days_in_timestamp2) - 1) % 7))
{
if (daydiff < 0)
diff--;
else
diff++;
}
break;
case DTK_DAY:
case DTK_DOY:
Expand Down Expand Up @@ -1627,12 +1677,12 @@ timestamp_diff_big(PG_FUNCTION_ARGS)
if(!validDateDiff) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\'%s\' is not a recognized %s option", lowunits, "datediff")));
errmsg("\'%s\' is not a recognized %s option", lowunits, "datediff_big")));
}
if(overflow) {
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart")));
errmsg("The %s function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use %s with a less precise datepart.", "datediff_big", "datediff_big")));
}

PG_RETURN_INT64(diff);
Expand Down Expand Up @@ -1738,15 +1788,15 @@ dateadd_datetime(PG_FUNCTION_ARGS) {

type = DecodeUnits(0, lowunits, &val);

if(strncmp(lowunits, "doy", 3) == 0 || strncmp(lowunits, "dayofyear", 9) == 0) {
if((strlen(lowunits) == 3 && strncmp(lowunits, "doy", 3) == 0) || (strlen(lowunits) == 9 && strncmp(lowunits, "dayofyear", 9) == 0)) {
type = UNITS;
val = DTK_DOY;
}
if(strncmp(lowunits, "nanosecond", 11) == 0) {
if(strlen(lowunits) == 10 && strncmp(lowunits, "nanosecond", 10) == 0) {
type = UNITS;
val = DTK_NANO;
}
if(strncmp(lowunits, "weekday", 7) == 0) {
if(strlen(lowunits) == 7 && strncmp(lowunits, "weekday", 7) == 0) {
type = UNITS;
val = DTK_DAY;
}
Expand Down
Loading

0 comments on commit cfc142c

Please sign in to comment.