From c2ad26efdedb47f2455a297932dca36380e3740d Mon Sep 17 00:00:00 2001 From: jian he Date: Wed, 25 Jun 2025 15:31:26 +0800 Subject: [PATCH v11 1/1] misc refactoring SubscriptingRef deparsing --- src/backend/utils/adt/ruleutils.c | 6 +++++- src/test/regress/expected/jsonb.out | 20 ++++++++++++++++++-- src/test/regress/sql/jsonb.sql | 8 ++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index c18e08dee29..fdd8b224906 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -9334,6 +9334,10 @@ get_rule_expr(Node *node, deparse_context *context, */ need_parens = !IsA(sbsref->refexpr, Var) && !IsA(sbsref->refexpr, FieldSelect); + + if (sbsref->refjsonbpath != NULL) + need_parens = true; + if (need_parens) appendStringInfoChar(buf, '('); get_rule_expr((Node *) sbsref->refexpr, context, showimplicit); @@ -13008,7 +13012,7 @@ printSubscripts(SubscriptingRef *sbsref, deparse_context *context) { Node *up = (Node *) lfirst(uplist_item); - if (IsA(up, String)) + if (up != NULL && IsA(up, String)) { appendStringInfoChar(buf, '.'); appendStringInfoString(buf, quote_identifier(strVal(up))); diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index 114200e0e43..5d536ea5c95 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -6038,7 +6038,7 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT (t.jb).a FROM test_jsonb_dot_notation t; QUERY PLAN ---------------------------------------------- Seq Scan on public.test_jsonb_dot_notation t - Output: jb.a + Output: (jb).a (2 rows) SELECT (t.jb).a FROM test_jsonb_dot_notation t; @@ -6051,7 +6051,7 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT (jb).a[1] FROM test_jsonb_dot_notation; QUERY PLAN -------------------------------------------- Seq Scan on public.test_jsonb_dot_notation - Output: jb.a[1] + Output: (jb).a[1] (2 rows) SELECT (jb).a[1] FROM test_jsonb_dot_notation; @@ -6060,6 +6060,22 @@ SELECT (jb).a[1] FROM test_jsonb_dot_notation; 2 (1 row) +CREATE VIEW v1 AS SELECT (jb).a[3].x.y FROM test_jsonb_dot_notation; +CREATE VIEW v2 AS SELECT (jb).a[3:].x.y[:-1] FROM test_jsonb_dot_notation; +CREATE VIEW v3 AS SELECT (jb).a[:3].x.y[-1:] FROM test_jsonb_dot_notation; +\sv v1 +CREATE OR REPLACE VIEW public.v1 AS + SELECT (jb).a[3].x.y AS y + FROM test_jsonb_dot_notation +\sv v2 +CREATE OR REPLACE VIEW public.v2 AS + SELECT (jb).a[3:].x.y[0:'-1'::integer] AS y + FROM test_jsonb_dot_notation +\sv v3 +CREATE OR REPLACE VIEW public.v3 AS + SELECT (jb).a[0:3].x.y['-1'::integer:] AS y + FROM test_jsonb_dot_notation +DROP VIEW v1, v2, v3; -- jsonb array access in plpgsql DO $$ DECLARE diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index 4bd3990fb55..6aa71c7c3a4 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -1634,6 +1634,14 @@ SELECT (t.jb).a FROM test_jsonb_dot_notation t; EXPLAIN (VERBOSE, COSTS OFF) SELECT (jb).a[1] FROM test_jsonb_dot_notation; SELECT (jb).a[1] FROM test_jsonb_dot_notation; +CREATE VIEW v1 AS SELECT (jb).a[3].x.y FROM test_jsonb_dot_notation; +CREATE VIEW v2 AS SELECT (jb).a[3:].x.y[:-1] FROM test_jsonb_dot_notation; +CREATE VIEW v3 AS SELECT (jb).a[:3].x.y[-1:] FROM test_jsonb_dot_notation; +\sv v1 +\sv v2 +\sv v3 +DROP VIEW v1, v2, v3; + -- jsonb array access in plpgsql DO $$ DECLARE -- 2.34.1