Skip to content

Commit d20e674

Browse files
committed
docs: improve docstrings on methods using the database param
1 parent 6af1dbf commit d20e674

File tree

22 files changed

+339
-587
lines changed

22 files changed

+339
-587
lines changed

docs/backend_table_hierarchy.qmd renamed to docs/concepts/backend-table-hierarchy.qmd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
title: Backend Table Hierarchy
33
---
44

5-
Several SQL backends support two levels of hierarchy in organizing tables
6-
(although the levels are also used for other purposes, like data access,
7-
billing, etc.).
5+
Most SQL backends organize tables into groups, and some use a two-level hierarchy.
6+
They use terms such as `catalog`, `database`, and/or `schema` to refer to these groups.
87

9-
Ibis uses the following terminology:
8+
Ibis uses the following terminology throughout its codebase, API, and documentation:
109

1110
- `database`: a collection of tables
1211
- `catalog`: a collection of databases
1312

13+
In other words, the full specification of a table in Ibis is either
14+
- `catalog.database.table`
15+
- `database.table`
16+
1417
Below is a table with the terminology used by each backend for the two levels of
1518
hierarchy. This is provided as a reference, note that when using Ibis, we will
1619
use the terms `catalog` and `database` and map them onto the appropriate fields.
@@ -31,7 +34,8 @@ use the terms `catalog` and `database` and map them onto the appropriate fields.
3134
| pandas | | NA |
3235
| polars | | NA |
3336
| postgres | database | schema |
34-
| pyspark | | database |
37+
| pyspark | database | schema |
3538
| risingwave | database | schema |
36-
| snowflake | | database |
39+
| sqlite | | schema |
40+
| snowflake | datab | schema |
3741
| trino | catalog | schema |

ibis/backends/__init__.py

Lines changed: 157 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,52 @@ def to_json(
617617
)
618618

619619

