Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_5_X_DEV__PG_17_X' into J…
Browse files Browse the repository at this point in the history
…IRA-BABEL-5342
  • Loading branch information
roshan0708 committed Dec 18, 2024
2 parents 3bd2029 + 9a28935 commit 989f276
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
32 changes: 4 additions & 28 deletions src/backend/catalog/aclchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid

tsql_has_linked_srv_permissions_hook_type tsql_has_linked_srv_permissions_hook = NULL;
bbf_execute_grantstmt_as_dbsecadmin_hook_type bbf_execute_grantstmt_as_dbsecadmin_hook = NULL;

pltsql_allow_storing_init_privs_hook_type pltsql_allow_storing_init_privs_hook = NULL;
/*
* If is_grant is true, adds the given privileges for the list of
* grantees to the existing old_acl. If is_grant is false, the
Expand Down Expand Up @@ -4705,6 +4705,9 @@ recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
if (!creating_extension && !binary_upgrade_record_init_privs)
return;

if (pltsql_allow_storing_init_privs_hook && !((*pltsql_allow_storing_init_privs_hook)(objoid, classoid, objsubid)))
return;

recordExtensionInitPrivWorker(objoid, classoid, objsubid, new_acl);
}

Expand Down Expand Up @@ -5039,32 +5042,6 @@ RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
* Generate new ACL. Grantor of rights is always the same as the owner.
*/
if (old_acl != NULL)
{
Oid sysadminOid;
Acl *temp_acl;
const char *babelfish_db_name;

/*
* For babelfish database, grantor will be sysadmin instead of object owner.
*/
if (bbf_get_sysadmin_oid_hook &&
classid == DatabaseRelationId &&
(babelfish_db_name = GetConfigOption("babelfishpg_tsql.database_name", true, false)) &&
objid == get_database_oid(babelfish_db_name, true) &&
is_member_of_role(GetUserId(), sysadminOid = (*bbf_get_sysadmin_oid_hook)()))
{

temp_acl = merge_acl_with_grant(old_acl,
false, /* is_grant */
false, /* grant_option */
DROP_RESTRICT,
list_make1_oid(roleid),
ACLITEM_ALL_PRIV_BITS,
sysadminOid,
ownerId);
old_acl = temp_acl;
}

new_acl = merge_acl_with_grant(old_acl,
false, /* is_grant */
false, /* grant_option */
Expand All @@ -5073,7 +5050,6 @@ RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
ACLITEM_ALL_PRIV_BITS,
ownerId,
ownerId);
}
else
new_acl = NULL; /* this case shouldn't happen, probably */

Expand Down
30 changes: 30 additions & 0 deletions src/backend/parser/parse_coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

find_coercion_pathway_hook_type find_coercion_pathway_hook = NULL;
determine_datatype_precedence_hook_type determine_datatype_precedence_hook = NULL;
is_tsql_base_datatype_hook_type is_tsql_base_datatype_hook = NULL;
coerce_string_literal_hook_type coerce_string_literal_hook = NULL;
validate_implicit_conversion_from_string_literal_hook_type validate_implicit_conversion_from_string_literal_hook = NULL;
select_common_type_hook_type select_common_type_hook = NULL;
Expand Down Expand Up @@ -1504,9 +1505,38 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
pcategory = ncategory;
pispreferred = nispreferred;
}
} else if (sql_dialect == SQL_DIALECT_TSQL && ntype == ptype)
{
/*
* For the columns which have the same base type, we choose the
* expression with higher precedence type in T-SQL.
* For example, smallmoney UNION money, the base type of
* them are both fixeddecimal. But we shouldn't use smallmoney as
* the result type, it could loss precision.
* Here we don't need to update other variables since they are the
* same.
*/
if (is_tsql_base_datatype_hook &&
(*is_tsql_base_datatype_hook)(exprType(nexpr)) &&
determine_datatype_precedence_hook &&
determine_datatype_precedence_hook(exprType(nexpr),
exprType(pexpr)))
pexpr = nexpr;
}
}

/*
* If the preferred type is not the result type of corresponding
* expression, it means the result type is a domain type in Postgres. Some
* base data types in T-SQL are implemented as domain types in Babelfish.
* From SQL Server's perspective, we should try to retain those types as
* result types.
*/
if (sql_dialect == SQL_DIALECT_TSQL && ptype != exprType(pexpr) &&
is_tsql_base_datatype_hook &&
(*is_tsql_base_datatype_hook)(exprType(pexpr)))
ptype = exprType(pexpr);

/*
* If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
* then resolve as type TEXT. This situation comes up with constructs
Expand Down
5 changes: 5 additions & 0 deletions src/include/parser/parse_coerce.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ typedef CoercionPathType (*find_coercion_pathway_hook_type) (Oid sourceTypeId,
*/
typedef bool (*determine_datatype_precedence_hook_type) (Oid typeId1, Oid typeId2);

/*
* Hook interface to determine if a data type is a base type in T-SQL
*/
typedef bool (*is_tsql_base_datatype_hook_type) (Oid typeId);

/*
* T-SQL has different rules for string literal datatype coercions
*/
Expand Down
3 changes: 3 additions & 0 deletions src/include/utils/acl.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ extern PGDLLEXPORT pltsql_get_object_owner_hook_type pltsql_get_object_owner_hoo
typedef bool (*is_bbf_db_ddladmin_operation_hook_type) (Oid namespaceId);
extern PGDLLEXPORT is_bbf_db_ddladmin_operation_hook_type is_bbf_db_ddladmin_operation_hook;

typedef bool (*pltsql_allow_storing_init_privs_hook_type) (Oid objoid, Oid classoid, int objsubid);
extern PGDLLEXPORT pltsql_allow_storing_init_privs_hook_type pltsql_allow_storing_init_privs_hook;

#define IS_BBF_DB_DDLADMIN(namespaceId) \
(is_bbf_db_ddladmin_operation_hook && \
is_bbf_db_ddladmin_operation_hook(namespaceId))
Expand Down

0 comments on commit 989f276

Please sign in to comment.