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

Display unmapped schema names in error messages #3328

Merged
Merged
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
60 changes: 60 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,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 @@ -250,6 +251,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 @@ -352,6 +354,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 @@ -543,6 +548,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 @@ -5972,3 +5978,57 @@ allow_storing_init_privs(Oid objoid, Oid classoid, int objsubid)
}
return true;
}

/*
* 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);
}
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
6 changes: 3 additions & 3 deletions test/JDBC/expected/102_3.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
6 changes: 3 additions & 3 deletions test/JDBC/expected/102_6.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/129_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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 t129
Expand Down Expand Up @@ -306,7 +306,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 @@ -370,7 +370,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/2760_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ exec ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_n0nexistantschema" does not exist)~~
~~ERROR (Message: schema "n0nexistantschema" 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 @@ -165,7 +165,7 @@ exec ErrorHandling2;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_n0nexistantschema" does not exist)~~
~~ERROR (Message: schema "n0nexistantschema" 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 @@ -213,7 +213,7 @@ int

~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_n0nexistantschema" does not exist)~~
~~ERROR (Message: schema "n0nexistantschema" does not exist)~~

GO

Expand Down
8 changes: 4 additions & 4 deletions test/JDBC/expected/BABEL-1454.out
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ GRANT ALL ON db1.dummy_schema TO PUBLIC;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "db1_db1" does not exist)~~
~~ERROR (Message: schema "db1" does not exist)~~


GRANT ALL ON babel1454.dummy_type TO PUBLIC;
Expand Down Expand Up @@ -247,7 +247,7 @@ DROP SCHEMA dummy_schema;
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "db1_dummy_schema" does not exist)~~
~~ERROR (Message: schema "dummy_schema" does not exist)~~


-- Trigger
Expand Down Expand Up @@ -376,14 +376,14 @@ EXEC dummy.foo
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "db1_dummy" does not exist)~~
~~ERROR (Message: schema "dummy" does not exist)~~


EXEC db1.dummy.foo
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "db1_dummy" does not exist)~~
~~ERROR (Message: schema "dummy" does not exist)~~


--- Cleanup
Expand Down
2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-1712.out
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SELECT * FROM dbo.t1; -- error expected, querying master.dbo.t1
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "master_dbo.t1" does not exist)~~
~~ERROR (Message: relation "dbo.t1" does not exist)~~

-- search path
USE db1;
Expand Down
4 changes: 2 additions & 2 deletions test/JDBC/expected/BABEL-1771.out
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SELECT * FROM .fake_schema.t
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "master_fake_schema.t" does not exist)~~
~~ERROR (Message: relation "fake_schema.t" does not exist)~~


EXEC test.bar
Expand All @@ -61,7 +61,7 @@ EXEC master..bar
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: procedure master_dbo.bar() does not exist)~~
~~ERROR (Message: procedure dbo.bar() does not exist)~~


EXEC ..bar
Expand Down
6 changes: 3 additions & 3 deletions test/JDBC/expected/BABEL-2496.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ drop schema non_exist_schema;
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_non_exist_schema" does not exist)~~
~~ERROR (Message: schema "non_exist_schema" does not exist)~~



Expand All @@ -20,7 +20,7 @@ drop schema s2496;
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_s2496" does not exist)~~
~~ERROR (Message: schema "s2496" does not exist)~~


drop schema if exists s2496;
Expand All @@ -30,5 +30,5 @@ drop schema s2496;
go
~~ERROR (Code: 33557097)~~

~~ERROR (Message: schema "master_s2496" does not exist)~~
~~ERROR (Message: schema "s2496" does not exist)~~

2 changes: 1 addition & 1 deletion test/JDBC/expected/BABEL-2844.out
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ SELECT dbo.WOSQL_BuildRevenueDetailOLUQuery()
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: function master_dbo.wosql_buildrevenuedetailoluquery() does not exist)~~
~~ERROR (Message: function dbo.wosql_buildrevenuedetailoluquery() does not exist)~~


SET BABELFISH_SHOWPLAN_ALL OFF;
Expand Down
36 changes: 36 additions & 0 deletions test/JDBC/expected/BABEL-2961-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
IF OBJECT_ID('dbo.SomeNonExistentTable', 'U') IS NOT NULL
DROP TABLE dbo.SomeNonExistentTable
GO


IF OBJECT_ID('dbo.NonExistentTableView', 'V') IS NOT NULL
DROP VIEW dbo.NonExistentTableView
GO

IF OBJECT_ID('dbo.NonExistentTableFunc', 'IF') IS NOT NULL
DROP FUNCTION dbo.NonExistentTableFunc
GO

IF OBJECT_ID('dbo.CallNonExistentProc', 'P') IS NOT NULL
DROP PROCEDURE dbo.CallNonExistentProc
GO

IF OBJECT_ID('dbo.TestTable', 'U') IS NOT NULL
DROP TABLE dbo.TestTable
GO

IF OBJECT_ID('dbo.NonExistentTable', 'U') IS NOT NULL
DROP TABLE dbo.NonExistentTable
GO

IF OBJECT_ID('dbo.DropNonExistentUser', 'P') IS NOT NULL
DROP PROCEDURE dbo.DropNonExistentUser
GO

IF OBJECT_ID('dbo.NonExistentProc', 'P') IS NOT NULL
DROP PROCEDURE dbo.NonExistentProc
GO

IF OBJECT_ID('nonexistent_schema.TestFunc', 'FN') IS NOT NULL
DROP FUNCTION nonexistent_schema.TestFunc
GO
Loading