Skip to content

Commit c11e29c

Browse files
committed
[BABEL] Resolved string literal type as sys.varchar in select_common_type for babelfish
In select_common_type() currently string literal has type unknownoid but for babelfish, string literal should be of type sys.varchar, this is causing incorrect results in babelfish in common type selection. Fixed this by updating type of string literal as sys.varchar and also migrated babelfish related changes from select_common_type to babelfish pre-hook, select_common_type_hook. Task: BABEL-6127 Signed-off-by: Rohit Bhagat <rohitbgt@amazon.com> cr: https://code.amazon.com/reviews/CR-227519704
1 parent 50c00bb commit c11e29c

File tree

1 file changed

+1
-67
lines changed

1 file changed

+1
-67
lines changed

src/backend/parser/parse_coerce.c

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,6 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
14301430
TYPCATEGORY pcategory;
14311431
bool pispreferred;
14321432
ListCell *lc;
1433-
const char *dump_restore = GetConfigOption("babelfishpg_tsql.dump_restore", true, false);
14341433

14351434
if (select_common_type_hook)
14361435
{
@@ -1495,9 +1494,7 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
14951494
pcategory = ncategory;
14961495
pispreferred = nispreferred;
14971496
}
1498-
else if (ncategory != pcategory &&
1499-
sql_dialect != SQL_DIALECT_TSQL && /* T-SQL allows to select common datatype between different categories */
1500-
(!dump_restore || (dump_restore && strcmp(dump_restore, "on") != 0))) /* allow common datatype between different categories while restoring babelfish database */
1497+
else if (ncategory != pcategory)
15011498
{
15021499
/*
15031500
* both types in different categories? then not much hope...
@@ -1527,55 +1524,9 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
15271524
pcategory = ncategory;
15281525
pispreferred = nispreferred;
15291526
}
1530-
else if ((sql_dialect == SQL_DIALECT_TSQL ||
1531-
(dump_restore && strcmp(dump_restore, "on") == 0)) &&
1532-
determine_datatype_precedence_hook != NULL &&
1533-
!pispreferred &&
1534-
can_coerce_type(1, &ptype, &ntype, COERCION_IMPLICIT) &&
1535-
can_coerce_type(1, &ntype, &ptype, COERCION_IMPLICIT) &&
1536-
determine_datatype_precedence_hook(ntype, ptype))
1537-
{
1538-
/*
1539-
* T-SQL allows implicit casting on both-side.
1540-
* common datatype should be decided by datatype precedence rule.
1541-
*/
1542-
pexpr = nexpr;
1543-
ptype = ntype;
1544-
pcategory = ncategory;
1545-
pispreferred = nispreferred;
1546-
}
1547-
} else if (sql_dialect == SQL_DIALECT_TSQL && ntype == ptype)
1548-
{
1549-
/*
1550-
* For the columns which have the same base type, we choose the
1551-
* expression with higher precedence type in T-SQL.
1552-
* For example, smallmoney UNION money, the base type of
1553-
* them are both fixeddecimal. But we shouldn't use smallmoney as
1554-
* the result type, it could loss precision.
1555-
* Here we don't need to update other variables since they are the
1556-
* same.
1557-
*/
1558-
if (is_tsql_base_datatype_hook &&
1559-
(*is_tsql_base_datatype_hook)(exprType(nexpr)) &&
1560-
determine_datatype_precedence_hook &&
1561-
determine_datatype_precedence_hook(exprType(nexpr),
1562-
exprType(pexpr)))
1563-
pexpr = nexpr;
15641527
}
15651528
}
15661529

1567-
/*
1568-
* If the preferred type is not the result type of corresponding
1569-
* expression, it means the result type is a domain type in Postgres. Some
1570-
* base data types in T-SQL are implemented as domain types in Babelfish.
1571-
* From SQL Server's perspective, we should try to retain those types as
1572-
* result types.
1573-
*/
1574-
if (sql_dialect == SQL_DIALECT_TSQL && ptype != exprType(pexpr) &&
1575-
is_tsql_base_datatype_hook &&
1576-
(*is_tsql_base_datatype_hook)(exprType(pexpr)))
1577-
ptype = exprType(pexpr);
1578-
15791530
/*
15801531
* If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
15811532
* then resolve as type TEXT. This situation comes up with constructs
@@ -1587,23 +1538,6 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
15871538
* literals while they are still literals, so a decision has to be made
15881539
* now.
15891540
*/
1590-
if (ptype == UNKNOWNOID && sql_dialect == SQL_DIALECT_TSQL)
1591-
{
1592-
bool all_nullconst = true;
1593-
foreach(lc, exprs)
1594-
{
1595-
Node* expr = (Node *) lfirst(lc);
1596-
if (exprType(expr) != UNKNOWNOID || !IsA(expr, Const) || !((Const *) expr)->constisnull)
1597-
{
1598-
all_nullconst = false;
1599-
break;
1600-
}
1601-
}
1602-
1603-
if (all_nullconst)
1604-
ptype = INT4OID; /* in T-SQL, it is should be decided to INT4 */
1605-
}
1606-
16071541
if (ptype == UNKNOWNOID)
16081542
ptype = TEXTOID;
16091543

0 commit comments

Comments
 (0)