# Execution (/docs/python/datasets/execution)

`create_dataset_client` is the entry point for running semantic queries. It plans each query, hands the compiled
statement to an executor, and returns rows keyed by column name.

<CodeBlock>
```python
from hypequery.datasets import create_dataset_client
from hypequery.execution import ClickHouseConnection, create_clickhouse_executor

# Reads CLICKHOUSE_URL, or CLICKHOUSE_HOST, _PORT, _DATABASE, _USERNAME, _PASSWORD and
# _SECURE; or pass host=..., username=..., password=..., database=... explicitly.
executor = create_clickhouse_executor(ClickHouseConnection.from_env())
analytics = create_dataset_client(executor=executor)
```
</CodeBlock>

The client does not own the executor, so call `executor.close()` when your application shuts down. The factory
connects as soon as it is called, so create the executor in your app's startup rather than at import time if the app
must start while ClickHouse is down. In a FastAPI app, close it from the app's lifespan. For async code, use
`create_async_dataset_client` with an async executor and `await analytics.execute(...)`.

In Django and Flask, let the [Django](/docs/python/django) or [Flask](/docs/python/flask) integration own the client:
those frameworks have no shutdown hook, and the integrations create, close and tenant-bind it for you.

## Connecting [#connecting]

<CodeBlock>
```python
ClickHouseConnection(host="ch.internal", database="analytics", username="reader", password=password)
ClickHouseConnection.from_url("clickhouses://reader:pw@ch.example.com/analytics")
ClickHouseConnection.from_env()
```
</CodeBlock>

| URL | Connects with |
| --- | --- |
| `clickhouse://`, `http://` | Plain HTTP (port 8123 by default) |
| `clickhouses://`, `https://` | TLS (port 8443 by default) |
| `clickhousedb://`, `clickhouse+http://` | SQLAlchemy URLs for the HTTP interface; `?secure=true` or `?protocol=https` for TLS |

The path is the database (`default` when omitted), and the user and password are percent-decoded. Native-protocol URLs
such as `clickhouse+native://` are refused: hypequery sends values as server-side parameters, which only the HTTP
interface carries. No error message repeats the URL.

`from_env()` reads `CLICKHOUSE_URL` when it is set, and `CLICKHOUSE_HOST`, `_PORT`, `_DATABASE`, `_USERNAME`,
`_PASSWORD` and `_SECURE` otherwise. Setting both is an error. To reuse a SQLAlchemy engine's server, see
[SQLAlchemy](/docs/python/sqlalchemy).

## Client methods [#client-methods]

| Method | Use it for |
| --- | --- |
| `analytics.validate(target, query, context=...)` | Check a query without executing it |
| `analytics.to_sql(target, query, context=...)` | Inspect a redacted debug statement |
| `analytics.execute(target, query, context=...)` | Plan, execute, and return rows |
| `analytics.stream(target, query, context=...)` | Plan and execute, receiving rows in batches as they arrive |
| `analytics.stream_arrow(target, query, context=...)` | Plan and execute, receiving Arrow record batches with native types |
| `analytics.for_tenant(scope)` | Get a client bound to one tenant |

A target is a `Dataset` or the name of a registered dataset. A query is a `DatasetQuery` or a plain mapping,
validated strictly with unknown keys rejected. A mapping uses the Python field names, so write `order_by`, not the
HTTP body's `orderBy`.

`DatasetQuery(timezone="Europe/Berlin")` sets the zone for time buckets and offset-free time-key bounds for one query;
`create_dataset_client(timezone=...)` sets the default, `UTC` unless given.

## Validate [#validate]

<CodeBlock>
```python
from hypequery.datasets import DatasetQuery, filter, order

validation = analytics.validate(
    orders,
    DatasetQuery(dimensions=("country",), filters=(filter.eq("status", "completed"),)),
)

if not validation.valid:
    print(validation.errors)
```
</CodeBlock>

## Inspect SQL [#inspect-sql]

<CodeBlock>
```python
sql = analytics.to_sql(
    orders,
    DatasetQuery(
        dimensions=("country",),
        measures=("revenue",),
        filters=(filter.eq("status", "completed"),),
        order_by=(order.desc("revenue"),),
        limit=10,
    ),
)
```
</CodeBlock>

The statement has placeholders instead of values and cannot be executed.

## Results [#results]

`execute` returns a result with `data` (rows keyed by column name), `columns`, and `meta` with `query_id`,
`row_count`, `timing_ms`, and `cache`.

`meta.pagination` is `None` unless you pass `paginate=True`:

