From f084cb6219a81f43c3cefb048dbf133888b2e952 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Thu, 28 May 2026 02:47:33 +0530
Subject: [PATCH v20260807] View referencing labels shared by vertex and edge
 tables

Add a test for view containing labels which are shared by both vertex
and edge tables. When such a label is dropped from only vertex tables or
only edge tables, the view may be rendered invalid because it does not
find any elements associated with the label. The fix depends upon how
the SQL/PGQ standard specifies the behaviour in such a case.

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
---
 src/test/regress/expected/graph_table.out | 26 +++++++++++++++++++++--
 src/test/regress/sql/graph_table.sql      | 20 +++++++++++++++--
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index cde3114ebf4..924269babc2 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -999,8 +999,7 @@ ALTER PROPERTY GRAPH myshop ALTER VERTEX TABLE products
 ERROR:  cannot drop property price of property graph myshop because other objects depend on it
 DETAIL:  view customers_us depends on property price of property graph myshop
 HINT:  Use DROP ... CASCADE to drop the dependent objects too.
--- ruleutils reverse parsing
-SELECT pg_get_viewdef('customers_us'::regclass);
+SELECT pg_get_viewdef('customers_us'::regclass); -- ruleutils reverse parsing
                                                                                                                                                                 pg_get_viewdef                                                                                                                                                                 
 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   SELECT g.customer_name,                                                                                                                                                                                                                                                                                                                     +
@@ -1012,6 +1011,29 @@ SELECT pg_get_viewdef('customers_us'::regclass);
    ORDER BY g.customer_name, g.product_name;
 (1 row)
 
+-- l1 is shared by all vertex tables and edge tables. Dropping it only  from all
+-- vertex tables renders a view unusable. This is because the standard considers
+-- a label shared between vertex labels and edge labels as two different labels
+-- vertex label and edge label respectively. Further note that the standard
+-- still requires the two labels to have the same properties associated with
+-- them. We need the standard to clarify the behaviour in this case: should we
+-- prohibit dropping an element specific label or return no rows when the label
+-- exists but is not associated with any element of required type.
+CREATE VIEW v_shared_label AS SELECT * FROM GRAPH_TABLE (g1 MATCH (v IS l1) COLUMNS (v.elname));
+BEGIN;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v1 DROP LABEL l1;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v2 DROP LABEL l1;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v3 DROP LABEL l1;
+SELECT * FROM v_shared_label;
+ERROR:  no property graph element of type "vertex" has label "l1" associated with it in property graph "g1"
+ROLLBACK;
+SELECT pg_get_viewdef('v_shared_label'::regclass); -- ruleutils reverse parsing
+                             pg_get_viewdef                             
+------------------------------------------------------------------------
+  SELECT elname                                                        +
+    FROM GRAPH_TABLE (g1 MATCH (v IS l1) COLUMNS (v.elname AS elname));
+(1 row)
+
 -- 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 7a4189833d8..013c48de5c8 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -561,8 +561,24 @@ ALTER PROPERTY GRAPH myshop ALTER VERTEX TABLE customers
     ALTER LABEL customers DROP PROPERTIES (address);  -- error
 ALTER PROPERTY GRAPH myshop ALTER VERTEX TABLE products
     ALTER LABEL products DROP PROPERTIES (price);  -- error
--- ruleutils reverse parsing
-SELECT pg_get_viewdef('customers_us'::regclass);
+SELECT pg_get_viewdef('customers_us'::regclass); -- ruleutils reverse parsing
+
+-- l1 is shared by all vertex tables and edge tables. Dropping it only  from all
+-- vertex tables renders a view unusable. This is because the standard considers
+-- a label shared between vertex labels and edge labels as two different labels
+-- vertex label and edge label respectively. Further note that the standard
+-- still requires the two labels to have the same properties associated with
+-- them. We need the standard to clarify the behaviour in this case: should we
+-- prohibit dropping an element specific label or return no rows when the label
+-- exists but is not associated with any element of required type.
+CREATE VIEW v_shared_label AS SELECT * FROM GRAPH_TABLE (g1 MATCH (v IS l1) COLUMNS (v.elname));
+BEGIN;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v1 DROP LABEL l1;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v2 DROP LABEL l1;
+ALTER PROPERTY GRAPH g1 ALTER VERTEX TABLE v3 DROP LABEL l1;
+SELECT * FROM v_shared_label;
+ROLLBACK;
+SELECT pg_get_viewdef('v_shared_label'::regclass); -- ruleutils reverse parsing
 
 -- test view/graph nesting
 

base-commit: bf80a4c2d238834073b09c231294c4479157f16e
-- 
2.34.1

