From 9e50ae6b436215cff88e61c21e25a13e54e33dfc Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih@gmail.com>
Date: Tue, 18 Aug 2026 14:30:47 +0000
Subject: [PATCH v1 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.
---
 src/backend/parser/parse_graphtable.c     | 15 +++++++++------
 src/test/regress/expected/graph_table.out |  4 ++++
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c
index e323376f0ea..8a8c0a64a17 100644
--- a/src/backend/parser/parse_graphtable.c
+++ b/src/backend/parser/parse_graphtable.c
@@ -131,8 +131,9 @@ 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", propname),
+						parser_errposition(pstate, cref->location));
 			pgpform = (Form_pg_propgraph_property) GETSTRUCT(pgptup);
 
 			gpr->location = cref->location;
@@ -163,8 +164,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 +190,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 +211,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 +252,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_GRAPH_TABLE_WHERE);
 
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 33909f0feb1..70e2e99ef10 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -87,8 +87,12 @@ 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
+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

