Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

21.2 KB
Raw Blame
from __future__ import annotations

from collections.abc import Collection
from dataclasses import field
from typing import TYPE_CHECKING, Any, cast

import structlog
from prometheus_client import Counter
from rest_framework import status

from posthog.dataclasses import frozen

from products.managed_warehouse.backend.facade.contracts import TrinoCompiledQuery, TrinoExpansionMode
from products.managed_warehouse.backend.facade.cp_teams import get_org_team_membership
from products.managed_warehouse.backend.table_binding import build_trino_table_locators

if TYPE_CHECKING:
    from collections.abc import Callable

    from posthog.schema import HogQLQuery

    from posthog.hogql import ast
    from posthog.hogql.constants import LimitContext
    from posthog.hogql.transforms.trino.manifest import PreparedTrinoCatalog, TrinoCatalogManifest

    from posthog.models import Team, User

    from products.access_control.backend.facade.user_access_control import UserAccessControl

logger = structlog.get_logger(__name__)

TRINO_COMPILATION_TOTAL = Counter(
    "managed_warehouse_trino_compilation_total",
    "Managed warehouse Trino compilations by semantic expansion mode.",
    labelnames=["mode"],
)
TRINO_PURE_REJECTION_TOTAL = Counter(
    "managed_warehouse_trino_pure_rejection_total",
    "Pure managed warehouse Trino compilation rejections by stable feature code.",
    labelnames=["feature_code"],
)


class TrinoTargetUnavailable(RuntimeError):
    pass


@frozen
class PreparedTrinoCompiler:
    """A validated managed-warehouse target with reusable pure catalog metadata."""

    team_id: int
    organization_id: str
    catalog_name: str
    _catalog: PreparedTrinoCatalog = field(repr=False, compare=False)

    def compile(self, query: HogQLQuery, *, include_hogql: bool = False) -> TrinoCompiledQuery:
        from posthog.hogql.transforms.trino.errors import TrinoLoweringError  # noqa: PLC0415

        TRINO_COMPILATION_TOTAL.labels(mode=TrinoExpansionMode.PURE.value).inc()
        try:
            transpiled = self._catalog.transpile(
                query.query,
                values=query.values,
                filters=query.filters,
                variables=query.variables,
                modifiers=query.modifiers,
                limit_top_select=False,
                include_hogql=include_hogql,
            )
        except TrinoLoweringError as error:
            TRINO_PURE_REJECTION_TOTAL.labels(feature_code=error.feature_code).inc()
            raise
        return TrinoCompiledQuery(sql=transpiled.sql, values=transpiled.values, hogql=transpiled.hogql)


def _reject_pure_compilation(error: Exception, *, feature_code: str) -> None:
    TRINO_PURE_REJECTION_TOTAL.labels(feature_code=feature_code).inc()
    raise error


def get_ready_trino_catalog_name(organization_id: str) -> str | None:
    """Return the control-plane-owned catalog only after its Trino target is ready."""
    from products.managed_warehouse.backend.presentation.views import _request  # noqa: PLC0415

    response = _request("GET", organization_id, "/trino", require_enabled=False)
    readiness_logger = logger.bind(organization_id=str(organization_id), status_code=response.status_code)
    if not status.is_success(response.status_code):
        readiness_logger.warning("trino_target_not_ready", reason="http_error")
        return None
    if not isinstance(response.data, dict):
        readiness_logger.warning(
            "trino_target_not_ready", reason="invalid_response", response_type=type(response.data).__name__
        )
        return None
    enabled = response.data.get("enabled")
    if not isinstance(enabled, bool):
        readiness_logger.warning(
            "trino_target_not_ready", reason="invalid_enabled", enabled_type=type(enabled).__name__
        )
        return None
    if not enabled:
        readiness_logger.info(
            "trino_target_not_ready",
            reason="not_enabled",
            enabled=enabled,
            enabled_type=type(enabled).__name__,
        )
        return None

    trino_status = response.data.get("status")
    if not isinstance(trino_status, dict):
        readiness_logger.warning(
            "trino_target_not_ready", reason="invalid_status", status_type=type(trino_status).__name__
        )
        return None
    state = trino_status.get("state")
    if state != "ready":
        readiness_logger.info(
            "trino_target_not_ready",
            reason="state_not_ready",
            state=state[:128] if isinstance(state, str) else None,
            state_type=type(state).__name__,
        )
        return None
    response_org = trino_status.get("org")
    if response_org is not None and str(response_org) != str(organization_id):
        readiness_logger.warning(
            "refusing_trino_catalog_for_mismatched_organization",
            reason="organization_mismatch",
            requested_organization_id=str(organization_id),
            response_organization_id=response_org[:128] if isinstance(response_org, str) else None,
            response_organization_id_type=type(response_org).__name__,
        )
        return None

    catalog_name = trino_status.get("trino_catalog_name") or trino_status.get("catalog")
    if not isinstance(catalog_name, str) or not catalog_name.strip():
        readiness_logger.warning(
            "trino_target_not_ready", reason="invalid_catalog", catalog_type=type(catalog_name).__name__
        )
        return None
    return catalog_name.strip()


