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(udf): remove hierarchical usage of schema #9078

Merged
merged 2 commits into from
Apr 30, 2024
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 ibis/backends/bigquery/tests/unit/udf/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def farm_fingerprint(value: bytes) -> int: ...


@ibis.udf.scalar.builtin(schema="fn", database="bqutil")
@ibis.udf.scalar.builtin(database="fn", catalog="bqutil")
def from_hex(value: str) -> int:
"""Community function to convert from hex string to integer.

Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/impala/udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _create_operation_class(self):
fn=self._make_fn(),
name=self.name,
signature=(self.inputs, self.output),
schema=self.database,
database=self.database,
)


Expand All @@ -78,7 +78,7 @@ def _create_operation_class(self):
fn=self._make_fn(),
name=self.name,
signature=(self.inputs, self.output),
schema=self.database,
database=self.database,
)


Expand Down
10 changes: 5 additions & 5 deletions ibis/backends/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,15 @@
(schema,) = cur.fetchone()
return schema

def function(self, name: str, *, schema: str | None = None) -> Callable:
def function(self, name: str, *, database: str | None = None) -> Callable:
n = ColGen(table="n")
p = ColGen(table="p")
f = self.compiler.f

predicates = [p.proname.eq(name)]

if schema is not None:
predicates.append(n.nspname.rlike(sge.convert(f"^({schema})$")))
if database is not None:
predicates.append(n.nspname.rlike(sge.convert(f"^({database})$")))

query = (
sg.select(
Expand All @@ -448,7 +448,7 @@
rows = cur.fetchall()

if not rows:
name = f"{schema}.{name}" if schema else name
name = f"{database}.{name}" if database else name

Check warning on line 451 in ibis/backends/postgres/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/postgres/__init__.py#L451

Added line #L451 was not covered by tests
raise exc.MissingUDFError(name)
elif len(rows) > 1:
raise exc.AmbiguousUDFError(name)
Expand All @@ -471,7 +471,7 @@
return_annotation=return_type,
)
fake_func.__annotations__ = {"return": return_type, **dict(signature)}
op = ops.udf.scalar.builtin(fake_func, schema=schema)
op = ops.udf.scalar.builtin(fake_func, database=database)
return op

def _get_udf_source(self, udf_node: ops.ScalarUDF):
Expand Down
12 changes: 6 additions & 6 deletions ibis/backends/postgres/tests/test_udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ def table(con_for_udf, table_name, test_database):
def test_existing_sql_udf(con_for_udf, test_database, table):
"""Test creating ibis UDF object based on existing UDF in the database."""
# Create ibis UDF objects referring to UDFs already created in the database
custom_length_udf = con_for_udf.function("custom_len", schema=test_database)
custom_length_udf = con_for_udf.function("custom_len", database=test_database)
result_obj = table[table, custom_length_udf(table["user_name"]).name("custom_len")]
result = result_obj.execute()
assert result["custom_len"].sum() == result["name_length"].sum()


def test_existing_plpython_udf(con_for_udf, test_database, table):
# Create ibis UDF objects referring to UDFs already created in the database
py_length_udf = con_for_udf.function("pylen", schema=test_database)
py_length_udf = con_for_udf.function("pylen", database=test_database)
result_obj = table[table, py_length_udf(table["user_name"]).name("custom_len")]
result = result_obj.execute()
assert result["custom_len"].sum() == result["name_length"].sum()
Expand All @@ -103,7 +103,7 @@ def test_udf(test_database, table):
"""Test creating a UDF in database based on Python function and then
creating an ibis UDF object based on that."""

@udf.scalar.python(schema=test_database)
@udf.scalar.python(database=test_database)
def mult_a_b(a: int, b: int) -> int:
return a * b

Expand All @@ -129,7 +129,7 @@ def test_array_type(test_database, table):
instantiated specifying the datatype of the elements of the array.
"""

@udf.scalar.python(schema=test_database)
@udf.scalar.python(database=test_database)
def pysplit(text: str, split: str) -> list[str]:
return text.split(split)

Expand All @@ -142,7 +142,7 @@ def test_client_udf_api(test_database, table):
"""Test creating a UDF in database based on Python function using an ibis
client method."""

@udf.scalar.python(schema=test_database)
@udf.scalar.python(database=test_database)
def multiply(a: int, b: int) -> int:
return a * b

Expand Down Expand Up @@ -171,7 +171,7 @@ def wrapped(*args, **kwds):
return wrapped

@decorator
@udf.scalar.python(schema=test_database)
@udf.scalar.python(database=test_database)
def multiply(a: int, b: int) -> int:
return a * b

Expand Down
7 changes: 5 additions & 2 deletions ibis/backends/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
def _to_sqlglot_table(self, database):
if database is None:
return None
elif isinstance(database, tuple):
elif isinstance(database, (list, tuple)):
if len(database) > 2:
raise ValueError(
"Only database hierarchies of two or fewer levels are supported."
Expand Down Expand Up @@ -558,6 +558,9 @@
db = table.args["this"]
database = sg.exp.Table(catalog=catalog, db=db)
else:
raise ValueError("oops")
raise ValueError(

Check warning on line 561 in ibis/backends/sql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/sql/__init__.py#L561

Added line #L561 was not covered by tests
"""Invalid database hierarchy format. Please use either dotted
strings ('catalog.database') or tuples ('catalog', 'database')."""
)

return database
Loading
Loading