From da8b93ad0d43d90ed7f7cd0654be722694561c63 Mon Sep 17 00:00:00 2001 From: Seongjun Shin Date: Wed, 25 Nov 2026 14:32:34 +0900 Subject: [PATCH v1] Fix deparsing of SUBSTRING SIMILAR with an operator as first argument get_func_sql_syntax() printed the first argument of SUBSTRING(... SIMILAR ... ESCAPE ...) without parentheses. SIMILAR binds more tightly than comparison operators, so an argument such as "s = 1" was deparsed into SQL that fails to parse, breaking dump and restore of views using it. Use get_rule_expr_paren() for that argument, as the IS NORMALIZED case already does. Author: Seongjun Shin --- src/backend/utils/adt/ruleutils.c | 3 ++- src/test/regress/expected/create_view.out | 17 +++++++++++++++++ src/test/regress/sql/create_view.sql | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index b2d5327cc20..ebc70f9a89a 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -11474,7 +11474,8 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context) case F_SUBSTRING_TEXT_TEXT_TEXT: /* SUBSTRING SIMILAR/ESCAPE */ appendStringInfoString(buf, "SUBSTRING("); - get_rule_expr((Node *) linitial(expr->args), context, false); + get_rule_expr_paren((Node *) linitial(expr->args), context, false, + (Node *) expr); appendStringInfoString(buf, " SIMILAR "); get_rule_expr((Node *) lsecond(expr->args), context, false); appendStringInfoString(buf, " ESCAPE "); diff --git a/src/test/regress/expected/create_view.out b/src/test/regress/expected/create_view.out index c2892dfb6d0..33ca73223d3 100644 --- a/src/test/regress/expected/create_view.out +++ b/src/test/regress/expected/create_view.out @@ -2117,6 +2117,23 @@ select pg_get_viewdef('tt201v', true); FROM SYSTEM_USER "system_user"("system_user")) AS su2; (1 row) +-- SUBSTRING SIMILAR with an operator as first argument +create function tt202f(text, int) returns text + language sql immutable as 'select $1 || $2'; +create operator < (leftarg = text, rightarg = int, function = tt202f); +create view tt202v as +select substring((s < 1) similar '%#"1#"' escape '#') as ss +from (values ('x'::text)) t(s); +select pg_get_viewdef('tt202v', true); + pg_get_viewdef +-------------------------------------------------------------------------- + SELECT SUBSTRING((s < 1) SIMILAR '%#"1#"'::text ESCAPE '#'::text) AS ss+ + FROM ( VALUES ('x'::text)) t(s); +(1 row) + +drop view tt202v; +drop operator < (text, int); +drop function tt202f(text, int); -- corner cases with empty join conditions create view tt21v as select * from tt5 natural inner join tt6; diff --git a/src/test/regress/sql/create_view.sql b/src/test/regress/sql/create_view.sql index f80b9823f00..857d444d073 100644 --- a/src/test/regress/sql/create_view.sql +++ b/src/test/regress/sql/create_view.sql @@ -774,6 +774,18 @@ select (select * from SYSTEM_USER) as su2; select pg_get_viewdef('tt201v', true); +-- SUBSTRING SIMILAR with an operator as first argument +create function tt202f(text, int) returns text + language sql immutable as 'select $1 || $2'; +create operator < (leftarg = text, rightarg = int, function = tt202f); +create view tt202v as +select substring((s < 1) similar '%#"1#"' escape '#') as ss +from (values ('x'::text)) t(s); +select pg_get_viewdef('tt202v', true); +drop view tt202v; +drop operator < (text, int); +drop function tt202f(text, int); + -- corner cases with empty join conditions create view tt21v as -- 2.50.1 (Apple Git-155)