Skip to content

MERGE incorrectly creates multiple vertices #1691

Closed

Description

@mingfang
SELECT * FROM cypher('playground', $$
unwind ["foo", "foo"] as each
merge (v:TEST {name: each})
return v
$$) AS (v agtype)

returns

[
  {
    "v": {
      "id": 3659174697238531,
      "label": "TEST",
      "properties": {
        "name": "foo"
      }
    }
  },
  {
    "v": {
      "id": 3659174697238532,
      "label": "TEST",
      "properties": {
        "name": "foo"
      }
    }
  }
]

creates two vertices when it should only create one. (confirmed with neo4j)

Activity

  1. added
    bug
    on Mar 16, 2024
  2. mingfang commented on Mar 16, 2024

    @mingfang
    Author

    maybe same as #1446

  3. added
    question
    and removed
    bug
    on Mar 18, 2024
  4. jrgemignani commented on Mar 18, 2024

    @jrgemignani
    Contributor

    @mingfang This is expected behavior. The key name is a property of a vertex, in this case. Vertices and edges are allowed to have the same properties.

  5. mingfang commented on Mar 18, 2024

    @mingfang
    Author

    @jrgemignani please note in my test case the problem is that MERGE created two vertices with the same name, and not one vertex and one edge.

  6. jrgemignani commented on Mar 18, 2024

    @jrgemignani
    Contributor

    @mingfang Yes. I was just stating that this is also the case for edges. Vertices can have duplicate properties and edges can have duplicate properties. Properties are not unique.

  7. mingfang commented on Mar 18, 2024

    @mingfang
    Author

    I'm sorry but I don't think that's correct.
    I've tested this in neo4j and it only results in one vertex created, not two like in AGE.
    Here is the neo4j doc that speaks directly to this
    https://neo4j.com/docs/cypher-manual/current/clauses/merge/#merge-merge-single-node-specifying-both-label-and-property
    Merging a single node with both label and property matching an existing node will not create a new node:

  8. jrgemignani commented on Mar 18, 2024

    @jrgemignani
    Contributor

    @mingfang I stand corrected. It seems odd, though, that properties would be unique in this sense. We will likely need to adjust the contains versus equals operator for this case.

  9. mingfang commented on Mar 18, 2024

    @mingfang
    Author

    Also this is only a problem when multiple MERGE are called in the same query.
    e.g. this works as expected, only one vertex is created after both queries.

    SELECT * FROM cypher('playground', $$
    unwind ["foo"] as each
    merge (v:TEST {name: each})
    return v
    $$) AS (v agtype)
    
    SELECT * FROM cypher('playground', $$
    unwind ["foo"] as each
    merge (v:TEST {name: each})
    return v
    $$) AS (v agtype)
    

    I'm guessing that multiple MERGE in the same query somehow is not seeing each statement's "state" since the transaction is not committed yet. Totally a wild guess.

  10. jrgemignani commented on Mar 18, 2024

    @jrgemignani
    Contributor

    Yeah, the more that I look at it, the more it appears to fall there. Transaction bugs, though, can be a pain to fix. Especially, for merge.

  11. added
    bug
    and removed
    question
    on Mar 18, 2024
  12. jrgemignani commented on Mar 20, 2024

    @jrgemignani
    Contributor

    @mingfang This is an interesting one.

  13. jrgemignani commented on Mar 21, 2024

    @jrgemignani
    Contributor

    @mingfang The particular cause for this issue is hard to easily describe, especially without getting very technical. Suffice to say, that MERGE (within the execution of a MERGE command instance) can't (or doesn't) see what it has just created.

    In most cases, this isn't an issue because MERGE only CREATEs what isn't already there. However, when it is driven externally, it is possible for it to have duplicates for duplicated data. I'm not sure if this is the only case where this is possible, though.

    I am looking into how to allow MERGE to know prior to inserting a duplicate and if this can resolve the issue. This may take some time, though, to implement.

  14. mingfang commented on Mar 21, 2024

    @mingfang
    Author

    @jrgemignani
    Thanks for the update.
    I appreciate all your hard work.
    Thanks!

  15. pinned this issue on Mar 25, 2024
  16. jrgemignani commented on Mar 30, 2024

    @jrgemignani
    Contributor

    @mingfang Update: I currently have a fix for this and am reviewing it, creating test cases, and cleaning up the code. If all goes well, a PR should be available Monday or Tuesday.

    Sorry for the long delay, but this particular issue required a bit of a rewrite of MERGE.

  17. jrgemignani commented on Apr 1, 2024

    @jrgemignani
    Contributor

    @mingfang PR #1718 should address this issue once it is reviewed and merged.

  18. mingfang commented on Apr 1, 2024

    @mingfang
    Author

    @jrgemignani Amazing. Thanks again!

  19. jrgemignani commented on Apr 2, 2024

    @jrgemignani
    Contributor

    @mingfang This should be fixed in the master branch now. If you could verify it, and close the ticket provided it resolves the issue, that would be great.

  20. mingfang commented on Apr 3, 2024

    @mingfang
    Author

    @jrgemignani
    confirm fixed. Thank you!

  21. unpinned this issue on Apr 8, 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions