Skip to content

Commit

Permalink
Merge branch 'babelfish-for-postgresql:BABEL_4_X_DEV' into babel_4891…
Browse files Browse the repository at this point in the history
…_hashbytes
  • Loading branch information
pranavJ23 authored Jan 6, 2025
2 parents 6785057 + 928987e commit 76fa832
Show file tree
Hide file tree
Showing 68 changed files with 1,843 additions and 149 deletions.
4 changes: 2 additions & 2 deletions .github/composite-actions/install-and-run-dotnet/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ runs:
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get install -y apt-transport-https
sudo apt-get install -y dotnet-sdk-5.0
sudo apt-get install -y dotnet-sdk-8.0
sudo apt-get install -y apt-transport-https
sudo apt-get install -y aspnetcore-runtime-5.0
sudo apt-get install -y aspnetcore-runtime-8.0
shell: bash

- name: Run Dotnet Tests
Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_common/src/collation.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define DATABASE_DEFAULT "database_default"
#define CATALOG_DEFAULT "catalog_default"

collation_callbacks collation_callbacks_var = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
collation_callbacks collation_callbacks_var = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

/* Cached values derived from server_collation_name */
static int server_collation_collidx = NOT_FOUND;
Expand Down
12 changes: 10 additions & 2 deletions contrib/babelfishpg_tds/src/backend/tds/tds.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ typedef struct LocalTdsStatus
} LocalTdsStatus;

static TdsStatus *TdsStatusArray = NULL;
static TdsStatus *MyTdsStatusEntry;
static TdsStatus *MyTdsStatusEntry = NULL;
static LocalTdsStatus *localTdsStatusTable = NULL;

uint32_t MyTdsClientVersion = 0;
Expand Down Expand Up @@ -446,9 +446,17 @@ tds_stats_shmem_shutdown(int code, Datum arg)
return;

/* Safety check ... shouldn't get here unless shmem is set up. */
if (TdsStatusArray == NULL)
if (TdsStatusArray == NULL || MyTdsStatusEntry == NULL)
return;

PGSTAT_BEGIN_WRITE_ACTIVITY(MyTdsStatusEntry);

MyTdsStatusEntry->st_procpid = 0; /* mark invalid */

PGSTAT_END_WRITE_ACTIVITY(MyTdsStatusEntry);

MyTdsStatusEntry = NULL;

return;
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/sql/information_schema_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ BEGIN
OR has_any_column_privilege(r.oid, 'SELECT, INSERT, UPDATE, REFERENCES') );
END;
$$
LANGUAGE plpgsql STABLE;
LANGUAGE plpgsql STABLE PARALLEL SAFE;

