Skip to content

Commit

Permalink
Merge 29ec9ef into 88c8871
Browse files Browse the repository at this point in the history
  • Loading branch information
robverschoor committed Jun 27, 2024
2 parents 88c8871 + 29ec9ef commit 8af83e4
Show file tree
Hide file tree
Showing 29 changed files with 18,105 additions and 143 deletions.
81 changes: 81 additions & 0 deletions contrib/babelfishpg_tsql/src/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,87 @@ is_created_with_recompile(Oid objectId)
return recompile;
}

/*
* Check if a catalog name is a classic T-SQL catalog starting with 'sys' (e.g. sysobjects).
* Historically (dating back to the Sybase era) these catalogs were located in the
* 'dbo' schema but have since been relocated to the 'sys' schema.
* They can however still be referenced in the 'dbo' schema which is equivalent to using 'sys'.
* Note: newer catalogs do not normally start with 'sys', but some exceptions exist, such as
* 'system_sql_modules'. These newer cases are however not referencable via 'dbo'.
*
* The input parameter is a table/view name, from which enclosing double quotes or square brackets
* have been stripped.
*/
bool
is_classic_catalog(const char *name)
{
Assert(name);
if (strlen(name) <= 7) // sysusers,systypes,syslocks,sysfiles are shortest
return false;

if (pg_strncasecmp(name, "sys", 3) != 0)
return false;

return (
// Currently supported catalogs:

// Instance-wide classic catalogs
// NB: sysdatabases does not need its schema mapped from 'dbo' to 'sys',
// but it is included here for completeness.
(pg_strncasecmp(name, "sysdatabases", 12) == 0) ||
(pg_strncasecmp(name, "syscharsets", 11) == 0) ||
(pg_strncasecmp(name, "sysconfigures", 13) == 0) ||
(pg_strncasecmp(name, "syscurconfigs", 13) == 0) ||
(pg_strncasecmp(name, "syslanguages", 12) == 0) ||
(pg_strncasecmp(name, "syslogins", 9) == 0) ||
(pg_strncasecmp(name, "sysprocesses", 12) == 0) ||

// DB-specific classic catalogs
(pg_strncasecmp(name, "syscolumns", 10) == 0) ||
(pg_strncasecmp(name, "sysforeignkeys", 14) == 0) ||
(pg_strncasecmp(name, "sysindexes", 10) == 0) ||
(pg_strncasecmp(name, "sysobjects", 10) == 0) ||
(pg_strncasecmp(name, "systypes", 8) == 0) ||
(pg_strncasecmp(name, "sysusers", 8) == 0)
);

/*
* Additional T-SQL catalogs, not currently supported in Babelfish.
*
* When adding support for such a catalog, add it to the list above.
* We could include all of these in the list above, but that might
* impact performance.
*
* Instance-wide catalogs:
sysaltfiles
syscacheobjects
sysdevices
sysfilegroups
sysfiles
syslockinfo
syslocks
sysoledbusers
sysopentapes
sysperfinfo
sysremotelogins
sysservers
* DB-specific catalogs:
syscomments
sysconstraints
sysdepends
sysforeignkeys
sysfulltextcatalogs
sysindexkeys
sysmembers
sysmessages
syspermissions
sysprotects
sysreferences
*
*/
}

/*****************************************
* SCHEMA
*****************************************/
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ extern Oid get_bbf_function_ext_idx_oid(void);
extern HeapTuple get_bbf_function_tuple_from_proctuple(HeapTuple proctuple);
extern void clean_up_bbf_function_ext(int16 dbid);
extern bool is_created_with_recompile(Oid objectId);
extern bool is_classic_catalog(const char *name);

typedef struct FormData_bbf_function_ext
{
Expand Down
71 changes: 8 additions & 63 deletions contrib/babelfishpg_tsql/src/multidb.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ static bool is_select_for_json(SelectStmt *stmt);
static void select_json_modify(SelectStmt *stmt);
static bool is_for_json(FuncCall *fc);
static bool get_array_wrapper(List *for_json_args);
static void set_schemaname_dbo_to_sys(RangeVar *rv);



/*************************************************************
Expand Down Expand Up @@ -707,13 +705,6 @@ rewrite_relation_walker(Node *node, void *context)
if (IsA(node, RangeVar))
{
RangeVar *rv = (RangeVar *) node;

/*
* For the list of catalog names if the schema name
* specified is 'dbo' then replace with 'sys'.
*/
set_schemaname_dbo_to_sys(rv);

rewrite_rangevar(rv);
return false;
}
Expand Down Expand Up @@ -971,51 +962,6 @@ rewrite_rangevar(RangeVar *rv)
}
}

