GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `MERGE` creates incomplete vertices after the first one · Issue #1709 · apache/age

MERGE creates incomplete vertices after the first one  #1709

New issue
Copy link
New issue
Copy link
Closed
Closed
MERGE creates incomplete vertices after the first one #1709
Copy link
Labels
bugSomething isn't working

Description

@mingfang
mingfang
opened on Mar 20, 2024
Issue body actions

when creating multiple vertices like this

SELECT * FROM cypher('playground', $$
  UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
  MERGE (v:PERSON {first: map.first})
  SET v=map
  RETURN v
$$) AS (v agtype)

returns 2 vertices, but all the vertices after the first one will be incomplete. e.g.

SELECT * FROM cypher('playground', $$
  MATCH (v:PERSON)                                                           
  RETURN v
$$) AS (v agtype)

returns

{"id": 3940649673949198, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
{"id": 3940649673949199, "label": "PERSON", "properties": {"first": "ned"}}::vertex

Note the first vertex is correct but the second vertex is missing the "last" field. Very strange.

Activity

  1. mingfang
    added
    bugSomething isn't working
    on Mar 20, 2024
  2. mingfang
    changed the title [-]`MERGE` creates incomplete vertices if the `RETURN` clause does not include the created vertices[/-] [+]`MERGE` creates incomplete vertices after the first one [/+] on Mar 20, 2024
  3. anfa-majid commented on Mar 25, 2024

    @anfa-majid
    anfa-majid
    on Mar 25, 2024
    More actions

    The issue you're encountering with MERGE creating incomplete vertices stems from the way the MERGE and SET commands are being applied. In your query, MERGE is used with a condition that solely relies on the first property to identify unique vertices. However, this approach can lead to unexpected results when the first property alone does not uniquely identify a vertex, as MERGE will match existing vertices based on the first property and might not correctly associate the last property with subsequent vertices.

    To ensure each vertex is accurately created or matched with its complete set of properties (first and last), you need to adjust the MERGE condition to include all properties that together uniquely define a vertex. The combination of first and last names uniquely identifies a PERSON vertex, therefore both should be included in the MERGE condition to prevent the creation of incomplete vertices.

  4. mingfang commented on Mar 25, 2024

    @mingfang
    mingfang
    on Mar 25, 2024
    Author
    More actions

    @anfa-majid
    In my specific test case above, first does uniquely identify the vertex.
    I think the problem is much deeper(and more serious) than what you described.

    Reacted by John Gemignani
  5. Akaza20-03 commented on Mar 26, 2024

    @Akaza20-03
    Akaza20-03
    on Mar 26, 2024
    More actions

    I think we can fix this issue by explicitly assign each property indivisually in merge operation

    //previous line
    SET v=map

    //updated line
    SET v.first=map.first , v.last=map.last

    In this updated query, after the MERGE operation, we set each property first and last separately from the map variable, which should ensure that all vertices have both "first" and "last" fields correctly set.

  6. mingfang commented on Mar 26, 2024

    @mingfang
    mingfang
    on Mar 26, 2024
    Author
    More actions

    @Akaza20-03
    No that does not work either.
    Please try it for yourself.

  7. jrgemignani commented on Apr 2, 2024

    @jrgemignani
    jrgemignani
    on Apr 2, 2024
    Contributor
    More actions

    @mingfang It looks like maybe my PR #1718 might have fixed this one too?! This is in the master branch, btw.

    psql-16.1-5432-pgsql=# SELECT create_graph('playground');
    NOTICE:  graph "playground" has been created
     create_graph
    --------------
    
    (1 row)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
    psql-16.1-5432-pgsql$#   UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
    psql-16.1-5432-pgsql$#   MERGE (v:PERSON {first: map.first})
    psql-16.1-5432-pgsql$#   SET v=map
    psql-16.1-5432-pgsql$#   RETURN v
    psql-16.1-5432-pgsql$# $$) AS (v agtype);
                                                      v
    -----------------------------------------------------------------------------------------------------
     {"id": 844424930131969, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930131970, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
    (2 rows)
    
    psql-16.1-5432-pgsql=#
    
  8. jrgemignani commented on Apr 2, 2024

    @jrgemignani
    jrgemignani
    on Apr 2, 2024
    Contributor
    More actions

    @mingfang Oops, nvm, sigh,...

    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
    psql-16.1-5432-pgsql$#   MATCH (v:PERSON)
    psql-16.1-5432-pgsql$#   RETURN v
    psql-16.1-5432-pgsql$# $$) AS (v agtype);
                                                     v
    ----------------------------------------------------------------------------------------------------
     {"id": 844424930131969, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930131970, "label": "PERSON", "properties": {"first": "ned"}}::vertex
    (2 rows)
    
    psql-16.1-5432-pgsql=#
    
  9. jrgemignani commented on Apr 3, 2024

    @jrgemignani
    jrgemignani
    on Apr 3, 2024
    Contributor
    More actions

    @mingfang I will look into this one. It looks like it is creating the correct return tuple, just not inserting the correct tuple. I sometimes think that I should just rewrite merge altogether.

    Reacted by Ming Fang
  10. jrgemignani commented on Apr 3, 2024

    @jrgemignani
    jrgemignani
    on Apr 3, 2024
    Contributor
    More actions

    @mingfang Question - Just curious, why are you doing it this particular way? If you use merge as follows, it works -

    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
      UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
      MERGE (v:PERSON {first: map.first, last: map.last}) RETURN v $$) AS (v agtype);
                                                      v
    -----------------------------------------------------------------------------------------------------
     {"id": 844424930131971, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930131972, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
    (2 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
      MATCH (v:PERSON)
      RETURN v
    $$) AS (v agtype);
                                                      v
    -----------------------------------------------------------------------------------------------------
     {"id": 844424930131971, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930131972, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
    (2 rows)
    
    psql-16.1-5432-pgsql=#
    

    I'm thinking this might be an issue with SET, and not MERGE.

  11. mingfang commented on Apr 3, 2024

    @mingfang
    mingfang
    on Apr 3, 2024
    Author
    More actions

    My real use case is to create a graph from a SQL query and I don't know ahead of time what fields are returned.
    This is my function to run SQL and return Agtype

    -- Apache AGE load_sql
    CREATE OR REPLACE FUNCTION public.load_sql(query agtype) RETURNS SETOF agtype AS $$
    BEGIN
        RETURN QUERY EXECUTE FORMAT($sql$
            WITH query AS (%s) 
            SELECT agtype_build_list(query) 
            FROM query
        $sql$, query::text);
    END
    $$ LANGUAGE plpgsql VOLATILE;

    Then in cypher is would call that function and use MERGE to create graph.

    SELECT * FROM cypher('movies', $$
        UNWIND public.load_sql('
            SELECT * FROM movies
        ') AS row
        MERGE (v:MOVIE {title: row.title})
        SET v=row
        RETURN count(v)
    $$) AS (v agtype);
    Reacted by John Gemignani
  12. jrgemignani commented on Apr 3, 2024

    @jrgemignani
    jrgemignani
    on Apr 3, 2024
    Contributor
    More actions

    @mingfang I traced the code and found it skipping the insert for the latter tuple. This looks to be a transaction/command id issue.

    Reacted by Ming Fang
  13. diangamichael commented on Apr 3, 2024

    @diangamichael
    diangamichael
    on Apr 3, 2024
    More actions

    The issue you're encountering is because of the way you're merging and setting properties in your Cypher query. When you use MERGE, it will only create a new node if it doesn't already exist with the specified properties. However, in your case, you're using SET after MERGE, which can overwrite existing properties on the node.

    To fix this issue and ensure that properties are correctly set for all nodes, you should use ON CREATE SET in addition to SET. This will only set properties if the node is newly created during the MERGE operation.

    Here's the corrected query:

    SELECT * FROM cypher('playground', $$
    UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
    MERGE (v:PERSON {first: map.first})
    ON CREATE SET v=map
    SET v.last = map.last
    RETURN v
    $$) AS (v agtype);
    With this modification, each node will have its properties correctly set, even if they already exist in the database.

  14. jrgemignani commented on Apr 3, 2024

    @jrgemignani
    jrgemignani
    on Apr 3, 2024
    Contributor
    More actions

    @diangamichael Could you, in addition to your response, include an example using Apache AGE showing this solves the issue?

  15. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @mingfang Yeah, it appears to be an issue with transaction/command ids. It likely has to do with the handoff between MERGE and SET as both are updates.

    Reacted by Ming Fang
  16. sir-sa commented on Apr 4, 2024

    @sir-sa
    sir-sa
    on Apr 4, 2024
    More actions

    The issue you're encountering is due to the behavior of the SET clause in your Cypher query. When you use SET v=map, you're overwriting all properties of the node v with the properties from the map. If the map doesn't contain a certain property (like "last" in the case of the second iteration), it will effectively remove that property from the node.

    To fix this issue, you can modify your query to conditionally set properties only if they exist in the map. Here's how you can do it:

    SELECT * FROM cypher('playground', $$
    UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}] AS map
    MERGE (v:PERSON {first: map.first})
    SET v.first = map.first
    SET v.last = CASE WHEN map.last IS NOT NULL THEN map.last ELSE v.last END
    RETURN v
    $$) AS (v agtype);

  17. jtomek commented on Apr 4, 2024

    @jtomek
    jtomek
    on Apr 4, 2024
    More actions

    I second the idea of implementing ON CREATE and ON MATCH.
    This would significantly help with business logic needed to prevent duplicates.

    A few neo4j examples as food for thought:

    MERGE (keanu:Person {name: 'Keanu Reeves'})
    ON CREATE
      SET keanu.created = timestamp()
    ON MATCH
      SET keanu.lastSeen = timestamp()
    RETURN keanu.name, keanu.created, keanu.lastSeen
    MERGE (person:Person)
    ON MATCH
      SET
        person.found = true,
        person.lastAccessed = timestamp()
    RETURN person.name, person.found, person.lastAccessed
  18. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @sir-sa Respectfully, that is not how SET works internally nor is it the cause of this issue. What is interesting, though, is that your query works. But, it also works without the conditional part, highlighting what I've stated and pointing again to transaction/command ids.

  19. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @jtomek ON CREATE or ON MATCH may be good to add to Apache AGE. You should consider opening a separate discussion thread about them to see what others might think.

    Unfortunately, for this particular issue, this is just a bug that needs to be fixed.

  20. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @mingfang From the example above I have a possible workaround for you while I figure out what the issue is with the ids -

    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$ MATCH (u) DELETE u $$) AS (u agtype);
     u
    ---
    (0 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$ MATCH (u) RETURN u $$) AS (u agtype);
     u
    ---
    (0 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
    UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}, {first: 'jane', last: 'doe'}] AS map
    MERGE (v:PERSON {first: map.first})
    SET v=map
    SET v=map
    RETURN v
    $$) AS (v agtype);
                                                      v
    -----------------------------------------------------------------------------------------------------
     {"id": 844424930132012, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930132013, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
     {"id": 844424930132014, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
    (3 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$ MATCH (u) RETURN u $$) AS (u agtype);
                                                      u
    -----------------------------------------------------------------------------------------------------
     {"id": 844424930132012, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930132013, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
     {"id": 844424930132014, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
    (3 rows)
    
    psql-16.1-5432-pgsql=#
    
    Reacted by Marco Aurélio Silva de Souza Júnior
  21. mingfang commented on Apr 4, 2024

    @mingfang
    mingfang
    on Apr 4, 2024
    Author
    More actions

    @jrgemignani
    omg your workaround does work!
    With this workaround and your other recent fixes, I'm now unblocked.
    You da man!

    Reacted by John Gemignani
    Reacted by Marco Aurélio Silva de Souza Júnior
  22. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @mingfang Don't get too carried away ;) it is just a temporary workaround. We shouldn't need to run SET twice to get it to work.

    Reacted by Ming Fang
  23. jrgemignani commented on Apr 4, 2024

    @jrgemignani
    jrgemignani
    on Apr 4, 2024
    Contributor
    More actions

    @mingfang I found the issue and corrected it. Although, I need to review that it is correct and there aren't other areas that may have a similar issue.

    Here is a sample output that also highlights something interesting. I'm curious of your thoughts -

    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$ MATCH (v) RETURN v $$) AS (v agtype);
     v
    ---
    (0 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$
    UNWIND [{first: 'jon', last: 'snow'}, {first: 'ned', last: 'stark'}, {first: 'jane', last: 'doe'}, {first: 'ned', last: 'flanders'}] AS map
    MERGE (v:PERSON {first: map.first}) SET v=map RETURN v $$) AS (v agtype);
                                                       v
    --------------------------------------------------------------------------------------------------------
     {"id": 844424930132054, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930132055, "label": "PERSON", "properties": {"last": "stark", "first": "ned"}}::vertex
     {"id": 844424930132056, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
     {"id": 844424930132055, "label": "PERSON", "properties": {"last": "flanders", "first": "ned"}}::vertex
    (4 rows)
    
    psql-16.1-5432-pgsql=# SELECT * FROM cypher('playground', $$ MATCH (v) RETURN v $$) AS (v agtype);
                                                       v
    --------------------------------------------------------------------------------------------------------
     {"id": 844424930132054, "label": "PERSON", "properties": {"last": "snow", "first": "jon"}}::vertex
     {"id": 844424930132056, "label": "PERSON", "properties": {"last": "doe", "first": "jane"}}::vertex
     {"id": 844424930132055, "label": "PERSON", "properties": {"last": "flanders", "first": "ned"}}::vertex
    (3 rows)
    
    psql-16.1-5432-pgsql=# 
    

    Note that the correct data is stored in the end.

    Reacted by Ming Fang
  24. jrgemignani commented on Apr 5, 2024

    @jrgemignani
    jrgemignani
    on Apr 5, 2024
    Contributor
    More actions

    @mingfang PR #1721 should address this issue. It just needs the prerequisite review and merge.

    Reacted by Ming Fang
  25. diangamichael commented on Apr 5, 2024

    @diangamichael
    diangamichael
    on Apr 5, 2024
    More actions

    Let's close this bug since the earlier suggestion works perfectly 🙂🙂🙂

    Reacted by Ruperth Nyagesoa
  26. jrgemignani commented on Apr 5, 2024

    @jrgemignani
    jrgemignani
    on Apr 5, 2024
    Contributor
    More actions

    @diangamichael Thank you for your input. However, this issue is a bug and the suggestion is a temporary workaround. This issue cannot be closed until the issue is resolved and @mingfang, who opened the issue, is satisfied with the solution.

  27. mingfang commented on Apr 5, 2024

    @mingfang
    mingfang
    on Apr 5, 2024
    Author
    More actions

    @jrgemignani
    Confirm fixed(by your PR). Thanks!

    Reacted by John Gemignani
  28. mingfang
    closed this as completedon Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.