"""Thin helper that opens an in-memory DuckDB connection scoped to the
sample pipeline's Parquet root."""

from __future__ import annotations

import duckdb
from pathlib import Path

# Resolved relative to this file: ../data/parquet
PARQUET_ROOT = (Path(__file__).resolve().parent.parent / "data" / "parquet").as_posix()


def open_pipeline_db() -> duckdb.DuckDBPyConnection:
    """Return an in-memory DuckDB connection with an ``events`` view over
    the L02 Parquet partitions.

    The view is partition-aware: DuckDB pushes down date filters into
    the Parquet partition discovery so a ``WHERE date = '...'`` clause
    only touches the relevant files.
    """
    con = duckdb.connect(":memory:")
    con.execute(
        f"""
        CREATE OR REPLACE VIEW events AS
        SELECT *
        FROM read_parquet('{PARQUET_ROOT}/date=*/*.parquet',
                          hive_partitioning = true);
        """
    )
    return con


if __name__ == "__main__":
    with open_pipeline_db() as con:
        row = con.execute("SELECT count(*) AS n FROM events").fetchone()
        print(f"events row count = {row[0]}")
