memgraph / gqlalchemy

GQLAlchemy is a library developed with the purpose of assisting in writing and running queries on Memgraph. GQLAlchemy supports high-level connection to Memgraph as well as modular query builder.
https://pypi.org/project/gqlalchemy/
Apache License 2.0
227 stars 32 forks source link

[BUG] If a node attribute value contains an apostrophe, nx_translator().to_cypher_queries(G) breaks because the string values are contained in a single quote instead of double. #318

Open joshtracy3 opened 2 months ago

joshtracy3 commented 2 months ago

Memgraph version Which version did you use? 1.6.0

Environment Some information about the environment you are using Memgraph on: operating system, how do you connect, with or without docker, which driver etc.

Operating System: Darwin OS Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000 OS Release: 23.6.0 OS Architecture: arm64 Full Platform Info: macOS-14.6.1-arm64-arm-64bit

Connecting with docker. Not sure about which driver

Describe the bug A clear and concise description of what the bug is. When a string attribute value on a node contains an apostrophe, the nx_translator().to_cypher_queries(G) will break. It seems to be because string values are enclosed by a single quote instead of double quotes.

To Reproduce Steps to reproduce the behavior:

  1. Run the following python code
    
    import networkx as nx
    from gqlalchemy import Memgraph
    from gqlalchemy.transformations.translators.nx_translator import NxTranslator

from warnings import filterwarnings

filterwarnings("ignore")

memgraph = Memgraph("127.0.0.1", 7687) memgraph.drop_database() graph = nx.Graph() graph.add_nodes_from([(1, {"labels": "First", "name": "Josh's Laptop"}), 2, 3]) graph.add_edges_from([(1, 2, {"type": "EDGE_TYPE"}), (1, 3)])

nx_translator = NxTranslator() for query in nx_translator.to_cypher_queries(graph): memgraph.execute(query)



**Expected behavior** A clear and concise description of what you expected to
happen.

I would expect that the queries should be seemlessly created and executed.
katarinasupe commented 2 months ago

Hi @joshtracy3 thank you for reporting this. It can definitely be handled better internally, but I tried running this and it worked:

graph.add_nodes_from([(1, {"labels": "First", "name": "Josh\\'s Laptop"}), 2, 3])

Let me know if it works for you. Btw. what are you working on?

joshtracy3 commented 2 months ago

@katarinasupe yes this works if you escape the apostrophe's. There also seems to be an issue if the name contains a backslash. I just updated the functions to_cypher_labels and to_cypher_values in nx_translator to double quotes. Seems to work alright. I'm working on an entity linkage problem.

katarinasupe commented 2 months ago

Feel free to open a PR @joshtracy3 😄 Thank you for letting me know what works. I hope Memgraph is performing well for your use case.

If you still haven't, join our Discord server or schedule an office hours call for more questions.