|
1 | 1 | import functools |
2 | 2 | import importlib.metadata |
| 3 | +from abc import ABC |
| 4 | +from typing import Any, Mapping |
| 5 | + |
| 6 | +from xorq.vendor.ibis import BaseBackend |
| 7 | +from xorq.vendor.ibis.expr import types as ir |
| 8 | + |
| 9 | + |
| 10 | +class ExecutionBackend(BaseBackend, ABC): |
| 11 | + def _pandas_execute(self, expr: ir.Expr, **kwargs): |
| 12 | + from xorq.expr.api import _transform_expr |
| 13 | + from xorq.expr.relations import FlightExpr, FlightUDXF |
| 14 | + |
| 15 | + node = expr.op() |
| 16 | + if isinstance(node, (FlightExpr, FlightUDXF)): |
| 17 | + df = node.to_rbr().read_pandas(timestamp_as_object=True) |
| 18 | + return expr.__pandas_result__(df) |
| 19 | + (expr, created) = _transform_expr(expr) |
| 20 | + |
| 21 | + return super().execute(expr, **kwargs) |
| 22 | + |
| 23 | + def execute(self, expr, **kwargs) -> Any: |
| 24 | + if self.name == "pandas": |
| 25 | + return self._pandas_execute(expr, **kwargs) |
| 26 | + |
| 27 | + batch_reader = self.to_pyarrow_batches(expr, **kwargs) |
| 28 | + df = batch_reader.read_pandas(timestamp_as_object=True) |
| 29 | + |
| 30 | + return expr.__pandas_result__(df) |
| 31 | + |
| 32 | + def to_pyarrow_batches( |
| 33 | + self, |
| 34 | + expr: ir.Expr, |
| 35 | + *, |
| 36 | + chunk_size: int = 1_000_000, |
| 37 | + **kwargs: Any, |
| 38 | + ): |
| 39 | + from xorq.common.utils.defer_utils import rbr_wrapper |
| 40 | + from xorq.expr.api import _transform_expr |
| 41 | + from xorq.expr.relations import FlightExpr, FlightUDXF |
| 42 | + |
| 43 | + if isinstance(expr.op(), (FlightExpr, FlightUDXF)): |
| 44 | + return expr.op().to_rbr() |
| 45 | + (expr, created) = _transform_expr(expr) |
| 46 | + reader = super().to_pyarrow_batches(expr, chunk_size=chunk_size, **kwargs) |
| 47 | + |
| 48 | + def clean_up(): |
| 49 | + for table_name, conn in created.items(): |
| 50 | + try: |
| 51 | + conn.drop_table(table_name, force=True) |
| 52 | + except Exception: |
| 53 | + conn.drop_view(table_name) |
| 54 | + |
| 55 | + return rbr_wrapper(reader, clean_up) |
| 56 | + |
| 57 | + def _pandas_to_pyarrow(self, expr, **kwargs): |
| 58 | + from xorq.expr.api import _transform_expr |
| 59 | + from xorq.expr.relations import FlightExpr, FlightUDXF |
| 60 | + |
| 61 | + node = expr.op() |
| 62 | + if isinstance(node, (FlightExpr, FlightUDXF)): |
| 63 | + df = node.to_rbr().read_pandas(timestamp_as_object=True) |
| 64 | + return expr.__pyarrow_result__(df) |
| 65 | + (expr, created) = _transform_expr(expr) |
| 66 | + |
| 67 | + return super().to_pyarrow(expr, **kwargs) |
| 68 | + |
| 69 | + def to_pyarrow( |
| 70 | + self, |
| 71 | + expr: ir.Expr, |
| 72 | + *, |
| 73 | + params: Mapping[ir.Scalar, Any] | None = None, |
| 74 | + limit: int | str | None = None, |
| 75 | + **kwargs: Any, |
| 76 | + ): |
| 77 | + if self.name == "pandas": |
| 78 | + return self._pandas_to_pyarrow(expr, **kwargs) |
| 79 | + |
| 80 | + batch_reader = self.to_pyarrow_batches(expr, **kwargs) |
| 81 | + arrow_table = batch_reader.read_all() |
| 82 | + return expr.__pyarrow_result__(arrow_table) |
3 | 83 |
|
4 | 84 |
|
5 | 85 | @functools.cache |
|
0 commit comments