From b98100ff6a44d89e76cb1863a9f9c4dbe1e784c9 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Wed, 17 Jul 2024 09:37:43 +0530
Subject: [PATCH 6/6] WIP: Report error location for implicit property graph
 components

When no property or label is provided for a property graph element, they are
derived from the underlying table. Report the location of the associated
element when reporting an error in derivation of implicit properties or labels
to simplify debugging such errors.

NOTE: if this helps in debugging the CI problem, we should consider this to be
included in the main patch.

Ashutosh Bapat
---
 src/backend/commands/propgraphcmds.c | 55 ++++++++++++++++++----------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/src/backend/commands/propgraphcmds.c b/src/backend/commands/propgraphcmds.c
index f204fc3125..d00f2452e8 100644
--- a/src/backend/commands/propgraphcmds.c
+++ b/src/backend/commands/propgraphcmds.c
@@ -60,6 +60,8 @@ struct element_info
 	ArrayType  *destref;
 
 	List	   *labels;
+
+	ParseLoc	location;
 };
 
 
@@ -71,9 +73,9 @@ static void propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols,
 										ArrayType **outkey, ArrayType **outref);
 static ArrayType *array_from_column_list(ParseState *pstate, const List *colnames, int location, Relation element_rel);
 static ArrayType *array_from_attnums(int numattrs, const AttrNumber *attnums);
-static Oid	insert_element_record(ObjectAddress pgaddress, struct element_info *einfo);
+static Oid	insert_element_record(ParseState *pstate, ObjectAddress pgaddress, struct element_info *einfo);
 static Oid	insert_label_record(Oid graphid, Oid peoid, const char *label);
-static void insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties);
+static void insert_property_records(ParseState *pstate, Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties);
 static void insert_property_record(Oid graphid, Oid ellabeloid, Oid pgerelid, const char *propname, const Expr *expr);
 static void check_element_properties(Oid peoid);
 static void check_element_label_properties(Oid ellabeloid);
@@ -117,6 +119,7 @@ CreatePropGraph(ParseState *pstate, const CreatePropGraphStmt *stmt)
 
 		vinfo = palloc0_object(struct element_info);
 		vinfo->kind = PGEKIND_VERTEX;
+		vinfo->location = vertex->location;
 
 		vinfo->relid = RangeVarGetRelidExtended(vertex->vtable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
 
@@ -160,6 +163,7 @@ CreatePropGraph(ParseState *pstate, const CreatePropGraphStmt *stmt)
 
 		einfo = palloc0_object(struct element_info);
 		einfo->kind = PGEKIND_EDGE;
+		einfo->location = edge->location;
 
 		einfo->relid = RangeVarGetRelidExtended(edge->etable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
 
@@ -258,7 +262,7 @@ CreatePropGraph(ParseState *pstate, const CreatePropGraphStmt *stmt)
 		struct element_info *vinfo = lfirst(lc);
 		Oid			peoid;
 
-		peoid = insert_element_record(pgaddress, vinfo);
+		peoid = insert_element_record(pstate, pgaddress, vinfo);
 		element_oids = lappend_oid(element_oids, peoid);
 	}
 
@@ -293,7 +297,7 @@ CreatePropGraph(ParseState *pstate, const CreatePropGraphStmt *stmt)
 		Assert(einfo->destvertexid);
 		Assert(einfo->srcrelid);
 		Assert(einfo->destrelid);
-		peoid = insert_element_record(pgaddress, einfo);
+		peoid = insert_element_record(pstate, pgaddress, einfo);
 		element_oids = lappend_oid(element_oids, peoid);
 	}
 
@@ -491,7 +495,7 @@ array_of_attnums_to_objectaddrs(Oid relid, ArrayType *arr, ObjectAddresses *addr
  * inserts labels and properties into their respective catalogs.
  */
 static Oid
-insert_element_record(ObjectAddress pgaddress, struct element_info *einfo)
+insert_element_record(ParseState *pstate, ObjectAddress pgaddress, struct element_info *einfo)
 {
 	Oid			graphid = pgaddress.objectId;
 	Relation	rel;
@@ -582,11 +586,16 @@ insert_element_record(ObjectAddress pgaddress, struct element_info *einfo)
 			PropGraphLabelAndProperties *lp = lfirst_node(PropGraphLabelAndProperties, lc);
 			Oid			ellabeloid;
 
+			if (lp->location == -1)
+				lp->location = einfo->location;
+			if (lp->properties->location == -1)
+				lp->properties->location = lp->location;
+
 			if (lp->label)
 				ellabeloid = insert_label_record(graphid, peoid, lp->label);
 			else
 				ellabeloid = insert_label_record(graphid, peoid, einfo->aliasname);
-			insert_property_records(graphid, ellabeloid, einfo->relid, lp->properties);
+			insert_property_records(pstate, graphid, ellabeloid, einfo->relid, lp->properties);
 
 			CommandCounterIncrement();
 		}
@@ -597,10 +606,10 @@ insert_element_record(ObjectAddress pgaddress, struct element_info *einfo)
 		PropGraphProperties *pr = makeNode(PropGraphProperties);
 
 		pr->all = true;
-		pr->location = -1;
+		pr->location = einfo->location;
 
 		ellabeloid = insert_label_record(graphid, peoid, einfo->aliasname);
-		insert_property_records(graphid, ellabeloid, einfo->relid, pr);
+		insert_property_records(pstate, graphid, ellabeloid, einfo->relid, pr);
 	}
 
 	return peoid;
