Skip to content

Commit 9d4f38e

Browse files
committed
Fix Issue 1884: Ambiguous column reference
Fix Issue 1884: Ambiguous column reference and invalid AGT header errors. Note: This PR was created with AI tools and a human, or 2. This commit addresses two related bugs that occur when using SET to store graph elements (vertices, edges, paths) as property values: Issue 1884 - "column reference is ambiguous" error: When a Cypher query uses the same variable in both the SET expression RHS and the RETURN clause (e.g., SET n.prop = n RETURN n), PostgreSQL would report "column reference is ambiguous" because the variable appeared in multiple subqueries without proper qualification. Solution: The fix for this issue was already in place through the target entry naming scheme that qualifies column references. "Invalid AGT header value" offset error: When deserializing nested VERTEX, EDGE, or PATH values stored in properties, the system would fail with errors like "Invalid AGT header value: 0x00000041". This occurred because ag_serialize_extended_type() did not include alignment padding (padlen) in the agtentry length calculation for these types, while fill_agtype_value() uses INTALIGN() when reading, causing offset mismatch. Solution: Modified ag_serialize_extended_type() in agtype_ext.c to include padlen in the agtentry length for VERTEX, EDGE, and PATH cases, matching the existing pattern used for INTEGER, FLOAT, and NUMERIC types: *agtentry = AGTENTRY_IS_AGTYPE | (padlen + (AGTENTRY_OFFLENMASK & ...)); This ensures the serialized length accounts for alignment padding, allowing correct deserialization of nested graph elements. Appropriate regression tests were added to verify the fixes. Co-authored by: Zainab Saad <105385638+Zainab-Saad@users.noreply.github.com> modified: regress/expected/cypher_set.out modified: regress/sql/cypher_set.sql modified: src/backend/parser/cypher_clause.c modified: src/backend/utils/adt/agtype_ext.c
1 parent 8bdeec5 commit 9d4f38e

4 files changed

Lines changed: 420 additions & 6 deletions

File tree

regress/expected/cypher_set.out

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,224 @@ SELECT * FROM cypher('issue_1634', $$ MATCH (u) DELETE (u) $$) AS (u agtype);
988988
---
989989
(0 rows)
990990

