kuzudb / kuzu

Embeddable property graph database management system built for query speed and scalability. Implements Cypher.
https://kuzudb.com/
MIT License
1.38k stars 97 forks source link

Pass parameters to `connection.execute` as dict instead of list of tuples #1959

Closed prrao87 closed 1 year ago

prrao87 commented 1 year ago

Observation

The current approach of passing parameters to a Cypher query in Kùzu via Python is a bit tedious, as the user has to type out a list of tuples that makes the code a bit ugly, and on many counts, is not "pythonic".

The Neo4j Python client allows the specification of parameters as dict[str, Any], which is much easier to use and type for Python users. In recent versions of Python, we also have TypedDict, which we can use to more carefully check types on parameters prior to passing them to the query.

Proposed solution

Instead of specifying that the user pass list[tuple[str, Any]], the docstring could instead say to pass dict[str, Any], in a similar way to Neo4j's Python client.

To avoid breaking anything for the C++ bindings, the dict could easily be converted to list[tuple[str, Any]] within the code (invisible to the user) as follows:

>>> params = {"age": 42}
>>> list(params.items())
[('age', 42)]

This effortless transformation could be added at the start of the execute statement src_py/connection.py, and the docstring could be updated to inform the user to passs params as dict[str, Any] accordingly.

I don't think this would break any existing tests, not require any changes to C++ query preparation code, and also maintain a more similar API to Neo4j's client. Hope this makes sense!

mewim commented 1 year ago

Hi @prrao87,

Thanks for your suggestion. I agree that passing args as dict is more user-friendly than list of tuples and this is how we implmented our Node.js API. We will change our Python API to also use dict soon.

mewim commented 1 year ago

Resolved in #1974