Skip to content

Commit

Permalink
refactor(python): lazily import connectorx (#5835)
Browse files Browse the repository at this point in the history
Signed-off-by: chitralverma <chitralverma@gmail.com>
  • Loading branch information
chitralverma committed Dec 17, 2022
1 parent d4f5e73 commit 00977c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
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

0 comments on commit 00977c5

Please sign in to comment.