def prepare_hogql_to_trino_compiler(
    team_id: int,
    *,
    team: Team | None = None,
    catalog_manifest: TrinoCatalogManifest | None = None,
) -> PreparedTrinoCompiler:
    from posthog.hogql.transforms.trino.errors import TrinoLoweringError  # noqa: PLC0415
    from posthog.hogql.transforms.trino.manifest import (  # noqa: PLC0415
        TrinoCatalogManifest,
        TrinoManifestTable,
        prepare_trino_catalog,
    )

    from posthog.models.team.team import Team  # noqa: PLC0415
    from posthog.week_start_day import WeekStartDay  # noqa: PLC0415

    team = team or Team.objects.get(pk=team_id)
    if team.pk != team_id:
        raise TrinoTargetUnavailable("The provided team does not match the requested project")

    organization_id = str(team.organization_id)
    catalog_name = get_ready_trino_catalog_name(organization_id)
    if catalog_name is None:
        raise TrinoTargetUnavailable("The organization does not have a ready Trino catalog")

    membership = get_org_team_membership(organization_id, team_id)
    if membership is None:
        raise TrinoTargetUnavailable("The project does not have an authoritative physical table mapping")
    if membership.team_id != team_id or str(membership.organization_id) != organization_id:
        raise TrinoTargetUnavailable("The physical table mapping does not match the requested project")

    additional_tables = catalog_manifest.tables if catalog_manifest is not None else ()
    if any(table.logical_name in {"events", "persons", "groups", "raw_groups"} for table in additional_tables):
        _reject_pure_compilation(
            TrinoLoweringError(
                "TRINO_PURE_CORE_TABLE_OVERRIDE",
                "core table manifest",
                detail="Managed warehouse compilation owns the core table physical mappings.",
            ),
            feature_code="TRINO_PURE_CORE_TABLE_OVERRIDE",
        )
    groups_tables = (
        tuple(
            TrinoManifestTable(
                logical_name=logical_name,
                locator=(catalog_name, "posthog", membership.table_names.groups_table),
            )
            for logical_name in ("groups", "raw_groups")
        )
        if membership.table_names.groups_table is not None
        else ()
    )
    pure_manifest = TrinoCatalogManifest(
        tables=(
            TrinoManifestTable(
                logical_name="events",
                locator=(catalog_name, "posthog", membership.table_names.events_table),
            ),
            TrinoManifestTable(
                logical_name="persons",
                locator=(catalog_name, "posthog", membership.table_names.persons_table),
            ),
            *groups_tables,
            *additional_tables,
        ),
        timezone=team.timezone,
        week_start_day=team.week_start_day or WeekStartDay.SUNDAY,
    )
    return PreparedTrinoCompiler(
        team_id=team_id,
        organization_id=organization_id,
        catalog_name=catalog_name,
        _catalog=prepare_trino_catalog(pure_manifest),
    )


