Support use of QueryResult
as a context manager, and add a get_schema
method
#3009
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently
close()
is called onQueryResult
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()
.Context manager:
Scope exit guarantees that
close()
will be called successfully.Also
get_schema()
method (combinesget_column_names()
andget_column_data_types()
as a dict).get_as_pl()
adaptive, minimising the chance that Polars needs to rechunk the result.CLA
I have read and agree to the terms under
CLA.md
👌