Skip to content

Query

Query(table, pipeline=list()) dataclass

A query builder for ClickHouse.

Parameters:

Name Type Description Default
table Table | View | str

The table to query.

required
pipeline list[str]

The pipeline of steps to execute.

list()

build()

Build the full PRQL query

Returns:

Type Description
str

The full PRQL query.

compile()

Compile the PRQL query to SQL

Returns:

Type Description
str

The compiled SQL query.

select(*args, **kwargs)

Select columns from the table.

Parameters:

Name Type Description Default
*args Any

Column names to select.

()
**kwargs Any

Column name-value pairs to select.

{}

Returns:

Type Description
Self

A new Query with the select step added.

derive(**kwargs)

Derive new columns from existing ones.

Parameters:

Name Type Description Default
**kwargs Any

Column name-expression pairs to derive.

{}

Returns:

Type Description
Self

A new Query with the derive step added.

filter(expression)

Filter the table by the given expression.

Parameters:

Name Type Description Default
expression Any

The expression to filter by.

required

Returns:

Type Description
Self

A new Query with the filter step added.

sort(*args)

Sort the table by the given columns.

Parameters:

Name Type Description Default
*args Any

Column names to sort by.

()

Returns:

Type Description
Self

A new Query with the sort step added.

aggregate(*args, **kwargs)

Aggregate the table by the given columns.

Parameters:

Name Type Description Default
*args Any

Column names to aggregate by.

()
**kwargs Any

Aggregate name-expression pairs.

{}

Returns:

Type Description
Self

A new Query with the aggregate step added.

group(*args, **kwargs)

Group the table by the given expressions and aggregates.

Parameters:

Name Type Description Default
*args Any

Expression, Aggregate, or Window to group.

()
**kwargs Any

Expression or Aggregate to group.

{}

Returns:

Type Description
Self

A new Query with the group step added.

take(n=None, *, start=None, end=None)

Take a subset of the table by row index.

Parameters:

Name Type Description Default
n int | None

Number of rows to take.

None
start int | None

Starting row index (inclusive).

None
end int | None

Ending row index (exclusive).

None

Returns:

Type Description
Self

A new Query with the take step added.

window(*args, **kwargs)

Create a window aggregation over the table.

Parameters:

Name Type Description Default
*args Any

Window to apply.

()
**kwargs Any

Aggregate to apply.

{}

Returns:

Type Description
Self

A new Query with the window step added.

join(rel, condition, side='inner')

Join the table with another table.

Parameters:

Name Type Description Default
rel Table | View | str

The table to join with.

required
condition Expression | str

The join condition expression.

required
side Literal['inner', 'left', 'right', 'full']

The type of join to perform. Defaults to "inner".

'inner'

Returns:

Type Description
Self

A new Query with the join step added.

exclude(*args)

Exclude columns from the table.

Exclude only works when there is a previous select/derive/group step that defines the columns to select.

Parameters:

Name Type Description Default
*args Any

Column names to exclude.

()

Returns:

Type Description
Self

A new Query with the exclude step added.