Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions regress/expected/cypher_delete.out
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,104 @@ NOTICE: graph "cypher_delete" has been dropped

(1 row)

--
-- DELETE expressions
--
SELECT create_graph('cypher_delete_expr');
NOTICE: graph "cypher_delete_expr" has been created
create_graph
--------------

(1 row)

SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 1})-[:R]->(:Node {id: 2}), (:Node {id: 3})$$) AS (a agtype);
a
---
(0 rows)

-- non-DETACH DELETE of a list element fails while the vertex has edges
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (a:Node)-[:R]->(b:Node) WITH collect(b) AS bs LIMIT 1 DELETE bs[0] RETURN bs[0] AS first$$) AS (first agtype);
ERROR: Cannot delete a vertex that has edge(s). Delete the edge(s) first, or try DETACH DELETE.
-- non-DETACH DELETE of a list element succeeds for an isolated vertex
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node {id: 3}) WITH collect(b) AS bs LIMIT 1 DELETE bs[0]$$) AS (a agtype);
a
---
(0 rows)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node {id: 3}) RETURN b$$) AS (b agtype);
b
---
(0 rows)

-- DETACH DELETE of a list element deletes the vertex and its edges
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (a:Node)-[:R]->(b:Node) WITH collect(b) AS bs LIMIT 1 DETACH DELETE bs[0] RETURN bs[0] AS first$$) AS (first agtype);
first
---------------------------------------------------------------------------
{"id": 844424930131970, "label": "Node", "properties": {"id": 2}}::vertex
(1 row)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);
r
---
(0 rows)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH (n:Node) RETURN n ORDER BY id(n)$$) AS (n agtype);
n
---------------------------------------------------------------------------
{"id": 844424930131969, "label": "Node", "properties": {"id": 1}}::vertex
(1 row)

-- DELETE of a path element
SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 4})-[:R]->(:Node {id: 5})$$) AS (a agtype);
a
---
(0 rows)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH p=(:Node {id: 4})-[:R]->(:Node {id: 5}) DETACH DELETE nodes(p)[0] RETURN nodes(p)[0] AS first$$) AS (first agtype);
first
---------------------------------------------------------------------------
{"id": 844424930131972, "label": "Node", "properties": {"id": 4}}::vertex
(1 row)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);
r
---
(0 rows)

-- DELETE of an edge list element
SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 6})-[:R]->(:Node {id: 7})$$) AS (a agtype);
a
---
(0 rows)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r:R]->() WITH collect(r) AS rs LIMIT 1 DELETE rs[0] RETURN rs[0] AS edge$$) AS (edge agtype);
edge
------------------------------------------------------------------------------------------------------------------------
{"id": 1125899906842627, "label": "R", "end_id": 844424930131975, "start_id": 844424930131974, "properties": {}}::edge
(1 row)

SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);
r
---
(0 rows)

-- non-entity expressions produce a user-facing error
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node) DELETE 1 RETURN b$$) AS (b agtype);
ERROR: DELETE clause can only delete vertices and edges
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node) WITH collect(b) AS bs LIMIT 1 DELETE bs RETURN bs$$) AS (bs agtype);
ERROR: DELETE clause can only delete vertices and edges
SELECT drop_graph('cypher_delete_expr', true);
NOTICE: drop cascades to 4 other objects
DETAIL: drop cascades to table cypher_delete_expr._ag_label_vertex
drop cascades to table cypher_delete_expr._ag_label_edge
drop cascades to table cypher_delete_expr."Node"
drop cascades to table cypher_delete_expr."R"
NOTICE: graph "cypher_delete_expr" has been dropped
drop_graph
------------

(1 row)

--
-- End
--
42 changes: 42 additions & 0 deletions regress/expected/security.out
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,48 @@ $$) AS (start_name agtype, end_name agtype);
"Alice" | "Bob"
(1 row)

RESET ROLE;
-- ============================================================================
-- PART 13: DELETE Expression Permission Tests
-- ============================================================================
SELECT * FROM cypher('security_test', $$
CREATE (:Person {name: 'ExpressionDelete'})
$$) AS (a agtype);
a
---
(0 rows)

