Skip to content

Commit

Permalink
fix: duckdb skip create function if no numpy (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored Jun 3, 2024
1 parent 353cdce commit 5cf92bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 9 additions & 0 deletions sqlframe/base/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ def verify_openai_installed():
)


def verify_numpy_installed():
try:
import numpy # noqa
except ImportError:
raise ImportError(
"""Numpy is required for this functionality. `pip install "sqlframe[pandas]"` (also include your engine if needed) to install pandas/numpy."""
)


def quote_preserving_alias_or_name(col: t.Union[exp.Column, exp.Alias]) -> str:
from sqlframe.base.session import _BaseSession

Expand Down
10 changes: 8 additions & 2 deletions sqlframe/duckdb/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import cached_property

from sqlframe.base.session import _BaseSession
from sqlframe.base.util import soundex
from sqlframe.base.util import soundex, verify_numpy_installed
from sqlframe.duckdb.catalog import DuckDBCatalog
from sqlframe.duckdb.dataframe import DuckDBDataFrame
from sqlframe.duckdb.readwriter import (
Expand Down Expand Up @@ -41,7 +41,13 @@ def __init__(self, conn: t.Optional[DuckDBPyConnection] = None, *args, **kwargs)

if not hasattr(self, "_conn"):
conn = conn or duckdb.connect()
conn.create_function("SOUNDEX", lambda x: soundex(x), return_type=VARCHAR)
try:
# Creating a function requires numpy to be installed so if they don't have it, we'll just skip it
verify_numpy_installed()
conn.create_function("SOUNDEX", lambda x: soundex(x), return_type=VARCHAR)
except ImportError:
pass

super().__init__(conn, *args, **kwargs)

@classmethod
Expand Down

0 comments on commit 5cf92bf

Please sign in to comment.