static void
set_schemaname_dbo_to_sys(RangeVar *rv)
{
/*
* list_of_dbo_catalog
* Contains the list of sys% views which are not database specific
*
* list_of_dbo_catalog_not_supported_for_cross_db
* Contains the list of sys% views which are database specific
*
* NOTE: While adding the sys% views to list check whether view is an database specific or not.
*
*/
char* list_of_dbo_catalog[6]= {"sysprocesses", "syscharsets", "sysconfigures", "syscurconfigs", "syslanguages", "syslogins"};
char* list_of_dbo_catalog_not_supported_for_cross_db[6]= {"syscolumns", "sysforeignkeys", "sysindexes", "sysobjects", "systypes", "sysusers"};
if (rv->schemaname && strcmp(rv->schemaname, "dbo") == 0)
{
for (int i = 0; i < (sizeof(list_of_dbo_catalog)/sizeof(list_of_dbo_catalog[0])); i++)
{
if(rv->relname && strcmp(rv->relname, list_of_dbo_catalog[i]) == 0)
{
rv->schemaname = pstrdup("sys");
break;
}
}
for (int i = 0; i < (sizeof(list_of_dbo_catalog_not_supported_for_cross_db)/sizeof(list_of_dbo_catalog_not_supported_for_cross_db[0])); i++)
{
if(rv->relname && strcmp(rv->relname, list_of_dbo_catalog_not_supported_for_cross_db[i]) == 0)
{
/* Throw error for dbo catalog which does not support cross-db */
if (rv->catalogname && strcmp(get_cur_db_name(), rv->catalogname) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("Cross-DB system view query is not currently supported in Babelfish.")));
}
else
{
rv->schemaname = pstrdup("sys");
break;
}
}
}
}
}
static void
rewrite_objectwithargs(ObjectWithArgs *obj)
{
Expand All @@ -1034,12 +980,12 @@ rewrite_plain_name(List *name)
String *new_schema;

if (is_shared_schema(strVal(schema)))
break; /* do not thing for shared schemas */
break; /* do nothing for shared schemas */

new_schema = makeString(get_physical_schema_name(cur_db, strVal(schema)));

/*
* ignoring the return value sinece list is valid and cannot
* ignoring the return value since list is valid and cannot
* be empty
*/
name = list_delete_first(name);
Expand All @@ -1062,7 +1008,7 @@ rewrite_plain_name(List *name)
new_schema = makeString(get_physical_schema_name(strVal(db), strVal(schema)));

/*
* ignoring the return value sinece list is valid and
* ignoring the return value since list is valid and
* cannot be empty
*/
name = list_delete_first(name);
Expand Down Expand Up @@ -1101,7 +1047,7 @@ is_shared_schema(const char *name)
{
if ((strcmp("sys", name) == 0)
|| (strcmp("information_schema_tsql", name) == 0))
return true; /* babelfish shared schema */
return true; /* Babelfish shared schema */
else if ((strcmp("public", name) == 0)
|| (strcmp("pg_catalog", name) == 0)
|| (strcmp("pg_toast", name) == 0)
Expand Down Expand Up @@ -1275,7 +1221,7 @@ get_physical_schema_name_by_mode(char *db_name, const char *schema_name, Migrati
}

/*
* Parser guarantees identifier will alsways be truncated to 64B. Schema
* Parser guarantees identifier will always be truncated to 64B. Schema
* name that comes from other source (e.g scheam_id function) needs one
* more truncate function call
*/
Expand Down Expand Up @@ -1356,7 +1302,7 @@ get_physical_user_name(char *db_name, char *user_name, bool suppress_error)
truncate_tsql_identifier(new_user_name);

/*
* All the role and user names are prefixed. Historically, dbo and
* All role and user names are prefixed. Historically, dbo and
* db_owner in single-db mode were unprefixed These are two exceptions to
* the naming convention
*/
Expand Down Expand Up @@ -1509,16 +1455,15 @@ physical_schema_name_exists(char *phys_schema_name)
}

/*
* Assume the database already exists and it is not a built in database
*/
* Assume the database already exists and it is not a built-in database
*/
bool
is_user_database_singledb(const char *dbname)
{
Assert(DbidIsValid(get_db_id(dbname)));
return !is_builtin_database(dbname) && physical_schema_name_exists("dbo");
}


/*************************************************************
* Helper Functions
*************************************************************/
Expand Down
Loading

0 comments on commit 8af83e4

Please sign in to comment.