From 48530de08d8dba01b5e9c8f321b437468ffd4097 Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih@gmail.com>
Date: Tue, 25 Aug 2026 01:41:31 +0000
Subject: [PATCH v2 1/1] Fix GRAPH_TABLE label and property error reporting

An unknown property reference in a graph query was reported with
ERRCODE_SYNTAX_ERROR.  This is not a syntax error and should be
classified as ERRCODE_UNDEFINED_OBJECT instead, matching the unknown
label check, so it can be handled appropriately by SQLSTATE processing.

In addition, neither the property nor the label lookup supplied an error
position.  Add one to both.

Author: Sami Imseih <samimseih@gmail.com>
Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Discussion: https://postgr.es/m/CAA5RZ0v27a=suTi1QBwJp9=XsPewimf3XLxv7j8TY+jQRwY7NQ@mail.gmail.com
---
 src/backend/parser/parse_graphtable.c     | 16 ++++++++++------
 src/test/regress/expected/graph_table.out |  6 +++++-
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c
index 73fbfb541f7..1514baf4b96 100644
--- a/src/backend/parser/parse_graphtable.c
+++ b/src/backend/parser/parse_graphtable.c
@@ -131,8 +131,10 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref)
 			pgptup = SearchSysCache2(PROPGRAPHPROPNAME, ObjectIdGetDatum(gpstate->graphid), CStringGetDatum(propname));
 			if (!HeapTupleIsValid(pgptup))
 				ereport(ERROR,
-						errcode(ERRCODE_SYNTAX_ERROR),
-						errmsg("property \"%s\" does not exist", propname));
+						errcode(ERRCODE_UNDEFINED_OBJECT),
+						errmsg("property \"%s\" does not exist in property graph \"%s\"",
+							   propname, get_rel_name(gpstate->graphid)),
+						parser_errposition(pstate, cref->location));
 			pgpform = (Form_pg_propgraph_property) GETSTRUCT(pgptup);
 
 			gpr->location = cref->location;
@@ -163,8 +165,9 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref)
  * graph, an error is raised.
  */
 static Node *
-transformLabelExpr(GraphTableParseState *gpstate, Node *labelexpr)
+transformLabelExpr(ParseState *pstate, Node *labelexpr)
 {
+	GraphTableParseState *gpstate = pstate->p_graph_table_pstate;
 	Node	   *result;
 
 	if (labelexpr == NULL)
@@ -188,7 +191,8 @@ transformLabelExpr(GraphTableParseState *gpstate, Node *labelexpr)
 				if (!labelid)
 					ereport(ERROR,
 							errcode(ERRCODE_UNDEFINED_OBJECT),
-							errmsg("label \"%s\" does not exist in property graph \"%s\"", labelname, get_rel_name(gpstate->graphid)));
+							errmsg("label \"%s\" does not exist in property graph \"%s\"", labelname, get_rel_name(gpstate->graphid)),
+							parser_errposition(pstate, cref->location));
 
 				lref = makeNode(GraphLabelRef);
 				lref->labelid = labelid;
@@ -208,7 +212,7 @@ transformLabelExpr(GraphTableParseState *gpstate, Node *labelexpr)
 				{
 					Node	   *arg = (Node *) lfirst(lc);
 
-					arg = transformLabelExpr(gpstate, arg);
+					arg = transformLabelExpr(pstate, arg);
 					args = lappend(args, arg);
 				}
 
@@ -249,7 +253,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep)
 
 	gpstate->cur_gep = gep;
 
-	gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr);
+	gep->labelexpr = transformLabelExpr(pstate, gep->labelexpr);
 
 	gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE);
 
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index cde3114ebf4..1208f9c7ed2 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -86,9 +86,13 @@ ERROR:  missing FROM-clause entry for table "cx"
 LINE 1: ...US')-[IS customer_orders]->(o IS orders) COLUMNS (cx.name AS...
                                                              ^
 SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.namex AS customer_name));  -- error
-ERROR:  property "namex" does not exist
+ERROR:  property "namex" does not exist in property graph "myshop"
+LINE 1: ...US')-[IS customer_orders]->(o IS orders) COLUMNS (c.namex AS...
+                                                             ^
 SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers|employees WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.name AS customer_name));  -- error
 ERROR:  label "employees" does not exist in property graph "myshop"
+LINE 1: ...me FROM GRAPH_TABLE (myshop MATCH (c IS customers|employees ...
+                                                             ^
 SELECT customer_name FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders] COLUMNS (c.name AS customer_name));  -- error
 ERROR:  syntax error at or near "COLUMNS"
 LINE 1: ...mers WHERE c.address = 'US')-[IS customer_orders] COLUMNS (c...
-- 
2.47.3