991+
--
992+
-- Issue 1884: column reference is ambiguous when using same variable in
993+
-- SET expression and RETURN clause
994+
--
995+
-- These tests cover:
996+
-- 1. "column reference is ambiguous" error when variable is used in both
997+
-- SET expression RHS (e.g., SET n.prop = n) and RETURN clause
998+
-- 2. "Invalid AGT header value" error caused by incorrect offset calculation
999+
-- when nested VERTEX/EDGE/PATH values are serialized in properties
1000+
--
1001+
-- Tests use isolated data to keep output manageable and avoid cumulative nesting
1002+
--
1003+
SELECT * FROM create_graph('issue_1884');
1004+
NOTICE: graph "issue_1884" has been created
1005+
create_graph
1006+
--------------
1007+
1008+
(1 row)
1009+
1010+
-- ============================================================================
1011+
-- Test Group A: Basic "column reference is ambiguous" fix (Issue 1884)
1012+
-- ============================================================================
1013+
-- Test A1: Core issue - SET n.prop = n with RETURN n (the original bug)
1014+
SELECT * FROM cypher('issue_1884', $$
1015+
CREATE (n:TestA1 {name: 'A1'})
1016+
SET n.self = n
1017+
RETURN n
1018+
$$) AS (result agtype);
1019+
result
1020+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1021+
{"id": 844424930131969, "label": "TestA1", "properties": {"name": "A1", "self": {"id": 844424930131969, "label": "TestA1", "properties": {"name": "A1"}}::vertex}}::vertex
1022+
(1 row)
1023+
1024+
-- Test A2: Multiple variables in SET and RETURN
1025+
SELECT * FROM cypher('issue_1884', $$
1026+
CREATE (a:TestA2 {name: 'A'})-[e:LINK {w: 1}]->(b:TestA2 {name: 'B'})
1027+
SET a.edge = e, b.edge = e
1028+
RETURN a, e, b
1029+
$$) AS (a agtype, e agtype, b agtype);
1030+
a | e | b
1031+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1032+
{"id": 1125899906842625, "label": "TestA2", "properties": {"edge": {"id": 1407374883553281, "label": "LINK", "end_id": 1125899906842626, "start_id": 1125899906842625, "properties": {"w": 1}}::edge, "name": "A"}}::vertex | {"id": 1407374883553281, "label": "LINK", "end_id": 1125899906842626, "start_id": 1125899906842625, "properties": {"w": 1}}::edge | {"id": 1125899906842626, "label": "TestA2", "properties": {"edge": {"id": 1407374883553281, "label": "LINK", "end_id": 1125899906842626, "start_id": 1125899906842625, "properties": {"w": 1}}::edge, "name": "B"}}::vertex
1033+
(1 row)
1034+
1035+
-- Test A3: SET edge property to node reference
1036+
SELECT * FROM cypher('issue_1884', $$
1037+
CREATE (a:TestA3 {name: 'X'})-[e:REL]->(b:TestA3 {name: 'Y'})
1038+
SET e.src = a, e.dst = b
1039+
RETURN e
1040+
$$) AS (e agtype);
1041+
e
1042+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1043+
{"id": 1970324836974593, "label": "REL", "end_id": 1688849860263938, "start_id": 1688849860263937, "properties": {"dst": {"id": 1688849860263938, "label": "TestA3", "properties": {"name": "Y"}}::vertex, "src": {"id": 1688849860263937, "label": "TestA3", "properties": {"name": "X"}}::vertex}}::edge
1044+
(1 row)
1045+
1046+
-- ============================================================================
1047+
-- Test Group B: Nested VERTEX/EDGE/PATH serialization (offset error fix)
1048+
-- ============================================================================
1049+
-- Test B1: Vertex nested in vertex property (tests VERTEX serialization)
1050+
SELECT * FROM cypher('issue_1884', $$
1051+
CREATE (n:TestB1 {val: 1})
1052+
SET n.copy = n
1053+
RETURN n
1054+
$$) AS (result agtype);
1055+
result
1056+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
1057+
{"id": 2251799813685249, "label": "TestB1", "properties": {"val": 1, "copy": {"id": 2251799813685249, "label": "TestB1", "properties": {"val": 1}}::vertex}}::vertex
1058+
(1 row)
1059+
1060+
-- Verify nested vertex can be read back
1061+
SELECT * FROM cypher('issue_1884', $$
1062+
MATCH (n:TestB1)
1063+
RETURN n.copy
1064+
$$) AS (copy agtype);
1065+
copy
1066+
-------------------------------------------------------------------------------
1067+
{"id": 2251799813685249, "label": "TestB1", "properties": {"val": 1}}::vertex
1068+
(1 row)
1069+
1070+
-- Test B2: Edge nested in node property (tests EDGE serialization)
1071+
SELECT * FROM cypher('issue_1884', $$
1072+
CREATE (a:TestB2 {name: 'start'})-[e:B2REL {x: 100}]->(b:TestB2 {name: 'end'})
1073+
SET a.myEdge = e
1074+
RETURN a
1075+
$$) AS (a agtype);
1076+
a
1077+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1078+
{"id": 2533274790395905, "label": "TestB2", "properties": {"name": "start", "myEdge": {"id": 2814749767106561, "label": "B2REL", "end_id": 2533274790395906, "start_id": 2533274790395905, "properties": {"x": 100}}::edge}}::vertex
1079+
(1 row)
1080+
1081+
-- Verify nested edge can be read back
1082+
SELECT * FROM cypher('issue_1884', $$
1083+
MATCH (n:TestB2 {name: 'start'})
1084+
RETURN n.myEdge
1085+
$$) AS (edge agtype);
1086+
edge
1087+
--------------------------------------------------------------------------------------------------------------------------------------
1088+
{"id": 2814749767106561, "label": "B2REL", "end_id": 2533274790395906, "start_id": 2533274790395905, "properties": {"x": 100}}::edge
1089+
(1 row)
1090+
1091+
-- Test B3: Path nested in node property (tests PATH serialization)
1092+
SELECT * FROM cypher('issue_1884', $$
1093+
CREATE (a:TestB3)-[e:B3REL]->(b:TestB3)
1094+
WITH a, e, b
1095+
MATCH p = (a)-[e]->(b)
1096+
SET a.myPath = p
1097+
RETURN a
1098+
$$) AS (a agtype);
1099+
a
1100+
---
1101+
(0 rows)
1102+
1103+
-- Verify nested path can be read back
1104+
SELECT * FROM cypher('issue_1884', $$
1105+
MATCH (n:TestB3)
1106+
WHERE n.myPath IS NOT NULL
1107+
RETURN n.myPath
1108+
$$) AS (path agtype);
1109+
path
1110+
------
1111+
(0 rows)
1112+
1113+
-- ============================================================================
1114+
-- Test Group C: Nested structures in arrays and maps
1115+
-- ============================================================================
1116+
-- Test C1: Vertex in array
1117+
SELECT * FROM cypher('issue_1884', $$
1118+
CREATE (n:TestC1 {tag: 'arrtest'})
1119+
SET n.arr = [n]
1120+
RETURN n
1121+
$$) AS (result agtype);
1122+
result
1123+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1124+
{"id": 3659174697238529, "label": "TestC1", "properties": {"arr": [{"id": 3659174697238529, "label": "TestC1", "properties": {"tag": "arrtest"}}::vertex], "tag": "arrtest"}}::vertex
1125+
(1 row)
1126+
1127+
-- Verify array with nested vertex
1128+
SELECT * FROM cypher('issue_1884', $$
1129+
MATCH (n:TestC1)
1130+
RETURN n.arr[0]
1131+
$$) AS (elem agtype);
1132+
elem
1133+
---------------------------------------------------------------------------------------
1134+
{"id": 3659174697238529, "label": "TestC1", "properties": {"tag": "arrtest"}}::vertex
1135+
(1 row)
1136+
1137+
-- Test C2: Vertex in map
1138+
SELECT * FROM cypher('issue_1884', $$
1139+
CREATE (n:TestC2 {tag: 'maptest'})
1140+
SET n.obj = {node: n}
1141+
RETURN n
1142+
$$) AS (result agtype);
1143+
result
1144+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1145+
{"id": 3940649673949185, "label": "TestC2", "properties": {"obj": {"node": {"id": 3940649673949185, "label": "TestC2", "properties": {"tag": "maptest"}}::vertex}, "tag": "maptest"}}::vertex
1146+
(1 row)
1147+
1148+
-- Verify map with nested vertex
1149+
SELECT * FROM cypher('issue_1884', $$
1150+
MATCH (n:TestC2)
1151+
RETURN n.obj.node
1152+
$$) AS (node agtype);
1153+
node
1154+
---------------------------------------------------------------------------------------
1155+
{"id": 3940649673949185, "label": "TestC2", "properties": {"tag": "maptest"}}::vertex
1156+
(1 row)
1157+
1158+
-- ============================================================================
1159+
-- Test Group D: MERGE and CREATE with self-reference
1160+
-- ============================================================================
1161+
-- Test D1: MERGE with SET self-reference
1162+
SELECT * FROM cypher('issue_1884', $$
1163+
MERGE (n:TestD1 {name: 'merged'})
1164+
SET n.ref = n
1165+
RETURN n
1166+
$$) AS (result agtype);
1167+
result
1168+
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1169+
{"id": 4222124650659841, "label": "TestD1", "properties": {"ref": {"id": 4222124650659841, "label": "TestD1", "properties": {"name": "merged"}}::vertex, "name": "merged"}}::vertex
1170+
(1 row)
1171+
1172+
-- Test D2: CREATE with SET self-reference
1173+
SELECT * FROM cypher('issue_1884', $$
1174+
CREATE (n:TestD2 {name: 'created'})
1175+
SET n.ref = n
1176+
RETURN n
1177+
$$) AS (result agtype);
1178+
result
1179+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1180+
{"id": 4503599627370497, "label": "TestD2", "properties": {"ref": {"id": 4503599627370497, "label": "TestD2", "properties": {"name": "created"}}::vertex, "name": "created"}}::vertex
1181+
(1 row)
1182+
1183+
-- ============================================================================
1184+
-- Test Group E: Functions with variable references
1185+
-- ============================================================================
1186+
-- Test E1: id() and label() functions
1187+
SELECT * FROM cypher('issue_1884', $$
1188+
CREATE (n:TestE1 {name: 'functest'})
1189+
SET n.myId = id(n), n.myLabel = label(n)
1190+
RETURN n
1191+
$$) AS (result agtype);
1192+
result
1193+
----------------------------------------------------------------------------------------------------------------------------------------
1194+
{"id": 4785074604081153, "label": "TestE1", "properties": {"myId": 4785074604081153, "name": "functest", "myLabel": "TestE1"}}::vertex
1195+
(1 row)
1196+
1197+
-- Test E2: nodes() and relationships() with path
1198+
SELECT * FROM cypher('issue_1884', $$
1199+
CREATE (a:TestE2)-[e:E2REL]->(b:TestE2)
1200+
WITH a, e, b
1201+
MATCH p = (a)-[e]->(b)
1202+
SET a.pathNodes = nodes(p), a.pathRels = relationships(p)
1203+
RETURN a
1204+
$$) AS (a agtype);
1205+
a
1206+
---
1207+
(0 rows)
1208+
9911209
--
9921210
-- Clean up
9931211
--
@@ -1038,6 +1256,33 @@ NOTICE: graph "issue_1634" has been dropped
10381256

