Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] conn.execute(query, params) does not support parameters with list or dict values #3054

Closed
bigluck opened this issue Mar 14, 2024 · 3 comments · Fixed by #3090
Closed
Assignees

Comments

@bigluck
Copy link

bigluck commented Mar 14, 2024

Bug tested on kuzu v0.3.2

I need of programmatically create or update a node having a column of type list or map:

CREATE NODE TABLE MyNode (
    my_id STRING,
    my_list STRING[],
    PRIMARY KEY(my_id)
)

But the Python SDK raises an exception when I pass a dict or a list to the params argument:

conn.execute(
    query="MERGE (n:MyNode { my_id: $my_id }) SET n.my_list = $my_list RETURN n.my_id",
    parameters ={
        'my_list': ['a', 'b']
    },
)
    def execute(
        self,
        query: str | PreparedStatement,
        parameters: dict[str, Any] | None = None,
    ) -> QueryResult:
        """
        Execute a query.

        Parameters
        ----------
        query : str | PreparedStatement
            A prepared statement or a query string.
            If a query string is given, a prepared statement will be created
            automatically.

        parameters : dict[str, Any]
            Parameters for the query.

        Returns
        -------
        QueryResult
            Query result.

        """
        if parameters is None:
            parameters = {}

        self.init_connection()
        if not isinstance(parameters, dict):
            # TODO(Chang): remove ROLLBACK once we can guarantee database is deleted after conn
            self._connection.execute(self.prepare("ROLLBACK")._prepared_statement, {})
            msg = f"Parameters must be a dict; found {type(parameters)}."
            raise RuntimeError(msg)  # noqa: TRY004

        prepared_statement = self.prepare(query) if isinstance(query, str) else query
>       _query_result = self._connection.execute(prepared_statement._prepared_statement, parameters)
E       RuntimeError: Unknown parameter type <class 'list'>
@andyfengHKU
Copy link
Contributor

The issue itself should be fixed in #3090. Though I would wait for #3140 before we close the issue. #3140 should fix the case when parameter type mismatch with expected type.

@andyfengHKU andyfengHKU reopened this Mar 26, 2024
@bigluck
Copy link
Author

bigluck commented Mar 27, 2024

Thanks, @andyfengHKU.
It now works for the vast majority of the cases, but one of my tests fails with: RuntimeError: Parameter a_dict_of_optional_integers has data type MAP(STRING: ANY) but expects MAP(STRING: INT64).

It happens when I pass:

'a_dict_of_optional_integers': {
    'key1': 1,
    'key2': None,
}

Or when I pass a list of optional strings
RuntimeError: Parameter a_list_of_optional_str has data type ANY[] but expects STRING[].:

'a_list_of_optional_str': ['test1', None],

@bigluck
Copy link
Author

bigluck commented Mar 29, 2024

It now works starting from v0.3.3.dev17, thanks so much!

@bigluck bigluck closed this as completed Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants