Skip to content

feat: improve typing and docstring for to_pyarrow #11215

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 40 additions & 6 deletions ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import weakref
from collections import Counter
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple
from typing import TYPE_CHECKING, Any, ClassVar, NamedTuple, overload

import ibis
import ibis.common.exceptions as exc
Expand Down Expand Up @@ -179,6 +179,39 @@ def to_pandas_batches(
)
)

@overload
def to_pyarrow(
self,
expr: ir.Table,
/,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Table: ...

@overload
def to_pyarrow(
self,
expr: ir.Column,
/,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Array: ...

@overload
def to_pyarrow(
self,
expr: ir.Scalar,
/,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Scalar: ...

@util.experimental
def to_pyarrow(
self,
Expand All @@ -188,8 +221,8 @@ def to_pyarrow(
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Table:
"""Execute expression and return results in as a pyarrow table.
) -> pa.Table | pa.Array | pa.Scalar:
"""Execute expression to a pyarrow object.

This method is eager and will execute the associated expression
immediately.
Expand All @@ -208,9 +241,10 @@ def to_pyarrow(

Returns
-------
Table
A pyarrow table holding the results of the executed expression.

result
If the passed expression is a Table, a pyarrow table is returned.
If the passed expression is a Column, a pyarrow array is returned.
If the passed expression is a Scalar, a pyarrow scalar is returned.
"""
pa = self._import_pyarrow()
self._run_pre_execute_hooks(expr)
Expand Down
12 changes: 7 additions & 5 deletions ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ def to_pyarrow(
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Table:
"""Execute expression and return results in as a pyarrow table.
) -> pa.Table | pa.Array | pa.Scalar:
"""Execute expression to a pyarrow object.

This method is eager and will execute the associated expression
immediately.
Expand All @@ -567,14 +567,16 @@ def to_pyarrow(
Mapping of scalar parameter expressions to value.
limit
An integer to effect a specific row limit. A value of `None` means
"no limit". The default is in `ibis/config.py`.
no limit. The default is in `ibis/config.py`.
kwargs
Keyword arguments

Returns
-------
Table
A pyarrow table holding the results of the executed expression.
result
If the passed expression is a Table, a pyarrow table is returned.
If the passed expression is a Column, a pyarrow array is returned.
If the passed expression is a Scalar, a pyarrow scalar is returned.
"""
return self._find_backend(use_default=True).to_pyarrow(
self, params=params, limit=limit, **kwargs
Expand Down
24 changes: 23 additions & 1 deletion ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ibis.common.grounds import Singleton
from ibis.expr.rewrites import rewrite_window_input
from ibis.expr.types.core import Expr, _binop, _FixedTextJupyterMixin
from ibis.util import deprecated, promote_list
from ibis.util import deprecated, experimental, promote_list

if TYPE_CHECKING:
import datetime
Expand Down Expand Up @@ -1384,6 +1384,17 @@ def to_pandas(

@public
class Scalar(Value):
# overriding Expr's implementation just for typing
@experimental
def to_pyarrow(
self,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Scalar:
return super().to_pyarrow(params=params, limit=limit, **kwargs)

def __pyarrow_result__(
self,
table: pa.Table,
Expand Down Expand Up @@ -1574,6 +1585,17 @@ def preview(
console_width=console_width,
)

# overriding Expr's implementation just for typing
@experimental
def to_pyarrow(
self,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Array:
return super().to_pyarrow(params=params, limit=limit, **kwargs)

def __pyarrow_result__(
self,
table: pa.Table,
Expand Down
13 changes: 12 additions & 1 deletion ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ibis.expr.types.core import Expr, _FixedTextJupyterMixin
from ibis.expr.types.generic import Value, literal
from ibis.expr.types.temporal import TimestampColumn
from ibis.util import deprecated
from ibis.util import deprecated, experimental

if TYPE_CHECKING:
import pandas as pd
Expand Down Expand Up @@ -242,6 +242,17 @@ def __polars_result__(self, df: pl.DataFrame) -> Any:

return PolarsData.convert_table(df, self.schema())

# overriding Expr's implementation just for typing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to indicate an override without redefining the method?

@experimental
def to_pyarrow(
self,
*,
params: Mapping[ir.Scalar, Any] | None = None,
limit: int | str | None = None,
**kwargs: Any,
) -> pa.Table:
return super().to_pyarrow(params=params, limit=limit, **kwargs)

def _fast_bind(self, *args, **kwargs):
# allow the first argument to be either a dictionary or a list of values
if len(args) == 1:
Expand Down
Loading