620+
class HasCurrentCatalog(abc.ABC):
621+
@property
622+
@abc.abstractmethod
623+
def current_catalog(self) -> str:
624+
"""The name of the current catalog in the backend.
625+
626+
A collection of `table` is referred to as a `database`.
627+
A collection of `database` is referred to as a `catalog`.
628+
629+
These terms are mapped onto the corresponding features in each
630+
backend (where available), regardless of the terminology the backend uses.
631+
632+
See the
633+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
634+
for more info.
635+
636+
Returns
637+
-------
638+
str
639+
The name of the current catalog.
640+
"""
641+
642+
643+
class HasCurrentDatabase(abc.ABC):
644+
@property
645+
@abc.abstractmethod
646+
def current_database(self) -> str:
647+
"""The name of the current database in the backend.
648+
649+
A collection of `table` is referred to as a `database`.
650+
A collection of `database` is referred to as a `catalog`.
651+
652+
These terms are mapped onto the corresponding features in each
653+
backend (where available), regardless of the terminology the backend uses.
654+
655+
See the
656+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
657+
for more info.
658+
659+
Returns
660+
-------
661+
str
662+
The name of the current database.
663+
"""
664+
665+
620666
class CanListCatalog(abc.ABC):
621667
@abc.abstractmethod
622668
def list_catalogs(self, *, like: str | None = None) -> list[str]:
@@ -629,15 +675,17 @@ def list_catalogs(self, *, like: str | None = None) -> list[str]:
629675
A collection of `database` is referred to as a `catalog`.
630676
631677
These terms are mapped onto the corresponding features in each
632-
backend (where available), regardless of whether the backend itself
633-
uses the same terminology.
678+
backend (where available), regardless of the terminology the backend uses.
679+
680+
See the
681+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
682+
for more info.
634683
:::
635684
636685
Parameters
637686
----------
638687
like
639-
A pattern in Python's regex format to filter returned database
640-
names.
688+
A pattern in Python's regex format to filter returned catalog names.
641689
642690
Returns
643691
-------
@@ -659,8 +707,11 @@ def create_catalog(self, name: str, /, *, force: bool = False) -> None:
659707
A collection of `database` is referred to as a `catalog`.
660708
661709
These terms are mapped onto the corresponding features in each
662-
backend (where available), regardless of whether the backend itself
663-
uses the same terminology.
710+
backend (where available), regardless of the terminology the backend uses.
711+
712+
See the
713+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
714+
for more info.
664715
:::
665716
666717
Parameters
@@ -682,8 +733,11 @@ def drop_catalog(self, name: str, /, *, force: bool = False) -> None:
682733
A collection of `database` is referred to as a `catalog`.
683734
684735
These terms are mapped onto the corresponding features in each
685-
backend (where available), regardless of whether the backend itself
686-
uses the same terminology.
736+
backend (where available), regardless of the terminology the backend uses.
737+
738+
See the
739+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
740+
for more info.
687741
:::
688742
689743
Parameters
@@ -709,8 +763,11 @@ def list_databases(
709763
A collection of `database` is referred to as a `catalog`.
710764
711765
These terms are mapped onto the corresponding features in each
712-
backend (where available), regardless of whether the backend itself
713-
uses the same terminology.
766+
backend (where available), regardless of the terminology the backend uses.
767+
768+
See the
769+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
770+
for more info.
714771
:::
715772
716773
Parameters
@@ -737,6 +794,20 @@ def create_database(
737794
) -> None:
738795
"""Create a database named `name` in `catalog`.
739796
797+
::: {.callout-note}
798+
## Ibis does not use the word `schema` to refer to database hierarchy.
799+
800+
A collection of `table` is referred to as a `database`.
801+
A collection of `database` is referred to as a `catalog`.
802+
803+
These terms are mapped onto the corresponding features in each
804+
backend (where available), regardless of the terminology the backend uses.
805+
806+
See the
807+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
808+
for more info.
809+
:::
810+
740811
Parameters
741812
----------
742813
name
@@ -755,13 +826,27 @@ def drop_database(
755826
) -> None:
756827
"""Drop the database with `name` in `catalog`.
757828
829+
::: {.callout-note}
830+
## Ibis does not use the word `schema` to refer to database hierarchy.
831+
832+
A collection of `table` is referred to as a `database`.
833+
A collection of `database` is referred to as a `catalog`.
834+
835+
These terms are mapped onto the corresponding features in each
836+
backend (where available), regardless of the terminology the backend uses.
837+
838+
See the
839+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
840+
for more info.
841+
:::
842+
758843
Parameters
759844
----------
760845
name
761846
Name of the schema to drop.
762847
catalog
763-
Name of the catalog to drop the database from. If `None`, the
764-
current catalog is used.
848+
Name of the catalog to drop the database from.
849+
If `None`, the current catalog is used.
765850
force
766851
If `False`, an exception is raised if the database does not exist.
767852
@@ -981,73 +1066,105 @@ def _filter_with_like(values: Iterable[str], like: str | None = None) -> list[st
9811066
def list_tables(
9821067
self, *, like: str | None = None, database: tuple[str, str] | str | None = None
9831068
) -> list[str]:
984-
"""Return the list of table names in the current database.
1069+
"""The table names that match `like` in the given `database`.
9851070
9861071
For some backends, the tables may be files in a directory,
9871072
or other equivalent entities in a SQL database.
9881073
989-
::: {.callout-note}
990-
## Ibis does not use the word `schema` to refer to database hierarchy.
991-
992-
A collection of tables is referred to as a `database`.
993-
A collection of `database` is referred to as a `catalog`.
994-
995-
These terms are mapped onto the corresponding features in each
996-
backend (where available), regardless of whether the backend itself
997-
uses the same terminology.
998-
:::
999-
10001074
Parameters
10011075
----------
10021076
like
10031077
A pattern in Python's regex format.
10041078
database
1005-
The database from which to list tables.
1006-
If not provided, the current database is used.
1079+
The database, or (catalog, database) from which to list tables.
1080+
1081+
For backends that support a single-level table hierarchy,
1082+
you can pass in a string like `"bar"`.
10071083
For backends that support multi-level table hierarchies, you can
10081084
pass in a dotted string path like `"catalog.database"` or a tuple of
10091085
strings like `("catalog", "database")`.
1086+
If not provided, the current database
1087+
(and catalog, if applicable for this backend) is used.
1088+
1089+
See the
1090+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
1091+
for more info.
10101092
10111093
Returns
10121094
-------
10131095
list[str]
10141096
The list of the table names that match the pattern `like`.
10151097
1098+
Examples
1099+
--------
1100+
>>> import ibis
1101+
>>> con = ibis.duckdb.connect()
1102+
>>> foo = con.create_table("foo", schema=ibis.schema(dict(a="int")))
1103+
>>> con.list_tables()
1104+
['foo']
1105+
>>> bar = con.create_view("bar", foo)
1106+
>>> con.list_tables()
1107+
['bar', 'foo']
1108+
>>> con.create_database("my_database")
1109+
>>> con.list_tables(database="my_database")
1110+
[]
1111+
>>> con.raw_sql("CREATE TABLE my_database.baz (a INTEGER)") # doctest: +ELLIPSIS
1112+
<duckdb.duckdb.DuckDBPyConnection object at 0x...>
1113+
>>> con.list_tables(database="my_database")
1114+
['baz']
10161115
"""
10171116

10181117
@abc.abstractmethod
10191118
def table(
10201119
self, name: str, /, *, database: tuple[str, str] | str | None = None
10211120
) -> ir.Table:
1022-
"""Construct a table expression.
1023-
1024-
::: {.callout-note}
1025-
## Ibis does not use the word `schema` to refer to database hierarchy.
1026-
1027-
A collection of tables is referred to as a `database`.
1028-
A collection of `database` is referred to as a `catalog`.
1029-
1030-
These terms are mapped onto the corresponding features in each
1031-
backend (where available), regardless of whether the backend itself
1032-
uses the same terminology.
1033-
:::
1121+
"""Construct a table expression from the corresponding table in the backend.
10341122
10351123
Parameters
10361124
----------
10371125
name
10381126
Table name
10391127
database
1040-
Database name
1041-
If not provided, the current database is used.
1128+
The database, or (catalog, database) from which to get the table.
1129+
1130+
For backends that support a single-level table hierarchy,
1131+
you can pass in a string like `"bar"`.
10421132
For backends that support multi-level table hierarchies, you can
10431133
pass in a dotted string path like `"catalog.database"` or a tuple of
10441134
strings like `("catalog", "database")`.
1135+
If not provided, the current database
1136+
(and catalog, if applicable for this backend) is used.
1137+
1138+
See the
1139+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
1140+
for more info.
10451141
10461142
Returns
10471143
-------
10481144
Table
10491145
Table expression
10501146
1147+
Examples
1148+
--------
1149+
>>> import ibis
1150+
>>> backend = ibis.duckdb.connect()
1151+
1152+
Get the "foo" table from the current database
1153+
(and catalog, if applicable for this backend):
1154+
1155+
>>> backend.table("foo") # doctest: +SKIP
1156+
1157+
Get the "foo" table from the "bar" database
1158+
(in DuckDB's language they would say the "bar" schema,
1159+
in SQL this would be `"bar"."foo"`)
1160+
1161+
>>> backend.table("foo", database="bar") # doctest: +SKIP
1162+
1163+
Get the "foo" table from the "bar" database, within the "baz" catalog
1164+
(in DuckDB's language they would say the "bar" schema, and "baz" database,
1165+
in SQL this would be `"baz"."bar"."foo"`)
1166+
1167+
>>> backend.table("foo", database=("baz", "bar")) # doctest: +SKIP
10511168
"""
10521169

10531170
@property

0 commit comments

Comments
 (0)