Skip to content

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

current_database: str | None abstractmethod property

Return the name of the current database.

Backends that don't support different databases will return None.

Returns:

Type Description
str | None

Name of the current database.

db_identity: str property cached

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 property cached

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. For example, in SQL backends, a NullLiteral operation could be translated to the string "NULL".

Examples:

>>> @ibis.sqlite.add_operation(ibis.expr.operations.NullLiteral)
... def _null_literal(translator, expression):
...     return 'NULL'

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 do_connect for details.

()
**kwargs

Extra connection parameters, see the docstring of do_connect for details.

{}
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_database(name, force=False)

Create a new database.

Not all backends implement this method.

Parameters:

Name Type Description Default
name str

Name of the new database.

required
force bool

If False, an exception is raised if the database already exists.

False

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 | 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, schema must be given.

None
schema ibis.Schema | None

The schema for the new table. Only one of schema or obj can be provided.

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 a table.

Parameters:

Name Type Description Default
name str

Name of the table to drop.

required
database str | None

Name of the database where the table exists, if not the default.

None
force bool

If False, an exception is raised if the table does not exist.

False

drop_view(name, *, database=None, force=False) abstractmethod

Drop a view.

Parameters:

Name Type Description Default
name str

Name of the view to drop.

required
database str | None

Name of the database where the view exists, if not the default.

None
force bool

If False, an exception is raised if the view does not exist.

False

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_databases(like=None) abstractmethod

List existing databases in the current connection.

Parameters:

Name Type Description Default
like str

A pattern in Python's regex format to filter returned database names.

None

Returns:

Type Description
list[str]

The database names that exist in the current connection, that match the like pattern if provided.

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, optional

A pattern in Python's regex format.

None
database str, optional

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 like.

register_options() classmethod

Register custom backend options.

table(name, database=None) abstractmethod

Construct a table expression.

Parameters:

Name Type Description Default
name str

Table name

required
database str | None

Database name

None

Returns:

Type Description
Table

Table expression


Last update: January 4, 2023