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.
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_name
str - The column to order by.direction
TSortOrder, optional - The direction to order by: "asc"/"desc". Defaults to "asc".
Returns:
Self
- A new Relation with theORDER BY
clause 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_name
str - The column to filter on.operator
TFilterOperation - The operator to use. Available operations are: eq, ne, gt, lt, gte, lte, in, not_invalue
Any - 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_name
str - The column to filter on.operator
TFilterOperation - The operator to use. Available operations are: eq, ne, gt, lt, gte, lte, in, not_invalue
Any - 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()
.