Skip to content

Commit

Permalink
BABEL: CREATE VIEW fails in UNION if column datatypes are from differ…
Browse files Browse the repository at this point in the history
…ent categories during MVU (yugabyte#9)

T-SQL has certain different rules and hooks for views
compared to PG and these rules and hooks are only
applicable in T-SQL dialect, that's why MVU fails while
restoring T-SQL views since restore happens in PG dialect.

Fix this by allowing T-SQL specific rules and hooks if
babelfishpg_tsql.dump_restore GUC is set to on. Following
three hooks will be allowed:
1. determine_datatype_precedence_hook
2. find_coercion_pathway_hook
3. func_select_candidate_hook

Task: BABEL-3221
Signed-off-by: Rishabh Tanwar <ritanwar@amazon.com>
(cherry picked from commit 091d490b2c965ac958833dee163b5ab399ea8702)
  • Loading branch information
rishabhtanwar29 authored and abhinab-yb committed Nov 14, 2024
1 parent b4f10fa commit 0f040b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/postgres/src/backend/parser/parse_coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/datum.h" /* needed for datumIsEqual() */
#include "utils/guc.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Expand Down Expand Up @@ -1380,6 +1381,7 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
TYPCATEGORY pcategory;
bool pispreferred;
ListCell *lc;
const char *dump_restore = GetConfigOption("babelfishpg_tsql.dump_restore", true, false);

Assert(exprs != NIL);
pexpr = (Node *) linitial(exprs);
Expand Down Expand Up @@ -1437,8 +1439,9 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
pcategory = ncategory;
pispreferred = nispreferred;
}
else if (ncategory != pcategory
&& sql_dialect != SQL_DIALECT_TSQL) /* T-SQL allows to select common datatype between different categories */
else if (ncategory != pcategory &&
sql_dialect != SQL_DIALECT_TSQL && /* T-SQL allows to select common datatype between different categories */
(!dump_restore || (dump_restore && strcmp(dump_restore, "on") != 0))) /* allow common datatype between different categories while restoring babelfish database */
{
/*
* both types in different categories? then not much hope...
Expand Down Expand Up @@ -1468,7 +1471,8 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
pcategory = ncategory;
pispreferred = nispreferred;
}
else if (sql_dialect == SQL_DIALECT_TSQL &&
else if ((sql_dialect == SQL_DIALECT_TSQL ||
(dump_restore && strcmp(dump_restore, "on") == 0)) &&
determine_datatype_precedence_hook != NULL &&
!pispreferred &&
can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
Expand Down Expand Up @@ -3202,14 +3206,16 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId,
{
CoercionPathType result = COERCION_PATH_NONE;
HeapTuple tuple;
const char *dump_restore = GetConfigOption("babelfishpg_tsql.dump_restore", true, false);

*funcid = InvalidOid;

/*
* T-SQL allows more implicit casting in general.
* check if rules (defiend with "assignment" property) is supported in T-SQL
*/
if (sql_dialect == SQL_DIALECT_TSQL &&
if ((sql_dialect == SQL_DIALECT_TSQL ||
(dump_restore && strcmp(dump_restore, "on") == 0)) && /* execute hook if dialect is T-SQL or while restoring babelfish database */
find_coercion_pathway_hook != NULL)
{
result = find_coercion_pathway_hook(sourceTypeId, targetTypeId, ccontext, funcid);
Expand Down
5 changes: 4 additions & 1 deletion src/postgres/src/backend/parser/parse_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "parser/parse_type.h"
#include "parser/parser.h" /* SQL_DIALECT_TSQL */
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"

Expand Down Expand Up @@ -1032,6 +1033,7 @@ func_select_candidate(int nargs,
bool current_is_preferred;
bool slot_has_preferred_type[FUNC_MAX_ARGS];
bool resolved_unknowns;
const char *dump_restore = GetConfigOption("babelfishpg_tsql.dump_restore", true, false);

/* protect local fixed-size arrays */
if (nargs > FUNC_MAX_ARGS)
Expand Down Expand Up @@ -1075,7 +1077,8 @@ func_select_candidate(int nargs,
* let's try to choose the best candidate by T-SQL precedence rule.
*/
if (nunknowns == 0 &&
sql_dialect == SQL_DIALECT_TSQL &&
(sql_dialect == SQL_DIALECT_TSQL ||
(dump_restore && strcmp(dump_restore, "on") == 0)) && /* execute hook if dialect is T-SQL or while restoring babelfish database */
func_select_candidate_hook != NULL)
{
last_candidate = func_select_candidate_hook(nargs, input_typeids, candidates, false);
Expand Down

0 comments on commit 0f040b5

Please sign in to comment.