dlt.dataset.relation
Relation Objects
class Relation(WithSqlClient)
__init__
def __init__(*,
dataset: dlt.Dataset,
query: Optional[Union[str, sge.Query, IbisExpr]] = None,
query_dialect: Optional[str] = None,
table_name: Optional[str] = None,
_execute_raw_query: bool = False) -> None
Create a lazy evaluated relation for the dataset of a destination
columns_schema
@property
def columns_schema() -> TTableSchemaColumns
dlt columns schema. Convenience method for dlt.schema["columns"]
schema
@property
def schema() -> TTableSchema
dlt table schema associated with the relation.
This infers the schema from the relation's content. It's likely to include less information than retrieving the schema from the pipeline or the dataset if the table already exists.
schema
@schema.setter
def schema(new_value: Any) -> None
Disable schema setter.
columns
@property
def columns() -> list[str]
List of column names found on the table.
_ipython_key_completions_
def _ipython_key_completions_() -> list[str]
Provide column names as completion suggestion in interactive environments.
sqlglot_expression
@property
def sqlglot_expression() -> sge.Query
SQLGlot expression
to_sql
def to_sql(pretty: bool = False, *, _raw_query: bool = False) -> str
Get the normalize query string in the correct sql dialect for this relation
destination_dialect
@property
def destination_dialect() -> TSqlGlotDialect
SQLGlot dialect used by the destination.
This is the target dialect when transpiling SQL queries.
to_ibis
def to_ibis() -> ir.Table
Create an ibis.Table expression from the current relation.
If the dlt.Relation was initialized with a table_name, it will return an
ibis.Table directly. If the dlt.Relation was transformed via .where(), .select(),
etc., it will apply the operations in a single step as an opaque SQLQuery Ibis operation.
limit
def limit(limit: int) -> Self
Create a Relation using a LIMIT clause.
head
def head(limit: int = 5) -> Self
Create a Relation using a LIMIT clause. Defaults to limit=5
This proxies Relation.limit().
select
def select(*columns: str) -> Self
CReate a Relation with the selected columns using a SELECT clause.
order_by
def order_by(column_name: str, direction: TSortOrder = "asc") -> Self
Create a Relation ordering results using a ORDER BY clause.
Arguments:
column_namestr - The column to order by.directionTSortOrder, optional - The direction to order by: "asc"/"desc". Defaults to "asc".
Returns:
Self- A new Relation with theORDER BYclause applied.
max
def max() -> Self
Create a Relation with the MAX aggregate applied.
Exactly one column must be selected.
min
def min() -> Self
Create a Relation with the MIN aggregate applied.
Exactly one column must be selected.
where
def where(column_or_expr: SqlglotExprOrStr,
operator: Optional[TFilterOperation] = None,
value: Optional[Any] = None) -> Self
Create a Relation filtering results using a WHERE clause.
This is identical to Relation.filter().
Arguments:
column_namestr - The column to filter on.operatorTFilterOperation - The operator to use. Available operations are: eq, ne, gt, lt, gte, lte, in, not_invalueAny - The value to filter on.
Returns:
Self- A new Relation with the WHERE clause applied.
filter
def filter(column_or_expr: SqlglotExprOrStr,
operator: Optional[TFilterOperation] = None,
value: Optional[Any] = None) -> Self
Create a Relation filtering results using a WHERE clause.
This is identical to Relation.where().
Arguments:
column_namestr - The column to filter on.operatorTFilterOperation - The operator to use. Available operations are: eq, ne, gt, lt, gte, lte, in, not_invalueAny - The value to filter on.
Returns:
Self- A new Relation with the WHERE clause applied.
fetchscalar
def fetchscalar() -> Any
Execute the relation and return the first value of first column as a Python primitive
__getitem__
def __getitem__(columns: Sequence[str]) -> Self
Create a new Relation with the specified columns selected.
This proxies Relation.select().