SET ROLE security_test_readonly;
-- Test: DELETE of an expression should fail without DELETE permission
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'})
WITH collect(p) AS ps
DELETE ps[0]
RETURN ps[0]
$$) AS (a agtype);
ERROR: permission denied for table Person
RESET ROLE;
SET ROLE security_test_delete;
-- Test: DELETE of an expression should succeed with DELETE permission
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'})
WITH collect(p) AS ps
DELETE ps[0]
RETURN ps[0]
$$) AS (a agtype);
a
------------------------------------------------------------------------------------------------
{"id": 844424930131975, "label": "Person", "properties": {"name": "ExpressionDelete"}}::vertex
(1 row)

-- Verify deletion
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'}) RETURN p
$$) AS (p agtype);
p
---
(0 rows)

RESET ROLE;
-- ============================================================================
-- Cleanup
Expand Down
34 changes: 34 additions & 0 deletions regress/sql/cypher_delete.sql
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,40 @@ SELECT drop_graph('setdelete', true);
DROP FUNCTION delete_test;
SELECT drop_graph('cypher_delete', true);

--
-- DELETE expressions
--
SELECT create_graph('cypher_delete_expr');
SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 1})-[:R]->(:Node {id: 2}), (:Node {id: 3})$$) AS (a agtype);

-- non-DETACH DELETE of a list element fails while the vertex has edges
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (a:Node)-[:R]->(b:Node) WITH collect(b) AS bs LIMIT 1 DELETE bs[0] RETURN bs[0] AS first$$) AS (first agtype);

-- non-DETACH DELETE of a list element succeeds for an isolated vertex
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node {id: 3}) WITH collect(b) AS bs LIMIT 1 DELETE bs[0]$$) AS (a agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node {id: 3}) RETURN b$$) AS (b agtype);

-- DETACH DELETE of a list element deletes the vertex and its edges
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (a:Node)-[:R]->(b:Node) WITH collect(b) AS bs LIMIT 1 DETACH DELETE bs[0] RETURN bs[0] AS first$$) AS (first agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (n:Node) RETURN n ORDER BY id(n)$$) AS (n agtype);

-- DELETE of a path element
SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 4})-[:R]->(:Node {id: 5})$$) AS (a agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH p=(:Node {id: 4})-[:R]->(:Node {id: 5}) DETACH DELETE nodes(p)[0] RETURN nodes(p)[0] AS first$$) AS (first agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);

-- DELETE of an edge list element
SELECT * FROM cypher('cypher_delete_expr', $$CREATE (:Node {id: 6})-[:R]->(:Node {id: 7})$$) AS (a agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r:R]->() WITH collect(r) AS rs LIMIT 1 DELETE rs[0] RETURN rs[0] AS edge$$) AS (edge agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH ()-[r]->() RETURN r$$) AS (r agtype);

-- non-entity expressions produce a user-facing error
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node) DELETE 1 RETURN b$$) AS (b agtype);
SELECT * FROM cypher('cypher_delete_expr', $$MATCH (b:Node) WITH collect(b) AS bs LIMIT 1 DELETE bs RETURN bs$$) AS (bs agtype);

SELECT drop_graph('cypher_delete_expr', true);

--
-- End
--
36 changes: 36 additions & 0 deletions regress/sql/security.sql
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,42 @@ $$) AS (start_name agtype, end_name agtype);

RESET ROLE;

-- ============================================================================
-- PART 13: DELETE Expression Permission Tests
-- ============================================================================

SELECT * FROM cypher('security_test', $$
CREATE (:Person {name: 'ExpressionDelete'})
$$) AS (a agtype);

SET ROLE security_test_readonly;

-- Test: DELETE of an expression should fail without DELETE permission
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'})
WITH collect(p) AS ps
DELETE ps[0]
RETURN ps[0]
$$) AS (a agtype);

RESET ROLE;
SET ROLE security_test_delete;

-- Test: DELETE of an expression should succeed with DELETE permission
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'})
WITH collect(p) AS ps
DELETE ps[0]
RETURN ps[0]
$$) AS (a agtype);

-- Verify deletion
SELECT * FROM cypher('security_test', $$
MATCH (p:Person {name: 'ExpressionDelete'}) RETURN p
$$) AS (p agtype);

