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

refactor(python): lazily import connectorx #5835

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion polars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ publish:

.PHONY: help
help: ## Display this help screen
@echo -e '\033[1mAvailable commands:\033[0m'
@echo -e "\033[1mAvailable commands:\033[0m\n"
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}' | sort
2 changes: 1 addition & 1 deletion py-polars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ clean: ## Clean up caches and build artifacts

.PHONY: help
help: ## Display this help screen
@echo -e '\033[1mAvailable commands:\033[0m'
@echo -e "\033[1mAvailable commands:\033[0m\n"
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}' | sort
36 changes: 16 additions & 20 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
from polars.internals.io import _prepare_file_arg
from polars.utils import deprecated_alias, format_path, handle_projection_columns

try:
import connectorx as cx

_WITH_CX = True
except ImportError:
_WITH_CX = False

if TYPE_CHECKING:
from polars.internals.type_aliases import CsvEncoding, ParallelStrategy

Expand Down Expand Up @@ -1103,21 +1096,24 @@ def read_sql(
>>> pl.read_sql(queries, uri) # doctest: +SKIP

"""
if _WITH_CX:
tbl = cx.read_sql(
conn=connection_uri,
query=sql,
return_type="arrow2",
partition_on=partition_on,
partition_range=partition_range,
partition_num=partition_num,
protocol=protocol,
)
return cast(DataFrame, from_arrow(tbl))
else:
try:
import connectorx as cx
except ImportError:
raise ImportError(
"connectorx is not installed. Please run `pip install connectorx>=0.2.2`."
)
) from None

tbl = cx.read_sql(
conn=connection_uri,
query=sql,
return_type="arrow2",
partition_on=partition_on,
partition_range=partition_range,
partition_num=partition_num,
protocol=protocol,
)

return cast(DataFrame, from_arrow(tbl))


@overload
Expand Down