/*
* TABLE_CONSTRAINTS view
Expand Down
36 changes: 14 additions & 22 deletions contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,7 @@ DECLARE
tz_offset PG_CATALOG.TEXT;
tz_name PG_CATALOG.TEXT;
lower_tzn PG_CATALOG.TEXT;
prev_res PG_CATALOG.TEXT;
result PG_CATALOG.TEXT;
is_dstt bool;
tz_diff PG_CATALOG.TEXT;
input_expr_tx PG_CATALOG.TEXT;
input_expr_tmz TIMESTAMPTZ;
Expand All @@ -1193,31 +1191,25 @@ BEGIN
END IF;

IF pg_typeof(input_expr) IN ('sys.smalldatetime'::regtype, 'sys.datetime'::regtype, 'sys.datetime2'::regtype) THEN
input_expr_tx := input_expr::TEXT;
input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;

tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;
if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;
tz_offset := PG_CATALOG.left(tz_diff,6);
input_expr_tx := PG_CATALOG.concat(input_expr_tx,tz_offset);
return cast(input_expr_tx as sys.datetimeoffset);
input_expr_tx := input_expr::TEXT || ' ' || tz_name;
ELSIF pg_typeof(input_expr) = 'sys.DATETIMEOFFSET'::regtype THEN
input_expr_tx := input_expr::TEXT;
input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;
result := (SELECT input_expr_tmz AT TIME ZONE tz_name)::TEXT;
tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;
if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;
tz_offset := PG_CATALOG.left(tz_diff,6);
result := PG_CATALOG.concat(result,tz_offset);
return cast(result as sys.datetimeoffset);
ELSE
RAISE USING MESSAGE := 'Argument data type varchar is invalid for argument 1 of AT TIME ZONE function.';
END IF;


input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;
result := (SELECT input_expr_tmz AT TIME ZONE tz_name)::TEXT;
tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;

if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;

tz_offset := PG_CATALOG.left(tz_diff,6);
result := PG_CATALOG.concat(result,tz_offset);

return cast(result as sys.datetimeoffset);
END;
$BODY$
LANGUAGE 'plpgsql' STABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2763,9 +2763,7 @@ DECLARE
tz_offset PG_CATALOG.TEXT;
tz_name PG_CATALOG.TEXT;
lower_tzn PG_CATALOG.TEXT;
prev_res PG_CATALOG.TEXT;
result PG_CATALOG.TEXT;
is_dstt bool;
tz_diff PG_CATALOG.TEXT;
input_expr_tx PG_CATALOG.TEXT;
input_expr_tmz TIMESTAMPTZ;
Expand All @@ -2786,31 +2784,25 @@ BEGIN
END IF;

IF pg_typeof(input_expr) IN ('sys.smalldatetime'::regtype, 'sys.datetime'::regtype, 'sys.datetime2'::regtype) THEN
input_expr_tx := input_expr::TEXT;
input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;

tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;
if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;
tz_offset := PG_CATALOG.left(tz_diff,6);
input_expr_tx := PG_CATALOG.concat(input_expr_tx,tz_offset);
return cast(input_expr_tx as sys.datetimeoffset);
input_expr_tx := input_expr::TEXT || ' ' || tz_name;
ELSIF pg_typeof(input_expr) = 'sys.DATETIMEOFFSET'::regtype THEN
input_expr_tx := input_expr::TEXT;
input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;
result := (SELECT input_expr_tmz AT TIME ZONE tz_name)::TEXT;
tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;
if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;
tz_offset := PG_CATALOG.left(tz_diff,6);
result := PG_CATALOG.concat(result,tz_offset);
return cast(result as sys.datetimeoffset);
ELSE
RAISE USING MESSAGE := 'Argument data type varchar is invalid for argument 1 of AT TIME ZONE function.';
END IF;


input_expr_tmz := input_expr_tx :: TIMESTAMPTZ;
result := (SELECT input_expr_tmz AT TIME ZONE tz_name)::TEXT;
tz_diff := (SELECT input_expr_tmz AT TIME ZONE tz_name - input_expr_tmz AT TIME ZONE 'UTC')::TEXT;

if PG_CATALOG.LEFT(tz_diff,1) <> '-' THEN
tz_diff := PG_CATALOG.concat('+',tz_diff);
END IF;

tz_offset := PG_CATALOG.left(tz_diff,6);
result := PG_CATALOG.concat(result,tz_offset);

return cast(result as sys.datetimeoffset);
END;
$BODY$
LANGUAGE 'plpgsql' STABLE;
Expand Down Expand Up @@ -11518,7 +11510,7 @@ BEGIN
OR has_any_column_privilege(r.oid, 'SELECT, INSERT, UPDATE, REFERENCES') );
END;
$$
LANGUAGE plpgsql STABLE;
LANGUAGE plpgsql STABLE PARALLEL SAFE;

CREATE OR REPLACE VIEW information_schema_tsql.table_constraints AS
SELECT
Expand Down
60 changes: 60 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static PlannedStmt *pltsql_planner_hook(Query *parse, const char *query_string,
static Oid set_param_collation(Param *param);
static Oid default_collation_for_builtin_type(Type typ, bool handle_text);
static char* pltsql_get_object_identity_event_trigger(ObjectAddress *addr);
static const char *remove_db_name_in_schema(const char *schema_name, const char *object_type);

/***************************************************
* Temp Table Related Declarations + Hooks
Expand Down Expand Up @@ -240,6 +241,7 @@ static pre_transform_setop_sort_clause_hook_type prev_pre_transform_setop_sort_c
static pre_transform_target_entry_hook_type prev_pre_transform_target_entry_hook = NULL;
static tle_name_comparison_hook_type prev_tle_name_comparison_hook = NULL;
static get_trigger_object_address_hook_type prev_get_trigger_object_address_hook = NULL;
static remove_db_name_in_schema_hook_type prev_remove_db_name_in_schema_hook = NULL;
static resolve_target_list_unknowns_hook_type prev_resolve_target_list_unknowns_hook = NULL;
static find_attr_by_name_from_column_def_list_hook_type prev_find_attr_by_name_from_column_def_list_hook = NULL;
static find_attr_by_name_from_relation_hook_type prev_find_attr_by_name_from_relation_hook = NULL;
Expand Down Expand Up @@ -342,6 +344,9 @@ InstallExtendedHooks(void)
prev_get_trigger_object_address_hook = get_trigger_object_address_hook;
get_trigger_object_address_hook = get_trigger_object_address;

prev_remove_db_name_in_schema_hook = remove_db_name_in_schema_hook;
remove_db_name_in_schema_hook = remove_db_name_in_schema;

prev_resolve_target_list_unknowns_hook = resolve_target_list_unknowns_hook;
resolve_target_list_unknowns_hook = resolve_target_list_unknowns;

Expand Down Expand Up @@ -531,6 +536,7 @@ UninstallExtendedHooks(void)
pre_transform_target_entry_hook = prev_pre_transform_target_entry_hook;
tle_name_comparison_hook = prev_tle_name_comparison_hook;
get_trigger_object_address_hook = prev_get_trigger_object_address_hook;
remove_db_name_in_schema_hook = prev_remove_db_name_in_schema_hook;
resolve_target_list_unknowns_hook = prev_resolve_target_list_unknowns_hook;
find_attr_by_name_from_column_def_list_hook = prev_find_attr_by_name_from_column_def_list_hook;
find_attr_by_name_from_relation_hook = prev_find_attr_by_name_from_relation_hook;
Expand Down Expand Up @@ -5864,3 +5870,57 @@ is_bbf_db_ddladmin_operation(Oid namespaceId)

return false;
}

/*
* remove_db_name_in_schema - remove the db name and underscore at the beginning
* of the given string. It is used to unmap schema name in error messages.
*
* @param object_name - char *
* @param object_type - char *, can be 'sch' or 'func'
* @return - unmapped schema name char *
*/
static const char *
remove_db_name_in_schema(const char *object_name, const char *object_type)
{
char *cur_db_name;
char **splited_object_name;
char *schema_name = NULL;
char *mutable_name;
size_t db_name_len;
size_t prefix_len = 0;
size_t schema_name_len;

mutable_name = pstrdup(object_name);
splited_object_name = split_object_name(mutable_name);

if (strcmp(object_type, "sch") == 0) {
/* If input is 'sch_name' format, consider when there is only one part in splited_object_name */
/* If there are two parts or more, then it’s a cross-db object name (db.sch_name) so we don’t need to deal with it */
if (strlen(splited_object_name[2]) == 0 && strlen(splited_object_name[1]) == 0)
schema_name = splited_object_name[3];
} else if (strcmp(object_type, "func") == 0) {
/* If input is 'func_name' format, consider when there is two parts in splited_object_name, like db1_sch.db1_func */
if (strlen(splited_object_name[2]) > 0 && strlen(splited_object_name[1]) == 0)
schema_name = splited_object_name[2];
}

if (schema_name)
{
cur_db_name = get_cur_db_name();
db_name_len = strlen(cur_db_name);
schema_name_len = strlen(schema_name);

if (schema_name_len > db_name_len && strncmp(schema_name, cur_db_name, db_name_len) == 0 && schema_name[db_name_len] == '_') {
/* Return the part after the prefix */
prefix_len = db_name_len + 1;
}
pfree(cur_db_name);
}

pfree(mutable_name);
for (int k = 0; k < 4; k++)
pfree(splited_object_name[k]);
pfree(splited_object_name);

return (const char *)pstrdup(object_name + prefix_len);
}
5 changes: 3 additions & 2 deletions contrib/babelfishpg_tsql/src/json_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS)