RESET ROLE;

-- ============================================================================
-- Cleanup
-- ============================================================================
Expand Down
32 changes: 28 additions & 4 deletions src/backend/executor/cypher_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ static void process_delete_list(CustomScanState *node);
static void check_for_connected_edges(CustomScanState *node);
static agtype_value *extract_entity(CustomScanState *node,
TupleTableSlot *scanTupleSlot,
int entity_position);
int entity_position,
bool allow_composite);
static void delete_entity(EState *estate, ResultRelInfo *resultRelInfo,
HeapTuple tuple);

Expand Down Expand Up @@ -268,7 +269,8 @@ Node *create_cypher_delete_plan_state(CustomScan *cscan)
*/
static agtype_value *extract_entity(CustomScanState *node,
TupleTableSlot *scanTupleSlot,
int entity_position)
int entity_position,
bool allow_composite)
{
agtype_value *original_entity_value;
agtype *original_entity;
Expand All @@ -282,9 +284,15 @@ static agtype_value *extract_entity(CustomScanState *node,
errmsg("DELETE clause can only delete agtype")));

original_entity = DATUM_GET_AGTYPE_P(scanTupleSlot->tts_values[entity_position - 1]);

if (!allow_composite && !AGT_ROOT_IS_SCALAR(original_entity))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("DELETE clause can only delete vertices and edges")));

original_entity_value = get_ith_agtype_value_from_container(&original_entity->root, 0);

if (original_entity_value->type != AGTV_VERTEX && original_entity_value->type != AGTV_EDGE)
if (original_entity_value == NULL ||
(original_entity_value->type != AGTV_VERTEX && original_entity_value->type != AGTV_EDGE))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("DELETE clause can only delete vertices and edges")));

Expand Down Expand Up @@ -452,7 +460,8 @@ static void process_delete_list(CustomScanState *node)
continue;

original_entity_value = extract_entity(node, scanTupleSlot,
entity_position);
entity_position,
item->is_vle);

id = GET_AGTYPE_VALUE_OBJECT_VALUE(original_entity_value, "id");
label = GET_AGTYPE_VALUE_OBJECT_VALUE(original_entity_value, "label");
Expand All @@ -462,6 +471,21 @@ static void process_delete_list(CustomScanState *node)
rel = resultRelInfo->ri_RelationDesc;
relid = RelationGetRelid(rel);

/*
* Expressions can resolve to any label at runtime, so their DELETE
* privilege cannot be derived from a variable during transformation.
*/
if (item->is_expression)
{
AclResult aclresult;

aclresult = pg_class_aclcheck(relid, GetUserId(), ACL_DELETE);
if (aclresult != ACLCHECK_OK)
{
aclcheck_error(aclresult, OBJECT_TABLE, label_name);
}
}

/*
* Setup the scan key to require the id field on-disc to match the
* entity's graphid.
Expand Down
2 changes: 2 additions & 0 deletions src/backend/nodes/cypher_copyfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ void copy_cypher_delete_item(ExtensibleNode *newnode, const ExtensibleNode *from

COPY_NODE_FIELD(entity_position);
COPY_STRING_FIELD(var_name);
COPY_SCALAR_FIELD(is_expression);
COPY_SCALAR_FIELD(is_vle);
}

/* copy function for cypher_merge_information */
Expand Down
2 changes: 2 additions & 0 deletions src/backend/nodes/cypher_outfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ void out_cypher_delete_item(StringInfo str, const ExtensibleNode *node)

WRITE_NODE_FIELD(entity_position);
WRITE_STRING_FIELD(var_name);
WRITE_BOOL_FIELD(is_expression);
WRITE_BOOL_FIELD(is_vle);
}

/* serialization function for the cypher_merge_information ExtensibleNode. */
Expand Down
2 changes: 2 additions & 0 deletions src/backend/nodes/cypher_readfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ void read_cypher_delete_item(struct ExtensibleNode *node)

READ_NODE_FIELD(entity_position);
READ_STRING_FIELD(var_name);
READ_BOOL_FIELD(is_expression);
READ_BOOL_FIELD(is_vle);
}

/*
Expand Down
Loading