Example #1
0
 def execute(
     self,
     expr: ir.Expr,
     params: Mapping[ir.Expr, object] = None,
     limit: str = 'default',
     **kwargs: Any,
 ):
     if isinstance(expr, ir.TableExpr):
         frame = self.compile(expr, params, **kwargs)
         table = _to_pyarrow_table(frame)
         return table.to_pandas()
     elif isinstance(expr, ir.ColumnExpr):
         # expression must be named for the projection
         expr = expr.name('tmp').to_projection()
         frame = self.compile(expr, params, **kwargs)
         table = _to_pyarrow_table(frame)
         return table['tmp'].to_pandas()
     elif isinstance(expr, ir.ScalarExpr):
         if expr.op().root_tables():
             # there are associated datafusion tables so convert the expr
             # to a selection which we can directly convert to a datafusion
             # plan
             expr = expr.name('tmp').to_projection()
             frame = self.compile(expr, params, **kwargs)
         else:
             # doesn't have any tables associated so create a plan from a
             # dummy datafusion table
             compiled = self.compile(expr, params, **kwargs)
             frame = self._context.empty_table().select(compiled)
         table = _to_pyarrow_table(frame)
         return table[0][0].as_py()
     else:
         raise com.IbisError(
             f"Cannot execute expression of type: {type(expr)}"
         )
Example #2
0
 def execute(
     self,
     expr: ir.Expr,
     timecontext: Mapping | None = None,
     params: Mapping[ir.Scalar, Any] | None = None,
     limit: str = 'default',
     **kwargs: Any,
 ) -> Any:
     """Execute an expression."""
     if isinstance(expr, types.Table):
         return self.compile(expr, timecontext, params, **kwargs).toPandas()
     elif isinstance(expr, types.Column):
         # expression must be named for the projection
         if not expr.has_name():
             expr = expr.name("tmp")
         return self.compile(
             expr.to_projection(), timecontext, params, **kwargs
         ).toPandas()[expr.get_name()]
     elif isinstance(expr, types.Scalar):
         compiled = self.compile(expr, timecontext, params, **kwargs)
         if isinstance(compiled, Column):
             # attach result column to a fake DataFrame and
             # select the result
             compiled = self._session.range(0, 1).select(compiled)
         return compiled.toPandas().iloc[0, 0]
     else:
         raise com.IbisError(
             f"Cannot execute expression of type: {type(expr)}"
         )