Skip to main content
Version: devel

dlt.dataset.relation

Relation Objects

class Relation(WithSqlClient)

View source on GitHub

__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

View source on GitHub

Create a lazy evaluated relation for the dataset of a destination

columns_schema

@property
def columns_schema() -> TTableSchemaColumns

View source on GitHub

dlt columns schema. Convenience method for dlt.schema["columns"]

schema

@property
def schema() -> TTableSchema

View source on GitHub

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

View source on GitHub

Disable schema setter.

columns

@property
def columns() -> list[str]

View source on GitHub

List of column names found on the table.

_ipython_key_completions_

def _ipython_key_completions_() -> list[str]

View source on GitHub

Provide column names as completion suggestion in interactive environments.

sqlglot_expression

@property
def sqlglot_expression() -> sge.Query

View source on GitHub

SQLGlot expression

to_sql

def to_sql(pretty: bool = False, *, _raw_query: bool = False) -> str

View source on GitHub

Get the normalize query string in the correct sql dialect for this relation

destination_dialect

@property
def destination_dialect() -> TSqlGlotDialect

View source on GitHub

SQLGlot dialect used by the destination.

This is the target dialect when transpiling SQL queries.

limit

def limit(limit: int) -> Self

View source on GitHub

Create a Relation using a LIMIT clause.

def head(limit: int = 5) -> Self

View source on GitHub

Create a Relation using a LIMIT clause. Defaults to limit=5

This proxies Relation.limit().

select

def select(*columns: str) -> Self

View source on GitHub

CReate a Relation with the selected columns using a SELECT clause.

order_by

def order_by(column_name: str, direction: TSortOrder = "asc") -> Self

View source on GitHub

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 the ORDER BY clause applied.

max

def max() -> Self

View source on GitHub

Create a Relation with the MAX aggregate applied.

Exactly one column must be selected.

min

def min() -> Self

View source on GitHub

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

View source on GitHub

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_in
  • value 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

View source on GitHub

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_in
  • value Any - The value to filter on.

Returns:

  • Self - A new Relation with the WHERE clause applied.

fetchscalar

def fetchscalar() -> Any

View source on GitHub

Execute the relation and return the first value of first column as a Python primitive

__getitem__

def __getitem__(columns: Sequence[str]) -> Self

View source on GitHub

Create a new Relation with the specified columns selected.

This proxies Relation.select().

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.