def compile_hogql_to_trino_sql(
    team_id: int,
    query: HogQLQuery,
    *,
    team: Team | None = None,
    user: User | None = None,
    user_access_control: UserAccessControl | None = None,
    limit_top_select: bool = False,
    limit_context: LimitContext | None = None,
    bypass_warehouse_access_control: bool = False,
    allowed_system_tables: Collection[str] | None = None,
    include_hogql: bool = False,
    expansion_mode: TrinoExpansionMode = TrinoExpansionMode.PURE,
    catalog_manifest: TrinoCatalogManifest | None = None,
    select_transform: Callable[[ast.SelectQuery | ast.SelectSetQuery], ast.SelectQuery | ast.SelectSetQuery]
    | None = None,
    preserve_clickhouse_output_names: bool = False,
) -> TrinoCompiledQuery:
    """Compile HogQL for the ready Trino catalog that serves the team's DuckLake data.

    Set ``bypass_warehouse_access_control`` only for trusted internal callers that compile
    without a user. ``allowed_system_tables`` exposes those scoped system tables to a userless
    Django-mode compilation. This entry point does not execute the returned SQL or alter query routing.
    Set ``include_hogql`` to render normalized HogQL for diagnostics. Pure manifest-backed
    compilation is the default; select ``TrinoExpansionMode.DJANGO`` only when the query needs
    actions, cohorts, saved queries, variables, filters, or other Django-backed semantics.
    ``select_transform`` rewrites the parsed query after placeholders are replaced, so it sees the
    same AST the printer does. It needs ``TrinoExpansionMode.DJANGO``.
    ``preserve_clickhouse_output_names`` resolves the query locally for both dialects and gives the
    Trino output the names that ClickHouse derives. It does not execute a ClickHouse query.
    """
    if expansion_mode == TrinoExpansionMode.PURE:
        if select_transform is not None or preserve_clickhouse_output_names:
            raise ValueError("A select transform or ClickHouse output names need Django expansion")
        return prepare_hogql_to_trino_compiler(
            team_id,
            team=team,
            catalog_manifest=catalog_manifest,
        ).compile(query, include_hogql=include_hogql)

    from posthog.hogql import ast  # noqa: PLC0415
    from posthog.hogql.context import HogQLContext  # noqa: PLC0415
    from posthog.hogql.database.database import Database  # noqa: PLC0415
    from posthog.hogql.filters import replace_filters  # noqa: PLC0415
    from posthog.hogql.modifiers import create_default_modifiers_for_team  # noqa: PLC0415
    from posthog.hogql.parser import parse_select, sanitize_client_parser_mode  # noqa: PLC0415
    from posthog.hogql.placeholders import find_placeholders, replace_placeholders  # noqa: PLC0415
    from posthog.hogql.printer.clickhouse import ClickHousePrinter  # noqa: PLC0415
    from posthog.hogql.printer.trino_hogql import (
        TrinoHogQLPrinter,  # noqa: PLC0415 -- keeps compiler imports off Django startup
    )
    from posthog.hogql.printer.utils import prepare_and_print_ast, prepare_ast_for_printing  # noqa: PLC0415
    from posthog.hogql.resolver import resolve_types  # noqa: PLC0415 -- keeps compiler imports off Django startup
    from posthog.hogql.variables import replace_variables  # noqa: PLC0415
    from posthog.hogql.visitor import clone_expr  # noqa: PLC0415

    from posthog.models.team.team import Team  # noqa: PLC0415

    team = team or Team.objects.get(pk=team_id)
    organization_id = str(team.organization_id)
    catalog_name = get_ready_trino_catalog_name(organization_id)
    if catalog_name is None:
        raise TrinoTargetUnavailable("The organization does not have a ready Trino catalog")

    membership = get_org_team_membership(organization_id, team_id)
    if membership is None:
        raise TrinoTargetUnavailable("The project does not have an authoritative physical table mapping")

    TRINO_COMPILATION_TOTAL.labels(mode=expansion_mode.value).inc()
    if expansion_mode != TrinoExpansionMode.DJANGO:
        raise ValueError(f"Unknown Trino expansion mode: {expansion_mode}")

    query_modifiers = create_default_modifiers_for_team(team, query.modifiers)
    placeholder_values: dict[str, ast.Expr] | None = (
        {key: ast.Constant(value=value) for key, value in query.values.items()} if query.values else None
    )
    parsed = parse_select(
        query.query,
        placeholders=placeholder_values,
        parser_mode=sanitize_client_parser_mode(query_modifiers.parserMode),
    )

    database = Database.create_for(
        team_id,
        team=team,
        user=user,
        user_access_control=user_access_control,
        modifiers=query_modifiers,
        # Preserve an explicit trusted-internal bypass; the public default remains fail-closed.
        bypass_warehouse_access_control=bypass_warehouse_access_control,
        allowed_system_tables=allowed_system_tables,
        trigger="trino",
    )
    placeholders = find_placeholders(parsed)
    if placeholders.has_filters:
        parsed = replace_filters(parsed, query.filters, team, database=database)
    if query.variables:
        parsed = replace_variables(parsed, list(query.variables.values()), team)
    if placeholders.placeholder_fields or placeholders.placeholder_expressions:
        variables: dict[str, Any] = {}
        replacements: dict[str, Any] = {"variables": variables, **(placeholder_values or {})}
        if query.variables:
            for variable in query.variables.values():
                variables[variable.code_name] = variable.value
        parsed = cast("ast.SelectQuery | ast.SelectSetQuery", replace_placeholders(parsed, replacements))
    if select_transform is not None:
        parsed = select_transform(parsed)

    clickhouse_output_names: list[str] | None = None
    if preserve_clickhouse_output_names:
        clickhouse_context = HogQLContext(
            team_id=team_id,
            team=team,
            user=user,
            user_access_control=user_access_control,
            enable_select_queries=True,
            limit_top_select=False,
            modifiers=query_modifiers.model_copy(deep=True),
            bypass_warehouse_access_control=bypass_warehouse_access_control,
            database=database,
        )
        clickhouse_ast = prepare_ast_for_printing(clone_expr(parsed), clickhouse_context, dialect="clickhouse")
        if clickhouse_ast is None:
            raise ValueError("ClickHouse preparation returned an empty query")

        clickhouse_output = clickhouse_ast
        while isinstance(clickhouse_output, ast.SelectSetQuery):
            clickhouse_output = clickhouse_output.initial_select_query
        clickhouse_output_names = ClickHousePrinter(
            context=clickhouse_context, stack=[clickhouse_output]
        ).select_column_names(clickhouse_output)

    trino_table_locators = build_trino_table_locators(
        database,
        team_id,
        catalog_name=catalog_name,
        table_names=membership.table_names,
    )
    trino_context = HogQLContext(
        team_id=team_id,
        team=team,
        user=user,
        user_access_control=user_access_control,
        enable_select_queries=True,
        limit_top_select=limit_top_select,
        limit_context=limit_context,
        modifiers=query_modifiers,
        # Match the access-controlled database above instead of widening it during resolution.
        bypass_warehouse_access_control=bypass_warehouse_access_control,
        database=database,
        trino_table_locators=trino_table_locators,
    )
    hogql_ast = clone_expr(parsed) if include_hogql else None

    def preserve_output_names(prepared: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
        if clickhouse_output_names is None:
            return prepared

        output_types: list[ast.SelectSetQueryType] = []
        trino_output = prepared
        while isinstance(trino_output, ast.SelectSetQuery):
            if isinstance(trino_output.type, ast.SelectSetQueryType):
                output_types.append(trino_output.type)
            trino_output = trino_output.initial_select_query

        if len(trino_output.select) != len(clickhouse_output_names):
            raise ValueError(
                "ClickHouse and Trino preparation produced different output column counts "
                f"({len(clickhouse_output_names)} and {len(trino_output.select)})"
            )

        named_columns: dict[str, ast.Type] = {}
        named_select: list[ast.Expr] = []
        for column, name in zip(trino_output.select, clickhouse_output_names):
            expression = column.expr if isinstance(column, ast.Alias) else column
            column_type = column.type.type if isinstance(column.type, ast.FieldAliasType) else column.type
            alias_type = ast.FieldAliasType(alias=name, type=column_type or ast.UnknownType())
            named_select.append(ast.Alias(alias=name, expr=expression, type=alias_type))
            named_columns[name] = alias_type
        trino_output.select = named_select
        if isinstance(trino_output.type, ast.SelectQueryType):
            trino_output.type.columns = named_columns
        for output_type in output_types:
            output_type.columns = named_columns

        # The output can read a derived table by column name, as the incremental guard does. Trino
        # does not name an unaliased expression, so give each one the name the resolver referenced.
        derived = trino_output.select_from.table if trino_output.select_from is not None else None
        while isinstance(derived, ast.SelectSetQuery):
            derived = derived.initial_select_query
        if isinstance(derived, ast.SelectQuery) and isinstance(derived.type, ast.SelectQueryType):
            resolved_names = {id(column_type): name for name, column_type in derived.type.columns.items()}
            derived.select = [
                ast.Alias(
                    alias=resolved_names[id(column.type)],
                    expr=column,
                    type=ast.FieldAliasType(alias=resolved_names[id(column.type)], type=column.type),
                )
                if not isinstance(column, ast.Alias) and column.type is not None and id(column.type) in resolved_names
                else column
                for column in derived.select
            ]
        return prepared

    needs_row_type_probe = True

    def prepare_output(prepared: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
        nonlocal needs_row_type_probe
        output = prepared
        while isinstance(output, ast.SelectSetQuery):
            output = output.initial_select_query
        needs_row_type_probe = any(_may_hold_anonymous_row(column, trino_context) for column in output.select)
        return preserve_output_names(prepared) if preserve_clickhouse_output_names else prepared

    trino_sql, _ = prepare_and_print_ast(
        parsed,
        trino_context,
        dialect="trino",
        prepared_ast_transform=prepare_output,
    )

    hogql_pretty: str | None = None
    if hogql_ast is not None:
        hogql_context = HogQLContext(
            team_id=team_id,
            team=team,
            user=user,
            user_access_control=user_access_control,
            enable_select_queries=True,
            limit_top_select=limit_top_select,
            limit_context=limit_context,
            modifiers=query_modifiers,
            bypass_warehouse_access_control=bypass_warehouse_access_control,
            database=database,
            trino_table_locators=trino_table_locators,
            restricted_properties=trino_context.restricted_properties,
        )
        resolved_hogql = resolve_types(hogql_ast, hogql_context, dialect="trino")
        hogql_pretty = TrinoHogQLPrinter(context=hogql_context).visit(resolved_hogql)

    return TrinoCompiledQuery(
        sql=trino_sql,
        values=dict(trino_context.values),
        hogql=hogql_pretty,
        needs_row_type_probe=needs_row_type_probe,
    )


def _may_hold_anonymous_row(column: Any, context: Any) -> bool:
    """Whether the resolved type of an output column is, or contains, a tuple or an unknown type."""
    from posthog.hogql import ast  # noqa: PLC0415 -- keeps compiler imports off Django startup

    def check(constant_type: ast.ConstantType) -> bool:
        if isinstance(constant_type, ast.TupleType | ast.UnknownType):
            return True
        if isinstance(constant_type, ast.ArrayType):
            return check(constant_type.item_type)
        if isinstance(constant_type, ast.MapType):
            return check(constant_type.key_type) or check(constant_type.value_type)
        if isinstance(constant_type, ast.AggregateStateType):
            return check(constant_type.wrapped_type)
        return False

    if column.type is None:
        return True
    try:
        return check(column.type.resolve_constant_type(context))
    except Exception:  # noqa: BLE001 -- a type that cannot be resolved is probed, which is always safe
        return True