From 1bca96b6235171a048be9d93628f89b7e67472c1 Mon Sep 17 00:00:00 2001 From: Chenxiao Wang Date: Tue, 24 Dec 2024 20:05:31 +0000 Subject: [PATCH] Previously, Babelfish displays the mapped schema names in error messages. This commit imports a hook function remove_db_name_in_schema and uses it to restore thr unmapped name. Original unmapped name will be displayed with this commit. Signed-off-by: Chenxiao Wang --- src/backend/catalog/namespace.c | 7 +++++++ src/backend/parser/parse_func.c | 4 ++++ src/backend/parser/parse_relation.c | 9 +++++++++ src/include/catalog/namespace.h | 2 ++ src/include/parser/parse_func.h | 2 ++ 5 files changed, 24 insertions(+) diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index b4d05adb485..cec280c5681 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -216,6 +216,7 @@ char *SYS_NAMESPACE_NAME = "sys"; relname_lookup_hook_type relname_lookup_hook = NULL; match_pltsql_func_call_hook_type match_pltsql_func_call_hook = NULL; +remove_db_name_in_schema_hook_type remove_db_name_in_schema_hook = NULL; /* Local functions */ @@ -3633,10 +3634,16 @@ get_namespace_oid(const char *nspname, bool missing_ok) oid = GetSysCacheOid1(NAMESPACENAME, Anum_pg_namespace_oid, CStringGetDatum(nspname)); if (!OidIsValid(oid) && !missing_ok) + { + if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook) + { + nspname = (*remove_db_name_in_schema_hook)(nspname); + } ereport(ERROR, (errcode(ERRCODE_UNDEFINED_SCHEMA), errmsg("schema \"%s\" does not exist", nspname))); + } return oid; } diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 7fda7c6aa25..1a55c8bd39d 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2063,6 +2063,10 @@ funcname_signature_string(const char *funcname, int nargs, initStringInfo(&argbuf); + if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook) { + funcname = (*remove_db_name_in_schema_hook)(funcname); + } + appendStringInfo(&argbuf, "%s(", funcname); numposargs = nargs - list_length(argnames); diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index bef4cd1a10f..17a9f12b5d1 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -1453,10 +1453,19 @@ parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode) if (rel == NULL) { if (relation->schemaname) + { + if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook){ + const char *schema_name = (*remove_db_name_in_schema_hook)(relation->schemaname); + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_TABLE), + errmsg("relation \"%s.%s\" does not exist", + schema_name, relation->relname))); + } else ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s.%s\" does not exist", relation->schemaname, relation->relname))); + } else { /* diff --git a/src/include/catalog/namespace.h b/src/include/catalog/namespace.h index 1f3a62d3891..a1c289fe860 100644 --- a/src/include/catalog/namespace.h +++ b/src/include/catalog/namespace.h @@ -90,6 +90,8 @@ typedef bool (*match_pltsql_func_call_hook_type) (HeapTuple proctup, int nargs, bool *use_defaults, bool *any_special, bool *variadic, Oid *va_elem_type); extern PGDLLEXPORT match_pltsql_func_call_hook_type match_pltsql_func_call_hook; +typedef const char * (*remove_db_name_in_schema_hook_type) (const char *schema_name); +extern PGDLLEXPORT remove_db_name_in_schema_hook_type remove_db_name_in_schema_hook; #define RangeVarGetRelid(relation, lockmode, missing_ok) \ RangeVarGetRelidExtended(relation, lockmode, \ diff --git a/src/include/parser/parse_func.h b/src/include/parser/parse_func.h index d2ac9719a95..32c6ee1ce08 100644 --- a/src/include/parser/parse_func.h +++ b/src/include/parser/parse_func.h @@ -80,5 +80,7 @@ typedef void (*make_fn_arguments_from_stored_proc_probin_hook_type)(ParseState * extern PGDLLEXPORT make_fn_arguments_from_stored_proc_probin_hook_type make_fn_arguments_from_stored_proc_probin_hook; typedef void (*report_proc_not_found_error_hook_type) (List *names, List *fargs, List *argnames, Oid *input_typeids, int nargs, ParseState *pstate, int location, bool proc_call); extern PGDLLEXPORT report_proc_not_found_error_hook_type report_proc_not_found_error_hook; +typedef const char * (*remove_db_name_in_schema_hook_type) (const char *schema_name); +extern PGDLLEXPORT remove_db_name_in_schema_hook_type remove_db_name_in_schema_hook; #endif /* PARSE_FUNC_H */