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

Support use of QueryResult as a context manager, and add a get_schema method #3009

Merged
merged 1 commit into from
Mar 9, 2024

Conversation

alexander-beedie
Copy link
Contributor

@alexander-beedie alexander-beedie commented Mar 8, 2024

Currently close() is called on QueryResult either manually or via __del__ (which is not deterministic as it relies on GC being triggered and may never actually be called).

A cleaner pattern is to use context manager syntax; this both guarantees immediate connection close on scope exit, and gracefully handles the case where an error is raised before the connection is manually closed.

Note: this syntax is optional. If you don't use it, you get the existing behaviour (it's a non-breaking addition).

Example

Manual:

Requires a potentially fallible call to close().

res = conn.execute("MATCH (a:person) WHERE a.ID = 0 RETURN a")
df = res.get_as_pl()  # <-- if this raises "close" will NOT be called
res.close()

Context manager:

Scope exit guarantees that close() will be called successfully.

with conn.execute("MATCH (a:person) WHERE a.ID = 0 RETURN a") as res:
    df = res.get_as_pl()  # <-- if this raises "close" WILL still be called

Also

  • Added a handy get_schema() method (combines get_column_names() and get_column_data_types() as a dict).
  • Made the chunk_size used by get_as_pl() adaptive, minimising the chance that Polars needs to rechunk the result.
  • Added test coverage for all of the above updates.
  • Added some "See Also" docstring sections.

CLA

I have read and agree to the terms under CLA.md 👌

@mewim
Copy link
Member

mewim commented Mar 9, 2024

@alexander-beedie

Thanks for your contribution.

Could you please take a look at our CLA and add “I have read and agree to the terms under CLA.md” to your PR description if you agree with the CLA?

@alexander-beedie
Copy link
Contributor Author

Could you please take a look at our CLA and add “I have read and agree to the terms under CLA.md” to your PR description if you agree with the CLA?

Done ;)

@mewim mewim merged commit 45c5aa9 into kuzudb:master Mar 9, 2024
9 of 13 checks passed
@alexander-beedie alexander-beedie deleted the query-result-context branch March 9, 2024 07:27
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 this pull request may close these issues.

3 participants