10391257
(1 row)
10401258

1259+
SELECT drop_graph('issue_1884', true);
1260+
NOTICE: drop cascades to 19 other objects
1261+
DETAIL: drop cascades to table issue_1884._ag_label_vertex
1262+
drop cascades to table issue_1884._ag_label_edge
1263+
drop cascades to table issue_1884."TestA1"
1264+
drop cascades to table issue_1884."TestA2"
1265+
drop cascades to table issue_1884."LINK"
1266+
drop cascades to table issue_1884."TestA3"
1267+
drop cascades to table issue_1884."REL"
1268+
drop cascades to table issue_1884."TestB1"
1269+
drop cascades to table issue_1884."TestB2"
1270+
drop cascades to table issue_1884."B2REL"
1271+
drop cascades to table issue_1884."TestB3"
1272+
drop cascades to table issue_1884."B3REL"
1273+
drop cascades to table issue_1884."TestC1"
1274+
drop cascades to table issue_1884."TestC2"
1275+
drop cascades to table issue_1884."TestD1"
1276+
drop cascades to table issue_1884."TestD2"
1277+
drop cascades to table issue_1884."TestE1"
1278+
drop cascades to table issue_1884."TestE2"
1279+
drop cascades to table issue_1884."E2REL"
1280+
NOTICE: graph "issue_1884" has been dropped
1281+
drop_graph
1282+
------------
1283+
1284+
(1 row)
1285+
10411286
--
10421287
-- End
10431288
--

0 commit comments

Comments
 (0)