Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ee0091

Browse files
committed
Fix deparsing FETCH FIRST <expr> ROWS WITH TIES
In the grammar, <expr> is a c_expr, which accepts only a limited setof simple constants and expressions without parens. The deparsinglogic didn't quite match the grammar rule, and failed to use parense.g. for "5::bigint".To fix, always surround the expression with parens. Would be nice toomit the parens in simple cases, but unfortunately it's non-trivial todetect such simple cases. Even if the expression is a simple literal123 in the original query, after parse analysis it becomes a FuncExprwith COERCE_IMPLICIT_CAST rather than a simple Const.Reported-by: yonghao leeBackpatch-through: 13Discussion:https://www.postgresql.org/message-id/18929-077d6b7093b176e2@postgresql.org
1 parente323d9d commit7ee0091

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

‎src/backend/utils/adt/ruleutils.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5300,9 +5300,19 @@ get_select_query_def(Query *query, deparse_context *context,
53005300
{
53015301
if (query->limitOption==LIMIT_OPTION_WITH_TIES)
53025302
{
5303+
/*
5304+
* The limitCount arg is a c_expr, so it needs parens. Simple
5305+
* literals and function expressions would not need parens, but
5306+
* unfortunately it's hard to tell if the expression will be
5307+
* printed as a simple literal like 123 or as a typecast
5308+
* expression, like '-123'::int4. The grammar accepts the former
5309+
* without quoting, but not the latter.
5310+
*/
53035311
appendContextKeyword(context," FETCH FIRST ",
53045312
-PRETTYINDENT_STD,PRETTYINDENT_STD,0);
5313+
appendStringInfoChar(buf,'(');
53055314
get_rule_expr(query->limitCount,context, false);
5315+
appendStringInfoChar(buf,')');
53065316
appendStringInfo(buf," ROWS WITH TIES");
53075317
}
53085318
else

‎src/test/regress/expected/limit.out

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ View definition:
643643
WHERE onek.thousand < 995
644644
ORDER BY onek.thousand
645645
OFFSET 10
646-
FETCH FIRST5 ROWS WITH TIES;
646+
FETCH FIRST(5) ROWS WITH TIES;
647647

648648
CREATE VIEW limit_thousand_v_2 AS SELECT thousand FROM onek WHERE thousand < 995
649649
ORDER BY thousand OFFSET 10 FETCH FIRST 5 ROWS ONLY;
@@ -675,15 +675,29 @@ View definition:
675675
FROM onek
676676
WHERE onek.thousand < 995
677677
ORDER BY onek.thousand
678-
FETCH FIRST (NULL::integer + 1) ROWS WITH TIES;
678+
FETCH FIRST ((NULL::integer + 1)) ROWS WITH TIES;
679679

680680
CREATE VIEW limit_thousand_v_4 AS SELECT thousand FROM onek WHERE thousand < 995
681-
ORDER BY thousand FETCH FIRSTNULL ROWSONLY;
681+
ORDER BY thousand FETCH FIRST(5::bigint) ROWSWITH TIES;
682682
\d+ limit_thousand_v_4
683683
View "public.limit_thousand_v_4"
684684
Column | Type | Collation | Nullable | Default | Storage | Description
685685
----------+---------+-----------+----------+---------+---------+-------------
686686
thousand | integer | | | | plain |
687+
View definition:
688+
SELECT onek.thousand
689+
FROM onek
690+
WHERE onek.thousand < 995
691+
ORDER BY onek.thousand
692+
FETCH FIRST (5::bigint) ROWS WITH TIES;
693+
694+
CREATE VIEW limit_thousand_v_5 AS SELECT thousand FROM onek WHERE thousand < 995
695+
ORDER BY thousand FETCH FIRST NULL ROWS ONLY;
696+
\d+ limit_thousand_v_5
697+
View "public.limit_thousand_v_5"
698+
Column | Type | Collation | Nullable | Default | Storage | Description
699+
----------+---------+-----------+----------+---------+---------+-------------
700+
thousand | integer | | | | plain |
687701
View definition:
688702
SELECT onek.thousand
689703
FROM onek

‎src/test/regress/sql/limit.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ CREATE VIEW limit_thousand_v_3 AS SELECT thousand FROM onek WHERE thousand < 995
196196
ORDER BY thousand FETCH FIRST (NULL+1) ROWS WITH TIES;
197197
\d+ limit_thousand_v_3
198198
CREATEVIEWlimit_thousand_v_4ASSELECT thousandFROM onekWHERE thousand<995
199-
ORDER BY thousand FETCH FIRSTNULL ROWSONLY;
199+
ORDER BY thousand FETCH FIRST(5::bigint) ROWSWITH TIES;
200200
\d+ limit_thousand_v_4
201+
CREATEVIEWlimit_thousand_v_5ASSELECT thousandFROM onekWHERE thousand<995
202+
ORDER BY thousand FETCH FIRSTNULL ROWS ONLY;
203+
\d+ limit_thousand_v_5
201204
-- leave these views

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp