You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@age.apache.org by GitBox <gi...@apache.org> on 2021/03/17 01:17:41 UTC
[GitHub] [incubator-age] ehsanonline opened a new issue #45: unique properties / indexing
ehsanonline opened a new issue #45:
URL: https://github.com/apache/incubator-age/issues/45
Hello, it would be great if we could use unique keys in nodes and edges. in Neo4j it's called [Node Key](https://neo4j.com/docs/cypher-manual/current/administration/constraints/#administration-constraints-node-key) and it can be used like these:
1. `CREATE CONSTRAINT constraint_name ON (n:Person) ASSERT (n.firstname) IS NODE KEY`
2. `CREATE CONSTRAINT constraint_name ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY`
3. `CREATE CONSTRAINT constraint_name IF NOT EXISTS ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY`
4. `CREATE CONSTRAINT constraint_with_provider ON (n:Label) ASSERT (n.prop1) IS NODE KEY OPTIONS {indexProvider: 'native-btree-1.0'}`
5. `DROP CONSTRAINT constraint_name`
6. `DROP CONSTRAINT missing_constraint_name IF EXISTS`
I've tried to use Postgres queries but since the `properties` column type is not json/jsonb I got error:
```sql
postgres=# CREATE UNIQUE INDEX person_name_idx ON mygraph.person( (properties->>'name') ) ;
ERROR: operator does not exist: ag_catalog.agtype ->> unknown
LINE 1: ...INDEX person_name_idx ON mygraph.person( (properties->>'name')...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-812524846
It would indeed be very useful to enable the creation of constraints / indexes. This could help to accelerate the creation of edges between nodes that were added earlier, which slows down a lot when there are a lot of nodes with the same label:
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA {id: 1}), (r:LabelB {id: 2})
CREATE (d)-[:Relation {prop: 'value'}]->(r)
$$) as (a agtype)
```
The `CREATE UNIQUE`, `CREATE CONSTRAINT`, and `CREATE INDEX` clauses / commands [are part of cypher, but not of openCypher](https://github.com/opencypher/openCypher/blob/master/docs/standardisation-scope.adoc#language-features-excluded-from-opencypher), so I don't know what the best approach would be here.
@ehsanonline
You can create a unique index, which will prevent the creation of duplicate values, but will not speed up matching, by creating an immutable function that casts to json over text
```
CREATE OR REPLACE FUNCTION get_name(properties agtype)
RETURNS text
AS
$BODY$
select $1::text::json->>'name';
$BODY$
LANGUAGE sql
IMMUTABLE;
```
and using that immutable function in the create index command
```
CREATE UNIQUE INDEX person_name_idx ON mygraph.person(get_name(properties)) ;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter edited a comment on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter edited a comment on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-812524846
It would indeed be very useful to enable the creation of constraints / indexes. This could help to accelerate the creation of edges between nodes that were added earlier, which slows down a lot when there are a lot of nodes with the same label:
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA {id: 1}), (r:LabelB {id: 2})
CREATE (d)-[:Relation {prop: 'value'}]->(r)
$$) as (a agtype)
```
The `CREATE UNIQUE`, `CREATE CONSTRAINT`, and `CREATE INDEX` clauses / commands [are part of cypher, but not of openCypher](https://github.com/opencypher/openCypher/blob/master/docs/standardisation-scope.adoc#language-features-excluded-from-opencypher), so I don't know what the best approach would be here.
@ehsanonline
<details>
<summary>See comment below for an update. Click here to view the original comment content.</summary>
You can create a unique index, which will prevent the creation of duplicate values, but will not speed up matching, by creating an immutable function that casts to json over text
```
CREATE OR REPLACE FUNCTION get_name(properties agtype)
RETURNS text
AS
$BODY$
select $1::text::json->>'name';
$BODY$
LANGUAGE sql
IMMUTABLE;
```
and using that immutable function in the create index command
```
CREATE UNIQUE INDEX person_name_idx ON mygraph.person(get_name(properties)) ;
```
</details>
[Link to the mentioned comment below](https://github.com/apache/incubator-age/issues/45#issuecomment-982438807)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-902711546
I found a (1000 times faster) workaround for the creation of edges between vertices that were added earlier. The workaround consists of inserting the data directly in the underlying tables that are used by Apache AGE.
First, the underlying ids of the vertices are retrieved:
```
SELECT * FROM cyper('graph_name',$$
MATCH (n:LabelA)
return id(n), n.id
$$) as (id agtype, prop agtype);
```
A single edge is then created using the cypher function to make sure the underlying tables are created correctly (the id values are made up)
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA), (r:LabelB)
WHERE id(d) = 11111 and id(r) = 11112
CREATE (d)-[:RelationA {prop: 'value'}]->(r)
$$) as (a agtype);
```
Indices on start_id and end_id are then created in the underlying table for the edges of a certain type, to speed up the inserts in the `_ag_label_edge` table later on:
```
CREATE INDEX RelationA__start_id ON graph_name.RelationA(start_id);
CREATE INDEX RelationA__end_id ON graph_name.RelationA(end_id);
```
All other edges are created by direct insertion, first in the specific edge table (using [executemany](https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection.executemany)):
```
INSERT INTO graph_name.RelationA (start_id, end_id, properties)
VALUES ($1, $2, $3)
```
Where `$1` are the domain_ids, `$2` are the range_ids and `$3` are the properties (as json dump).
Secondly, the edges are also directly inserted into the `_ag_label_edge` table (also using executemany). In my use case, each edge has an `id` property that can be used to select the correct edge when there are multiple edges between two vertices:
```
INSERT INTO graph_name._ag_label_edge (id, start_id, end_id, properties)
VALUES (
(
SELECT id from graph_name.RelationA
WHERE start_id = $1
AND end_id = $2
AND properties::text::json->>'id' = $3
),
$1,
$2,
$4
)
```
Where `$1` are the domain_ids, `$2` are the range_ids, `$3` are the relation ids and `$4` are the properties (as json dump).
Any thoughts on this workaround?
Are there any plans to add property indexes to Apache AGE later on?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] JoshInnis commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
JoshInnis commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-982285644
Hello,
A schema is a graph name and a label is a table name. Id and properties field are columns in vertices and id, start_id, end_id, and properties are columns in the edges. Use the agtype_access_operator(properties, key) to get to get a property value.
Know all that you can use Postges' standard DDL language to implement constraints and unique values.
```
ALTER TABLE graph_name.label_name
ADD CONSTRAINT constraint_name
CHECK(agtype_access_operator(properties, "name_of_property") != '"Check against here"'::agtype);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-908222122
When the edges are inserted in the specific edge table, they are automatically added to the `_ag_label_edge` table, reducing the workaround to the following and making the relation creation even faster:
First, the underlying ids of the vertices are retrieved:
```
SELECT * FROM cyper('graph_name',$$
MATCH (n:LabelA)
return id(n), n.id
$$) as (id agtype, prop agtype);
```
A single edge is then created using the cypher function to make sure the underlying tables are created correctly (the id values are made up)
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA), (r:LabelB)
WHERE id(d) = 11111 and id(r) = 11112
CREATE (d)-[:RelationA {prop: 'value'}]->(r)
$$) as (a agtype);
```
All other edges are created by direct insertion in the specific edge table (using [executemany](https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection.executemany)):
```
INSERT INTO graph_name.RelationA (start_id, end_id, properties)
VALUES ($1, $2, $3)
```
Where `$1` are the domain_ids, `$2` are the range_ids and `$3` are the properties (as json dump).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-845153669
I tried using id() instead of using properties for matching, but this was even slower (I noticed using an additional `WHERE` clause instead of defining properties in the `MATCH` clause is slower in another use case as well).
For this, I rewrote
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA {id: 1}), (r:LabelB {id: 2})
CREATE (d)-[:Relation {prop: 'value'}]->(r)
$$) as (a agtype);
```
to
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA), (r:LabelB)
WHERE id(d) = 11111 and id(r) = 11112
CREATE (d)-[:Relation {prop: 'value'}]->(r)
$$) as (a agtype);
```
where I obtained the ids (the ones above are made up) using
```
SELECT * FROM cyper('graph_name',$$
MATCH (n:LabelA)
return id(n), n.id
$$) as (id agtype, prop agtype);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-982438807
This is great, thank you!
Since indices require an immutable function, an additional function will still need to be created for them. When I create a `get_id` function with
```
CREATE OR REPLACE FUNCTION get_id(properties agtype)
RETURNS agtype
AS
$BODY$
select agtype_access_operator($1, '"id"');
$BODY$
LANGUAGE sql
IMMUTABLE;
```
and use it in an index with
```
CREATE UNIQUE INDEX person_id_idx ON mygraph.person(get_id(properties)) ;
```
the creation of vertices with the same id will be prevented
```
ERROR: duplicate key value violates unique constraint "person_id_idx"
DETAIL: Key (get_id(properties))=(2250) already exists.
```
but the index will still not be used when trying to match vertices with a specific id:
```
SELECT * FROM ag_catalog.cypher('mygraph', $$EXPLAIN ANALYZE MATCH (a:person {id:2250}) return a$$) as (a agtype);
```
```
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
Seq Scan on person a (cost=0.00..2775.32 rows=16906 width=32) (actual time=12.688..85.521 rows=1 loops=1)
Filter: _property_constraint_check(properties, agtype_build_map('id'::text, '2250'::agtype))
Rows Removed by Filter: 50718
Planning Time: 0.122 ms
Execution Time: 85.550 ms
(5 rows)
```
Is there a way to use indices when matching?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] JoshInnis edited a comment on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
JoshInnis edited a comment on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-982285644
Hello,
A graph name is a schema and a label name is a table. Id and properties are columns in vertex table. Id, start_id, end_id, and properties are columns in the edge tables. Use the agtype_access_operator(properties, key) to get to get a property value.
Knowing all that you can use Postges' standard DDL language to implement constraints, indices and unique values.
```
ALTER TABLE graph_name.label_name
ADD CONSTRAINT constraint_name
CHECK(agtype_access_operator(properties, "name_of_property") != '"Check against here"'::agtype);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] JoshInnis commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
JoshInnis commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-983105857
Indices cannot currently be used while matching. There will need to be some re factoring done to allow the planner to realize opportunities where the indices can be used.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] pdpotter edited a comment on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
pdpotter edited a comment on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-902711546
I found a (1000 times faster) workaround for the creation of edges between vertices that were added earlier. The workaround consists of inserting the data directly in the underlying tables that are used by Apache AGE.
<details>
<summary>See comment below for an update. Click here to view the original comment content.</summary>
First, the underlying ids of the vertices are retrieved:
```
SELECT * FROM cyper('graph_name',$$
MATCH (n:LabelA)
return id(n), n.id
$$) as (id agtype, prop agtype);
```
A single edge is then created using the cypher function to make sure the underlying tables are created correctly (the id values are made up)
```
SELECT * FROM cypher('graph_name', $$
MATCH (d:LabelA), (r:LabelB)
WHERE id(d) = 11111 and id(r) = 11112
CREATE (d)-[:RelationA {prop: 'value'}]->(r)
$$) as (a agtype);
```
Indices on start_id and end_id are then created in the underlying table for the edges of a certain type, to speed up the inserts in the `_ag_label_edge` table later on:
```
CREATE INDEX RelationA__start_id ON graph_name.RelationA(start_id);
CREATE INDEX RelationA__end_id ON graph_name.RelationA(end_id);
```
All other edges are created by direct insertion, first in the specific edge table (using [executemany](https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.connection.Connection.executemany)):
```
INSERT INTO graph_name.RelationA (start_id, end_id, properties)
VALUES ($1, $2, $3)
```
Where `$1` are the domain_ids, `$2` are the range_ids and `$3` are the properties (as json dump).
Secondly, the edges are also directly inserted into the `_ag_label_edge` table (also using executemany). In my use case, each edge has an `id` property that can be used to select the correct edge when there are multiple edges between two vertices:
```
INSERT INTO graph_name._ag_label_edge (id, start_id, end_id, properties)
VALUES (
(
SELECT id from graph_name.RelationA
WHERE start_id = $1
AND end_id = $2
AND properties::text::json->>'id' = $3
),
$1,
$2,
$4
)
```
Where `$1` are the domain_ids, `$2` are the range_ids, `$3` are the relation ids and `$4` are the properties (as json dump).
</details>
Any thoughts on this workaround?
Are there any plans to add property indexes to Apache AGE later on?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] audiBookning commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
audiBookning commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-812604412
Here some links just for reference.
List of [Cypher Improvement Proposals](https://www.opencypher.org/cips) (CIP)
As for indexes and constraints in Opencypher, there are already some PR for their addition, but i think that they are "blocked" by another CIP
CIP:
- [CIP2016-12-16 Neo4j Indexes](https://github.com/Mats-SX/openCypher/blob/c542f94f07fb934254748f4f2ce44612d45866a3/cip/vendor-extensions/neo4j/CIP2016-12-14-Neo4j-indexes.adoc)
- [CIP2016-12-16 Constraints syntax](https://github.com/Mats-SX/openCypher/blob/88c0f11f11410ce2f68dc050e54ff419058c6cad/cip/1.accepted/CIP2016-12-14-Constraint-syntax.adoc)
PR:
- [Add Neo4j index extension CIP](https://github.com/opencypher/openCypher/pull/197)
- [Specifying Cypher constraints](https://github.com/opencypher/openCypher/pull/166)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] JoshInnis commented on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
JoshInnis commented on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-983087777
It might be safe to change `agtype_access_operator` to immutable.
Per Postgres' Documentation:
IMMUTABLE indicates that the function cannot modify the database and always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list. If this option is given, any call of the function with all-constant arguments can be immediately replaced with the function value.
STABLE indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements. This is the appropriate selection for functions whose results depend on database lookups, parameter variables (such as the current time zone), etc. (It is inappropriate for AFTER triggers that wish to query rows modified by the current command.) Also note that the current_timestamp family of functions qualify as stable, since their values do not change within a transaction.
The access operator works for Agtype Lists and Maps, it does not perform any database lookup, it just extracts a value from the first passed in parameter.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-age] JoshInnis edited a comment on issue #45: unique properties / indexing
Posted by GitBox <gi...@apache.org>.
JoshInnis edited a comment on issue #45:
URL: https://github.com/apache/incubator-age/issues/45#issuecomment-982285644
Hello,
A schema is a graph name and a label is a table name. Id and properties field are columns in vertices and id, start_id, end_id, and properties are columns in the edges. Use the agtype_access_operator(properties, key) to get to get a property value.
Knowing all that you can use Postges' standard DDL language to implement constraints and unique values.
```
ALTER TABLE graph_name.label_name
ADD CONSTRAINT constraint_name
CHECK(agtype_access_operator(properties, "name_of_property") != '"Check against here"'::agtype);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@age.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org