Skip to content

Commit e364f80

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

File tree

21 files changed

+119
-541
lines changed

21 files changed

+119
-541
lines changed

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

Lines changed: 8 additions & 4 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.
@@ -33,5 +36,6 @@ use the terms `catalog` and `database` and map them onto the appropriate fields.
3336
| postgres | database | schema |
3437
| pyspark | | database |
3538
| risingwave | database | schema |
39+
| sqlite | | schema |
3640
| snowflake | | database |
3741
| trino | catalog | schema |

ibis/backends/__init__.py

Lines changed: 111 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,17 @@ def list_catalogs(self, *, like: str | None = None) -> list[str]:
629629
A collection of `database` is referred to as a `catalog`.
630630
631631
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.
632+
backend (where available), regardless of the terminology the backend uses.
633+
634+
See the
635+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
636+
for more info.
634637
:::
635638
636639
Parameters
637640
----------
638641
like
639-
A pattern in Python's regex format to filter returned database
640-
names.
642+
A pattern in Python's regex format to filter returned catalog names.
641643
642644
Returns
643645
-------
@@ -659,8 +661,11 @@ def create_catalog(self, name: str, /, *, force: bool = False) -> None:
659661
A collection of `database` is referred to as a `catalog`.
660662
661663
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.
664+
backend (where available), regardless of the terminology the backend uses.
665+
666+
See the
667+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
668+
for more info.
664669
:::
665670
666671
Parameters
@@ -682,8 +687,11 @@ def drop_catalog(self, name: str, /, *, force: bool = False) -> None:
682687
A collection of `database` is referred to as a `catalog`.
683688
684689
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.
690+
backend (where available), regardless of the terminology the backend uses.
691+
692+
See the
693+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
694+
for more info.
687695
:::
688696
689697
Parameters
@@ -709,8 +717,11 @@ def list_databases(
709717
A collection of `database` is referred to as a `catalog`.
710718
711719
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.
720+
backend (where available), regardless of the terminology the backend uses.
721+
722+
See the
723+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
724+
for more info.
714725
:::
715726
716727
Parameters
@@ -737,6 +748,20 @@ def create_database(
737748
) -> None:
738749
"""Create a database named `name` in `catalog`.
739750
751+
::: {.callout-note}
752+
## Ibis does not use the word `schema` to refer to database hierarchy.
753+
754+
A collection of `table` is referred to as a `database`.
755+
A collection of `database` is referred to as a `catalog`.
756+
757+
These terms are mapped onto the corresponding features in each
758+
backend (where available), regardless of the terminology the backend uses.
759+
760+
See the
761+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
762+
for more info.
763+
:::
764+
740765
Parameters
741766
----------
742767
name
@@ -755,13 +780,27 @@ def drop_database(
755780
) -> None:
756781
"""Drop the database with `name` in `catalog`.
757782
783+
::: {.callout-note}
784+
## Ibis does not use the word `schema` to refer to database hierarchy.
785+
786+
A collection of `table` is referred to as a `database`.
787+
A collection of `database` is referred to as a `catalog`.
788+
789+
These terms are mapped onto the corresponding features in each
790+
backend (where available), regardless of the terminology the backend uses.
791+
792+
See the
793+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
794+
for more info.
795+
:::
796+
758797
Parameters
759798
----------
760799
name
761800
Name of the schema to drop.
762801
catalog
763-
Name of the catalog to drop the database from. If `None`, the
764-
current catalog is used.
802+
Name of the catalog to drop the database from.
803+
If `None`, the current catalog is used.
765804
force
766805
If `False`, an exception is raised if the database does not exist.
767806
@@ -981,73 +1020,105 @@ def _filter_with_like(values: Iterable[str], like: str | None = None) -> list[st
9811020
def list_tables(
9821021
self, *, like: str | None = None, database: tuple[str, str] | str | None = None
9831022
) -> list[str]:
984-
"""Return the list of table names in the current database.
1023+
"""The table names that match `like` in the given `database`.
9851024
9861025
For some backends, the tables may be files in a directory,
9871026
or other equivalent entities in a SQL database.
9881027
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-
10001028
Parameters
10011029
----------
10021030
like
10031031
A pattern in Python's regex format.
10041032
database
1005-
The database from which to list tables.
1006-
If not provided, the current database is used.
1033+
The database, or (catalog, database) from which to list tables.
1034+
1035+
For backends that support a single-level table hierarchy,
1036+
you can pass in a string like `"bar"`.
10071037
For backends that support multi-level table hierarchies, you can
10081038
pass in a dotted string path like `"catalog.database"` or a tuple of
10091039
strings like `("catalog", "database")`.
1040+
If not provided, the current database
1041+
(and catalog, if applicable for this backend) is used.
1042+
1043+
See the
1044+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
1045+
for more info.
10101046
10111047
Returns
10121048
-------
10131049
list[str]
10141050
The list of the table names that match the pattern `like`.
10151051
1052+
Examples
1053+
--------
1054+
>>> import ibis
1055+
>>> con = ibis.duckdb.connect()
1056+
>>> foo = con.create_table("foo", schema=ibis.schema(dict(a="int")))
1057+
>>> con.list_tables()
1058+
['foo']
1059+
>>> bar = con.create_view("bar", foo)
1060+
>>> con.list_tables()
1061+
['bar', 'foo']
1062+
>>> con.create_database("my_database")
1063+
>>> con.list_tables(database="my_database")
1064+
[]
1065+
>>> con.raw_sql("CREATE TABLE my_database.baz (a INTEGER)") # doctest: +ELLIPSIS
1066+
<duckdb.duckdb.DuckDBPyConnection object at 0x...>
1067+
>>> con.list_tables(database="my_database")
1068+
['baz']
10161069
"""
10171070

10181071
@abc.abstractmethod
10191072
def table(
10201073
self, name: str, /, *, database: tuple[str, str] | str | None = None
10211074
) -> 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-
:::
1075+
"""Construct a table expression from the corresponding table in the backend.
10341076
10351077
Parameters
10361078
----------
10371079
name
10381080
Table name
10391081
database
1040-
Database name
1041-
If not provided, the current database is used.
1082+
The database, or (catalog, database) from which to get the table.
1083+
1084+
For backends that support a single-level table hierarchy,
1085+
you can pass in a string like `"bar"`.
10421086
For backends that support multi-level table hierarchies, you can
10431087
pass in a dotted string path like `"catalog.database"` or a tuple of
10441088
strings like `("catalog", "database")`.
1089+
If not provided, the current database
1090+
(and catalog, if applicable for this backend) is used.
1091+
1092+
See the
1093+
[Table Hierarchy Concepts Guide](/concepts/backend-table-hierarchy.qmd)
1094+
for more info.
10451095
10461096
Returns
10471097
-------
10481098
Table
10491099
Table expression
10501100
1101+
Examples
1102+
--------
1103+
>>> import ibis
1104+
>>> backend = ibis.duckdb.connect()
1105+
1106+
Get the "foo" table from the current database
1107+
(and catalog, if applicable for this backend):
1108+
1109+
>>> backend.table("foo") # doctest: +SKIP
1110+
1111+
Get the "foo" table from the "bar" database
1112+
(in DuckDB's language they would say the "bar" schema,
1113+
in SQL this would be `"bar"."foo"`)
1114+
1115+
>>> backend.table("foo", database="bar") # doctest: +SKIP
1116+
1117+
Get the "foo" table from the "bar" database, within the "baz" catalog
1118+
(in DuckDB's language they would say the "bar" schema, and "baz" database,
1119+
in SQL this would be `"baz"."bar"."foo"`)
1120+
1121+
>>> backend.table("foo", database=("baz", "bar")) # doctest: +SKIP
10511122
"""
10521123

10531124
@property

ibis/backends/athena/__init__.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,6 @@ def create_table(
210210
return self.table(orig_table_ref.name, database=(catalog, db))
211211

212212
def table(self, name: str, /, *, database: str | None = None) -> ir.Table:
213-
"""Construct a table expression.
214-
215-
Parameters
216-
----------
217-
name
218-
Table name
219-
database
220-
Database name
221-
222-
Returns
223-
-------
224-
Table
225-
Table expression
226-
"""
227213
table_loc = self._to_sqlglot_table(database)
228214

229215
# TODO: set these to better defaults
@@ -311,20 +297,6 @@ def list_catalogs(self, *, like: str | None = None) -> list[str]:
311297
def list_databases(
312298
self, like: str | None = None, catalog: str | None = None
313299
) -> list[str]:
314-
"""List databases.
315-
316-
Parameters
317-
----------
318-
like
319-
Regular expression to use to match database names.
320-
catalog
321-
Catalog in which to search for databases.
322-
323-
Returns
324-
-------
325-
list[str]
326-
A list of databases in `catalog` matching the pattern `like`.
327-
"""
328300
if catalog is None:
329301
catalog = self.current_catalog
330302
with self.con.cursor() as cur:
@@ -483,57 +455,6 @@ def drop_database(
483455
def list_tables(
484456
self, *, like: str | None = None, database: tuple[str, str] | str | None = None
485457
) -> list[str]:
486-
"""List tables and views.
487-
488-
::: {.callout-note}
489-
## Ibis does not use the word `schema` to refer to database hierarchy.
490-
491-
A collection of tables is referred to as a `database`.
492-
A collection of `database` is referred to as a `catalog`.
493-
494-
These terms are mapped onto the corresponding features in each
495-
backend (where available), regardless of whether the backend itself
496-
uses the same terminology.
497-
:::
498-
499-
Parameters
500-
----------
501-
like
502-
Regex to filter by table/view name.
503-
database
504-
Database location. If not passed, uses the current database.
505-
506-
By default uses the current `database` (`self.current_database`) and
507-
`catalog` (`self.current_catalog`).
508-
509-
To specify a table in a separate catalog, you can pass in the
510-
catalog and database as a string `"catalog.database"`, or as a tuple of
511-
strings `("catalog", "database")`.
512-
513-
Returns
514-
-------
515-
list[str]
516-
List of table and view names.
517-
518-
Examples
519-
--------
520-
>>> import ibis
521-
>>> con = ibis.athena.connect()
522-
>>> foo = con.create_table("foo", schema=ibis.schema(dict(a="int")))
523-
>>> con.list_tables()
524-
['foo']
525-
>>> bar = con.create_view("bar", foo)
526-
>>> con.list_tables()
527-
['bar', 'foo']
528-
>>> con.create_database("my_database")
529-
>>> con.list_tables(database="my_database")
530-
[]
531-
>>> con.raw_sql("CREATE TABLE my_database.baz (a INTEGER)") # doctest: +ELLIPSIS
532-
<... object at 0x...>
533-
>>> con.list_tables(database="my_database")
534-
['baz']
535-
536-
"""
537458
table_loc = self._to_sqlglot_table(database)
538459

539460
catalog = table_loc.catalog or self.current_catalog

0 commit comments

Comments
 (0)