Skip to content

Commit

Permalink
Update RESTORE behaviour for LIKE operator for CS_AI collation (#3207) (
Browse files Browse the repository at this point in the history
#3296)

(cherry picked from commit 3fe245b)
  • Loading branch information
ahmed-shameem authored Dec 23, 2024
1 parent aa800eb commit a692763
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
19 changes: 19 additions & 0 deletions contrib/babelfishpg_common/src/collation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,24 @@ has_ilike_node(Node *expr)
return false;
}

bool
has_like_node(Node *expr)
{
OpExpr *op;

Assert(IsA(expr, OpExpr));

op = (OpExpr *) expr;
for (int i = 0; i < TOTAL_LIKE_OP_COUNT; i++)
{
if (strcmp(get_opname(op->opno), like_ilike_table[i].like_op_name) == 0)
{
return true;
}
}
return false;
}

Datum
is_collated_ci_as_internal(PG_FUNCTION_ARGS)
{
Expand Down Expand Up @@ -1625,6 +1643,7 @@ get_collation_callbacks(void)
collation_callbacks_var.find_cs_as_collation_internal = &find_cs_as_collation;
collation_callbacks_var.find_collation_internal = &find_collation;
collation_callbacks_var.has_ilike_node = &has_ilike_node;
collation_callbacks_var.has_like_node = &has_like_node;
collation_callbacks_var.translate_bbf_collation_to_tsql_collation = &translate_bbf_collation_to_tsql_collation;
collation_callbacks_var.translate_tsql_collation_to_bbf_collation = &translate_tsql_collation_to_bbf_collation;
collation_callbacks_var.set_db_collation = &set_db_collation;
Expand Down
3 changes: 3 additions & 0 deletions contrib/babelfishpg_common/src/collation.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ typedef struct collation_callbacks

bool (*has_ilike_node) (Node *expr);

bool (*has_like_node) (Node *expr);

const char *(*translate_bbf_collation_to_tsql_collation) (const char *collname);

const char *(*translate_tsql_collation_to_bbf_collation) (const char *collname);
Expand Down Expand Up @@ -147,6 +149,7 @@ extern const char *translate_bbf_collation_to_tsql_collation(const char *collnam
extern const char *translate_tsql_collation_to_bbf_collation(const char *collname);
Oid get_oid_from_collidx(int collidx);
extern bool has_ilike_node(Node *expr);
extern bool has_like_node(Node *expr);
extern Oid babelfish_define_type_default_collation(Oid typeNamespace);
extern void set_db_collation(Oid db_coll);

Expand Down
14 changes: 13 additions & 1 deletion contrib/babelfishpg_tsql/src/collation.c
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,14 @@ transform_likenode(Node *node)
* by a user. So, it is safe to go ahead with replacing the ci_as
* collation with a corresponding cs_as one if an ILIKE node is found
* during dump and restore.
* For CS_AI collation, we keep the LIKE operator but convert it to
* CS_AS by adding a call to remove_accents_internal*. During restore,
* it will take the same code path and will try to add a new call to
* remove_accents_internal* on top of current node. So, we take care
* of that case here.
*/
init_and_check_collation_callbacks();
if ((*collation_callbacks_ptr->has_ilike_node) (node) && babelfish_dump_restore)
if (babelfish_dump_restore && ((*collation_callbacks_ptr->has_ilike_node) (node) || (*collation_callbacks_ptr->has_like_node) (node)))
{
int collidx_of_cs_as;

Expand All @@ -965,6 +970,13 @@ transform_likenode(Node *node)
/* If a collation is not specified, use the default one */
op->inputcollid = DEFAULT_COLLATION_OID;
}

/*
* If this has a like node, then it is CS collation
* So we can return from here directly
*/
if ((*collation_callbacks_ptr->has_like_node) (node))
return node;
}

if (OidIsValid(like_entry.like_oid) &&
Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/src/collation.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ typedef struct collation_callbacks

bool (*has_ilike_node) (Node *expr);

bool (*has_like_node) (Node *expr);

const char *(*translate_bbf_collation_to_tsql_collation) (const char *collname);

const char *(*translate_tsql_collation_to_bbf_collation) (const char *collname);
Expand Down

0 comments on commit a692763

Please sign in to comment.