Skip to content

Commit

Permalink
Fix function expression deparsing implicit/explicit coercion.
Browse files Browse the repository at this point in the history
MySQL does not support explicit coercion/casting syntax, so avoid
casting like a function call, e.g. numeric(a) while deparsing.

FDW-447, Suraj Kharage, reviewed by Vaibhav Dalvi, Jeevan Ladhe, and
tested by Rajkumar Raghuwanshi.
  • Loading branch information
jeevanladhe committed Nov 15, 2021
1 parent 21efaea commit 52b31d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions deparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,28 @@ mysql_deparse_func_expr(FuncExpr *node, deparse_expr_cxt *context)
bool first;
ListCell *arg;

/*
* If the function call came from an implicit coercion, then just show the
* first argument.
*/
if (node->funcformat == COERCE_IMPLICIT_CAST)
{
deparseExpr((Expr *) linitial(node->args), context);
return;
}

/* If the function call came from a cast, then show the first argument. */
if (node->funcformat == COERCE_EXPLICIT_CAST)
{
int32 coercedTypmod;

/* Get the typmod if this is a length-coercion function */
(void) exprIsLengthCoercion((Node *) node, &coercedTypmod);

deparseExpr((Expr *) linitial(node->args), context);
return;
}

/*
* Normal function: display as proname(args).
*/
Expand Down
19 changes: 19 additions & 0 deletions expected/select.out
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,25 @@ SELECT c1, c2, c3 FROM f_test_tbl1 WHERE pg_catalog.timeofday() IS NOT NULL
500 | EMP5 | SALESMAN
(5 rows)

-- FDW-447: Fix function implicit/explicit coercion.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT * FROM f_test_tbl1 WHERE c1 = 12.2;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Foreign Scan on public.f_test_tbl1
Output: c1, c2, c3, c4, c5, c6, c7, c8
Remote query: SELECT `c1`, `c2`, `c3`, `c4`, `c5`, `c6`, `c7`, `c8` FROM `mysql_fdw_regress`.`test_tbl1` WHERE ((`c1` = 12.2))
(3 rows)

EXPLAIN (VERBOSE, COSTS OFF)
SELECT * FROM f_test_tbl1 WHERE c1::numeric = 12.2;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Foreign Scan on public.f_test_tbl1
Output: c1, c2, c3, c4, c5, c6, c7, c8
Remote query: SELECT `c1`, `c2`, `c3`, `c4`, `c5`, `c6`, `c7`, `c8` FROM `mysql_fdw_regress`.`test_tbl1` WHERE ((`c1` = 12.2))
(3 rows)

-- Cleanup
DROP TABLE l_test_tbl1;
DROP TABLE l_test_tbl2;
Expand Down
6 changes: 6 additions & 0 deletions sql/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ SELECT c1, c2, c3 FROM f_test_tbl1 WHERE pg_catalog.timeofday() IS NOT NULL
SELECT c1, c2, c3 FROM f_test_tbl1 WHERE pg_catalog.timeofday() IS NOT NULL
ORDER BY 1 limit 5;

-- FDW-447: Fix function implicit/explicit coercion.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT * FROM f_test_tbl1 WHERE c1 = 12.2;
EXPLAIN (VERBOSE, COSTS OFF)
SELECT * FROM f_test_tbl1 WHERE c1::numeric = 12.2;

-- Cleanup
DROP TABLE l_test_tbl1;
DROP TABLE l_test_tbl2;
Expand Down

0 comments on commit 52b31d8

Please sign in to comment.