@@ -691,15 +700,18 @@ insert_label_record(Oid graphid, Oid peoid, const char *label)
  * Insert records for properties into the pg_propgraph_property catalog.
  */
 static void
-insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties)
+insert_property_records(ParseState *pstate, Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties)
 {
 	List	   *proplist = NIL;
-	ParseState *pstate;
+	ParseState *elem_pstate;
 	ParseNamespaceItem *nsitem;
 	List	   *tp;
 	Relation	rel;
 	ListCell   *lc;
 
+	if (properties->location == -1)
+		elog(ERROR, "properties clause without a valid location");
+
 	if (properties->all)
 	{
 		Relation	attRelation;
@@ -727,11 +739,11 @@ insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGra
 			rt = makeNode(ResTarget);
 
 			cr->fields = list_make1(makeString(NameStr(att->attname)));
-			cr->location = -1;
+			cr->location = properties->location;
 
 			rt->name = pstrdup(NameStr(att->attname));
 			rt->val = (Node *) cr;
-			rt->location = -1;
+			rt->location = properties->location;
 
 			proplist = lappend(proplist, rt);
 		}
@@ -756,18 +768,19 @@ insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGra
 
 	rel = table_open(pgerelid, AccessShareLock);
 
-	pstate = make_parsestate(NULL);
-	nsitem = addRangeTableEntryForRelation(pstate,
+	elem_pstate = make_parsestate(NULL);
+	elem_pstate->p_sourcetext = pstate->p_sourcetext;
+	nsitem = addRangeTableEntryForRelation(elem_pstate,
 										   rel,
 										   AccessShareLock,
 										   NULL,
 										   false,
 										   true);
-	addNSItemToQuery(pstate, nsitem, true, true, true);
+	addNSItemToQuery(elem_pstate, nsitem, true, true, true);
 
 	table_close(rel, NoLock);
 
-	tp = transformTargetList(pstate, proplist, EXPR_KIND_PROPGRAPH_PROPERTY);
+	tp = transformTargetList(elem_pstate, proplist, EXPR_KIND_PROPGRAPH_PROPERTY);
 
 	foreach(lc, tp)
 	{
@@ -1167,6 +1180,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 
 		vinfo = palloc0_object(struct element_info);
 		vinfo->kind = PGEKIND_VERTEX;
+		vinfo->location = vertex->location;
 
 		vinfo->relid = RangeVarGetRelidExtended(vertex->vtable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
 
@@ -1190,7 +1204,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 
 		table_close(rel, NoLock);
 
-		peoid = insert_element_record(pgaddress, vinfo);
+		peoid = insert_element_record(pstate, pgaddress, vinfo);
 
 		CommandCounterIncrement();
 		check_element_properties(peoid);
@@ -1208,6 +1222,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 
 		einfo = palloc0_object(struct element_info);
 		einfo->kind = PGEKIND_EDGE;
+		einfo->location = edge->location;
 
 		einfo->relid = RangeVarGetRelidExtended(edge->etable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
 
@@ -1250,7 +1265,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 
 		table_close(rel, NoLock);
 
-		peoid = insert_element_record(pgaddress, einfo);
+		peoid = insert_element_record(pstate, pgaddress, einfo);
 
 		CommandCounterIncrement();
 		check_element_properties(peoid);
@@ -1311,7 +1326,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 		pgerelid = get_element_relid(peoid);
 
 		ellabeloid = insert_label_record(pgrelid, peoid, lp->label);
-		insert_property_records(pgrelid, ellabeloid, pgerelid, lp->properties);
+		insert_property_records(pstate, pgrelid, ellabeloid, pgerelid, lp->properties);
 
 		CommandCounterIncrement();
 		check_element_properties(peoid);
@@ -1400,7 +1415,7 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
 
 		pgerelid = get_element_relid(peoid);
 
-		insert_property_records(pgrelid, ellabeloid, pgerelid, stmt->add_properties);
+		insert_property_records(pstate, pgrelid, ellabeloid, pgerelid, stmt->add_properties);
 
 		CommandCounterIncrement();
 		check_element_properties(peoid);
-- 
2.34.1

