Skip to content

Commit 17073e8

Browse files
committed
try to cast constant input string to target type
1 parent c7ce700 commit 17073e8

File tree

5 files changed

+54
-40
lines changed

5 files changed

+54
-40
lines changed

expected/plpgsql_check_active.out

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,10 @@ end;
17151715
$body$
17161716
language 'plpgsql' stable;
17171717
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1718-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1720-
foreach_array_loop | 7 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1721-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1722-
(2 rows)
1718+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1720+
foreach_array_loop | 7 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1721+
(1 row)
17231722

17241723
create or replace function foreach_array_loop()
17251724
returns void as
@@ -1771,11 +1770,10 @@ end;
17711770
$body$
17721771
language 'plpgsql' stable;
17731772
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1774-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1775-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1776-
foreach_array_loop | 5 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1777-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1778-
(2 rows)
1773+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1774+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1775+
foreach_array_loop | 5 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1776+
(1 row)
17791777

17801778
drop function foreach_array_loop();
17811779
create or replace function scan_rows(int[]) returns void AS $$

expected/plpgsql_check_active_2.out

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,10 @@ end;
17151715
$body$
17161716
language 'plpgsql' stable;
17171717
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1718-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1720-
foreach_array_loop | 7 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1721-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1722-
(2 rows)
1718+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1720+
foreach_array_loop | 7 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1721+
(1 row)
17231722

17241723
create or replace function foreach_array_loop()
17251724
returns void as
@@ -1771,11 +1770,10 @@ end;
17711770
$body$
17721771
language 'plpgsql' stable;
17731772
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1774-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1775-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1776-
foreach_array_loop | 5 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1777-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1778-
(2 rows)
1773+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1774+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1775+
foreach_array_loop | 5 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1776+
(1 row)
17791777

17801778
drop function foreach_array_loop();
17811779
create or replace function scan_rows(int[]) returns void AS $$

expected/plpgsql_check_active_3.out

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,10 @@ end;
17151715
$body$
17161716
language 'plpgsql' stable;
17171717
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1718-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1720-
foreach_array_loop | 7 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1721-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1722-
(2 rows)
1718+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1720+
foreach_array_loop | 7 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1721+
(1 row)
17231722

17241723
create or replace function foreach_array_loop()
17251724
returns void as
@@ -1771,11 +1770,10 @@ end;
17711770
$body$
17721771
language 'plpgsql' stable;
17731772
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1774-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1775-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1776-
foreach_array_loop | 5 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1777-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1778-
(2 rows)
1773+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1774+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1775+
foreach_array_loop | 5 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1776+
(1 row)
17791777

17801778
drop function foreach_array_loop();
17811779
create or replace function scan_rows(int[]) returns void AS $$

expected/plpgsql_check_active_4.out

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,11 +1715,10 @@ end;
17151715
$body$
17161716
language 'plpgsql' stable;
17171717
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1718-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1720-
foreach_array_loop | 7 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1721-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1722-
(2 rows)
1718+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1719+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1720+
foreach_array_loop | 7 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1721+
(1 row)
17231722

17241723
create or replace function foreach_array_loop()
17251724
returns void as
@@ -1771,11 +1770,10 @@ end;
17711770
$body$
17721771
language 'plpgsql' stable;
17731772
select * from plpgsql_check_function_tb('foreach_array_loop()', performance_warnings := true);
1774-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1775-
--------------------+--------+--------------------+----------+--------------------------------------------------+-------------------------------------+-----------------------------------------------------------------------------------+-------------+----------+-------+-----------------------
1776-
foreach_array_loop | 5 | FOREACH over array | 42804 | target type is different type than source type | cast "date" value to "integer" type | There are no possible explicit coercion between those types, possibly bug! | warning | | | at FOREACH over array
1777-
foreach_array_loop | | | 00000 | routine is marked as STABLE, should be IMMUTABLE | | When you fix this issue, please, recheck other functions that uses this function. | performance | | |
1778-
(2 rows)
1773+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
1774+
--------------------+--------+--------------------+----------+-----------------------------------------------------+--------+------+-------+----------+-------+---------
1775+
foreach_array_loop | 5 | FOREACH over array | 22P02 | invalid input syntax for type integer: "01-01-2014" | | | error | | |
1776+
(1 row)
17791777

17801778
drop function foreach_array_loop();
17811779
create or replace function scan_rows(int[]) returns void AS $$

src/check_expr.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,28 @@ plpgsql_check_expr_as_rvalue(PLpgSQL_checkstate *cstate, PLpgSQL_expr *expr,
12821282

12831283
node = plpgsql_check_expr_get_node(cstate, expr, false);
12841284

1285+
if (node && exprType((Node *) node) != expected_typoid)
1286+
{
1287+
char *str;
1288+
1289+
/* When expr is constant string, try it cast to target type */
1290+
str = plpgsql_check_get_const_string(cstate, node, NULL);
1291+
1292+
if (str)
1293+
{
1294+
Oid infunc;
1295+
Oid intypeioparam;
1296+
Oid typeid;
1297+
1298+
typeid = use_element_type ? get_array_type(expected_typoid) : expected_typoid;
1299+
1300+
getTypeInputInfo(typeid, &infunc, &intypeioparam);
1301+
(void) OidInputFunctionCall(infunc, str, intypeioparam, -1);
1302+
1303+
pfree(str);
1304+
}
1305+
}
1306+
12851307
if (bms_is_member(target_dno, expr->paramnos))
12861308
{
12871309
/*

0 commit comments

Comments
 (0)