Backend Base Classes¶
BaseBackend
¶
Bases: abc.ABC
, _FileIOHandler
Base backend class.
All Ibis backends must subclass this class and implement all the required methods.
Attributes¶
db_identity: str
cached
property
¶
Return the identity of the database.
Multiple connections to the same
database will return the same value for db_identity
.
The default implementation assumes connection parameters uniquely specify the database.
Returns:
Type | Description |
---|---|
Hashable
|
Database identity |
tables
cached
property
¶
An accessor for tables in the database.
Tables may be accessed by name using either index or attribute access:
Examples:
>>> con = ibis.sqlite.connect("example.db")
>>> people = con.tables['people'] # access via index
>>> people = con.tables.people # access via attribute
version: str
abstractmethod
property
¶
Return the version of the backend engine.
For database servers, return the server version.
For others such as SQLite and pandas return the version of the underlying library or application.
Returns:
Type | Description |
---|---|
str
|
The backend version |
Functions¶
add_operation(operation)
¶
Add a translation function to the backend for a specific operation.
Operations are defined in ibis.expr.operations
, and a translation
function receives the translator object and an expression as
parameters, and returns a value depending on the backend.
compile(expr, params=None)
¶
Compile an expression.
connect(*args, **kwargs)
¶
Connect to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Mandatory connection parameters, see the docstring of |
()
|
|
**kwargs |
Extra connection parameters, see the docstring of |
{}
|
Notes¶
This creates a new backend instance with saved args
and kwargs
,
then calls reconnect
and finally returns the newly created and
connected backend instance.
Returns:
Type | Description |
---|---|
BaseBackend
|
An instance of the backend |
create_table(name, obj=None, *, schema=None, database=None, temp=False, overwrite=False)
abstractmethod
¶
Create a new table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the new table. |
required |
obj |
pd.DataFrame | pa.Table | ir.Table | None
|
An Ibis table expression or pandas table that will be used to
extract the schema and the data of the new table. If not provided,
|
None
|
schema |
ibis.Schema | None
|
The schema for the new table. Only one of |
None
|
database |
str | None
|
Name of the database where the table will be created, if not the default. |
None
|
temp |
bool
|
Whether a table is temporary or not |
False
|
overwrite |
bool
|
Whether to clobber existing data |
False
|
Returns:
Type | Description |
---|---|
Table
|
The table that was created. |
create_view(name, obj, *, database=None, overwrite=False)
abstractmethod
¶
Create a new view from an expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the new view. |
required |
obj |
ir.Table
|
An Ibis table expression that will be used to create the view. |
required |
database |
str | None
|
Name of the database where the view will be created, if not provided the database's default is used. |
None
|
overwrite |
bool
|
Whether to clobber an existing view with the same name |
False
|
Returns:
Type | Description |
---|---|
Table
|
The view that was created. |
database(name=None)
¶
Return a Database
object for the name
database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str | None
|
Name of the database to return the object for. |
None
|
Returns:
Type | Description |
---|---|
Database
|
A database object for the specified database. |
drop_table(name, *, database=None, force=False)
abstractmethod
¶
drop_view(name, *, database=None, force=False)
abstractmethod
¶
execute(expr)
¶
Execute an expression.
has_operation(operation)
classmethod
¶
Return whether the backend implements support for operation
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operation |
type[ops.Value]
|
A class corresponding to an operation. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the backend implements the operation. |
Examples:
>>> import ibis
>>> import ibis.expr.operations as ops
>>> ibis.sqlite.has_operation(ops.ArrayIndex)
False
>>> ibis.postgres.has_operation(ops.ArrayIndex)
True
list_tables(like=None, database=None)
abstractmethod
¶
Return the list of table names in the current database.
For some backends, the tables may be files in a directory, or other equivalent entities in a SQL database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
like |
str
|
A pattern in Python's regex format. |
None
|
database |
str
|
The database to list tables of, if not the current one. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
The list of the table names that match the pattern |
read_csv(path, table_name=None, **kwargs)
¶
Register a CSV file as a table in the current backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str | Path
|
The data source. A string or Path to the CSV file. |
required |
table_name |
str | None
|
An optional name to use for the created table. This defaults to a sequentially generated name. |
None
|
**kwargs |
Any
|
Additional keyword arguments passed to the backend loading function. |
{}
|
Returns:
Type | Description |
---|---|
ir.Table
|
The just-registered table |
read_parquet(path, table_name=None, **kwargs)
¶
Register a parquet file as a table in the current backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str | Path
|
The data source. |
required |
table_name |
str | None
|
An optional name to use for the created table. This defaults to a sequentially generated name. |
None
|
**kwargs |
Any
|
Additional keyword arguments passed to the backend loading function. |
{}
|
Returns:
Type | Description |
---|---|
ir.Table
|
The just-registered table |
register_options()
classmethod
¶
Register custom backend options.
table(name, database=None)
abstractmethod
¶
to_csv(expr, path, *, params=None, **kwargs)
¶
Write the results of executing the given expression to a CSV file.
This method is eager and will execute the associated expression immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Table
|
The ibis expression to execute and persist to CSV. |
required |
path |
str | Path
|
The data source. A string or Path to the CSV file. |
required |
params |
Mapping[ir.Scalar, Any] | None
|
Mapping of scalar parameter expressions to value. |
None
|
kwargs |
Any
|
Additional keyword arguments passed to pyarrow.csv.CSVWriter |
{}
|
to_delta(expr, path, *, params=None, **kwargs)
¶
Write the results of executing the given expression to a Delta Lake table.
This method is eager and will execute the associated expression immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Table
|
The ibis expression to execute and persist to Delta Lake table. |
required |
path |
str | Path
|
The data source. A string or Path to the Delta Lake table. |
required |
params |
Mapping[ir.Scalar, Any] | None
|
Mapping of scalar parameter expressions to value. |
None
|
kwargs |
Any
|
Additional keyword arguments passed to deltalake.writer.write_deltalake method |
{}
|
to_parquet(expr, path, *, params=None, **kwargs)
¶
Write the results of executing the given expression to a parquet file.
This method is eager and will execute the associated expression immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Table
|
The ibis expression to execute and persist to parquet. |
required |
path |
str | Path
|
The data source. A string or Path to the parquet file. |
required |
params |
Mapping[ir.Scalar, Any] | None
|
Mapping of scalar parameter expressions to value. |
None
|
**kwargs |
Any
|
Additional keyword arguments passed to pyarrow.parquet.ParquetWriter |
{}
|
to_pyarrow(expr, *, params=None, limit=None, **kwargs)
¶
Execute expression and return results in as a pyarrow table.
This method is eager and will execute the associated expression immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Expr
|
Ibis expression to export to pyarrow |
required |
params |
Mapping[ir.Scalar, Any] | None
|
Mapping of scalar parameter expressions to value. |
None
|
limit |
int | str | None
|
An integer to effect a specific row limit. A value of |
None
|
kwargs |
Any
|
Keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
Table
|
A pyarrow table holding the results of the executed expression. |
to_pyarrow_batches(expr, *, params=None, limit=None, chunk_size=1000000, **kwargs)
¶
Execute expression and return a RecordBatchReader.
This method is eager and will execute the associated expression immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Expr
|
Ibis expression to export to pyarrow |
required |
limit |
int | str | None
|
An integer to effect a specific row limit. A value of |
None
|
params |
Mapping[ir.Scalar, Any] | None
|
Mapping of scalar parameter expressions to value. |
None
|
chunk_size |
int
|
Maximum number of rows in each returned record batch. |
1000000
|
kwargs |
Any
|
Keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
results
|
RecordBatchReader |
to_torch(expr, *, params=None, limit=None, **kwargs)
¶
Execute an expression and return results as a dictionary of torch tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
ir.Expr
|
Ibis expression to execute. |
required |
params |
Mapping[ir.Scalar, Any] | None
|
Parameters to substitute into the expression. |
None
|
limit |
int | str | None
|
An integer to effect a specific row limit. A value of |
None
|
kwargs |
Any
|
Keyword arguments passed into the backend's |
{}
|
Returns:
Type | Description |
---|---|
dict[str, torch.Tensor]
|
A dictionary of torch tensors, keyed by column name. |