From 6a0152a350ae9643d3ff14b79c1b25a60c9c06ae Mon Sep 17 00:00:00 2001
From: Dhruv Chauhan <chauhandhruv351@gmail.com>
Date: Sat, 25 Jul 2026 14:28:48 +0530
Subject: [PATCH v1] Fix missing space before WHERE in GRAPH_TABLE deparse

`get_graph_pattern_def()` emitted the pattern-level WHERE keyword as
"WHERE " with no leading space, so reverse-parsing produced output like
"(o IS orders)WHERE (...)".  The element-level WHERE deparse in
get_path_pattern_expr_def() already prepends a separating space; the
pattern-level branch was inconsistent with it.

The output still re-parses to the same tree, so this is cosmetic, but
the deparsed text is wrong.  Emit " WHERE " to match.

Add a regression test with a whole-pattern WHERE clause reverse-parsed
via `pg_get_viewdef()`.
---
 src/backend/utils/adt/ruleutils.c         |  2 +-
 src/test/regress/expected/graph_table.out | 13 +++++++++++++
 src/test/regress/sql/graph_table.sql      |  8 ++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 1b44b7a78d..e697fb66da 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -8134,7 +8134,7 @@ get_graph_pattern_def(GraphPattern *graph_pattern, deparse_context *context)
 
 	if (graph_pattern->whereClause)
 	{
-		appendStringInfoString(buf, "WHERE ");
+		appendStringInfoString(buf, " WHERE ");
 		get_rule_expr(graph_pattern->whereClause, context, false);
 	}
 }
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 46566b2e32..3854a5069d 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -1004,6 +1004,19 @@ SELECT pg_get_viewdef('customers_us'::regclass);
    ORDER BY g.customer_name, g.product_name;
 (1 row)
 
+-- Reverse-parse a WHERE clause on the whole graph pattern.
+CREATE VIEW customers_us_where AS
+SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers)-[IS customer_orders]->(o IS orders)
+                                  WHERE c.address = 'US'
+                                  COLUMNS (c.name AS customer_name));
+SELECT pg_get_viewdef('customers_us_where'::regclass);
+                                                                          pg_get_viewdef                                                                           
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------
+  SELECT customer_name                                                                                                                                            +
+    FROM GRAPH_TABLE (myshop MATCH (c IS customers)-[IS customer_orders]->(o IS orders) WHERE ((c.address)::text = 'US'::text) COLUMNS (c.name AS customer_name));
+(1 row)
+
+DROP VIEW customers_us_where;
 -- test view/graph nesting
 CREATE VIEW customers_view AS SELECT customer_id, 'redacted' || customer_id AS name_redacted, address FROM customers;
 SELECT * FROM customers;
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index 3fb0e50ddb..4b8b8a38e6 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -559,6 +559,14 @@ ALTER PROPERTY GRAPH myshop ALTER VERTEX TABLE products
 -- ruleutils reverse parsing
 SELECT pg_get_viewdef('customers_us'::regclass);
 
+-- Reverse-parse a WHERE clause on the whole graph pattern.
+CREATE VIEW customers_us_where AS
+SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers)-[IS customer_orders]->(o IS orders)
+                                  WHERE c.address = 'US'
+                                  COLUMNS (c.name AS customer_name));
+SELECT pg_get_viewdef('customers_us_where'::regclass);
+DROP VIEW customers_us_where;
+
 -- test view/graph nesting
 
 CREATE VIEW customers_view AS SELECT customer_id, 'redacted' || customer_id AS name_redacted, address FROM customers;
-- 
2.34.1