<CodeBlock>
```python
page = analytics.execute(orders, DatasetQuery(dimensions=("country",), limit=50, offset=100), paginate=True)
page.meta.pagination  # Pagination(limit=50, offset=100, has_more=True)
```
</CodeBlock>

The client fetches one extra row to compute `has_more` and drops it from `data`. There is no total row count.

Value types differ from the HTTP wire, where every measure is a string, and so is a dimension integer beyond ±(2^53 − 1):
- integer counts come back as `int`;
- floating-point results come back as `float`;
- `Decimal` values come back as `str`, to keep their precision;
- timestamps come back as ISO 8601 strings.

For analysis, `result.to_pandas()`, `to_polars()` and `to_arrow()` restore real types from the dataset definition. See
[pandas, Polars and Arrow](/docs/python/dataframes).

## Stream large results [#stream-large-results]

`execute` holds the whole result in memory. For exports, notebooks and wide breakdowns, `stream` hands you the rows in
batches as the executor receives them, so memory stays flat however many rows there are.

<CodeBlock>
```python
import json

query = DatasetQuery(dimensions=("customer_id", "country"), measures=("revenue", "orders"))

with analytics.stream(orders, query, context=ExecutionContext(tenant=tenant("acme"))) as stream:
    with open("orders.ndjson", "w") as out:
        for batch in stream:
            for row in batch:
                out.write(json.dumps(row) + "\n")
```
</CodeBlock>

Each batch is a tuple of the rows `execute` would return, with the same values, so a stream's rows equal `execute`'s
`data`. `stream.rows()` iterates them one at a time across batches, and `stream.columns`, `column_types` and
`query_id` are known before the first batch.

`stream_arrow` hands the result over as `pyarrow.RecordBatch`es instead. Columns keep their ClickHouse types: a
`Decimal` measure is `decimal128` and a count `uint64`, not the JSON-safe values of a row stream. It needs
`hypequery[arrow]`:

<CodeBlock>
```python
import duckdb

with analytics.stream_arrow(orders, query, context=context) as stream:
    stream.schema                      # known before the first batch
    reader = stream.to_reader()        # a pyarrow.RecordBatchReader
    duckdb.sql("SELECT country, sum(revenue) FROM reader GROUP BY ALL").show()
```
</CodeBlock>

`read_all()` collects the batches into one `pyarrow.Table`, and `pl.from_arrow(stream.read_all())` gives a polars
frame. On the async client, enter the stream with `async with` and iterate with `async for`:

<CodeBlock>
```python
async with analytics.stream(orders, query, context=context) as stream:
    async for batch in stream:
        ...
```
</CodeBlock>

A stream runs exactly the query `execute` would: the same planning, tenant scope, settings, deadline and cancellation,
without the result cache or pagination. The query is planned when you call `stream`, so an invalid query raises there;
it starts when you enter the `with` block. Leaving the block early stops it, with `KILL QUERY` where the executor has
a control connection.

The client's settings bound the whole stream. A result that grows past `max_result_rows`, or a query that runs past
`max_execution_time`, fails with `too-large` or `deadline-exceeded` partway through rather than ending short. A stream's
memory stays flat, so the byte limit is its own: `stream_max_result_bytes` when you pass it (at most 1 TiB). Without
it, a stream keeps a `max_result_bytes` you set in `settings`, and runs under 1 TiB in place of the default.

A dataset with limits caps a stream's rows too. Its query runs with your `limit`, or, when it sets none, with the
dataset's ceiling as its `LIMIT`: `max_stream_rows` if set, else `max_result_size`. That stream ends at the ceiling
without an error, so give an export an explicit `limit`, or a `max_stream_rows` above its largest result and a
`max_result_rows` at least as high. `max_stream_rows` lets streams return more rows than pages; `max_result_size` keeps
bounding pages and agents:

<CodeBlock>
```python
from hypequery.datasets import DatasetLimits
from hypequery.datasets.planner import query_settings

orders = dataset(
    "orders",
    source="orders",
    dimensions={"country": dimension.string()},
    measures={"revenue": measure.sum("amount")},
    limits=DatasetLimits(max_result_size=1_000, max_stream_rows=5_000_000),
)

# Rows and time bound exports; the defaults (100,000 rows, 30 s) suit interactive queries.
exports = create_dataset_client(
    executor=executor,
    settings=query_settings(max_result_rows=10_000_000, max_execution_time=3600),
)
```
</CodeBlock>

Every built-in executor streams. A stream holds its executor's admission while it is open: an async executor's slot,
or a chDB connection, which runs one query at a time, so other queries on that executor wait until the stream closes.