funcctx = SRF_FIRSTCALL_INIT();
prev_sql_dialect = sql_dialect;
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
PG_TRY();
{
Jsonb *sub_jb;
Expand All @@ -317,7 +318,6 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS)
* processing
*/
sql_dialect = SQL_DIALECT_TSQL;
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

/*
* Get information about return type. Used to build return message
Expand Down Expand Up @@ -377,9 +377,10 @@ tsql_openjson_with_internal(PG_FUNCTION_ARGS)
PG_FINALLY();
{
sql_dialect = prev_sql_dialect;
MemoryContextSwitchTo(oldcontext);
}
PG_END_TRY();

MemoryContextSwitchTo(oldcontext);
}

funcctx = SRF_PERCALL_SETUP();
Expand Down
5 changes: 4 additions & 1 deletion contrib/babelfishpg_tsql/src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void
set_cur_db_name_for_parallel_worker(const char* logical_db_name)
{
int len;
int16 db_id;

if (logical_db_name == NULL)
ereport(ERROR,
Expand All @@ -80,13 +81,15 @@ set_cur_db_name_for_parallel_worker(const char* logical_db_name)

Assert(len <= MAX_BBF_NAMEDATALEND);

if(!DbidIsValid(get_db_id(logical_db_name)))
db_id = get_db_id(logical_db_name);
if(!DbidIsValid(db_id))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", logical_db_name)));

strncpy(current_db_name, logical_db_name, MAX_BBF_NAMEDATALEND);
current_db_name[len] = '\0';
current_db_id = db_id;
}


Expand Down
6 changes: 3 additions & 3 deletions test/JDBC/expected/102_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ GO

~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~


DROP TABLE t102__2;
Expand Down Expand Up @@ -344,7 +344,7 @@ exec error_mapping.ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~

declare @err int = @@error; if (@err > 0 and @@trancount > 0) select cast('BATCH ONLY TERMINATING' as text) else if @err > 0 select cast('BATCH TERMINATING\ txn rolledback' as text);
if @@trancount > 0 rollback transaction;
Expand Down Expand Up @@ -413,7 +413,7 @@ exec error_mapping.ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~

declare @err int = @@error; if (@err > 0 and @@trancount > 0) select cast('BATCH ONLY TERMINATING' as text) else if @err > 0 select cast('BATCH TERMINATING\ txn rolledback' as text);
if @@trancount > 0 rollback transaction;
Expand Down
6 changes: 3 additions & 3 deletions test/JDBC/expected/102_2.out
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ GO

~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~


DROP TABLE t102__2;
Expand Down Expand Up @@ -352,7 +352,7 @@ exec error_mapping.ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~

declare @err int = @@error; if (@err > 0 and @@trancount > 0) select cast('BATCH ONLY TERMINATING' as text) else if @err > 0 select cast('BATCH TERMINATING\ txn rolledback' as text);
if @@trancount > 0 rollback transaction;
Expand Down Expand Up @@ -422,7 +422,7 @@ exec error_mapping.ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_error_mapping.errorhandling1() does not exist)~~
~~ERROR (Message: procedure error_mapping.errorhandling1() does not exist)~~

declare @err int = @@error; if (@err > 0 and @@trancount > 0) select cast('BATCH ONLY TERMINATING' as text) else if @err > 0 select cast('BATCH TERMINATING\ txn rolledback' as text);
if @@trancount > 0 rollback transaction;
Expand Down
Loading

0 comments on commit 76fa832

Please sign in to comment.