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.

6.1 KB
Raw Blame
from __future__ import annotations

from typing import TYPE_CHECKING

import posthoganalytics

from posthog.schema import HogQLQuery, HogQLQueryResponse

from posthog.hogql import ast
from posthog.hogql.constants import LimitContext
from posthog.hogql.direct_sql.trino_adapter import (
    DIRECT_TRINO_DEFAULT_STATEMENT_TIMEOUT_SECONDS,
    TrinoQueryRequest,
    ensure_read_only_raw_trino_statement,
    execute_trino_query,
)
from posthog.hogql.errors import ExposedHogQLError

from posthog.clickhouse.query_tagging import get_query_tags
from posthog.direct_query_cancellation import build_direct_query_cancellation_token
from posthog.hogql_queries.paginators import HogQLHasMorePaginator

from products.access_control.backend.facade.user_access_control import UserAccessControl
from products.managed_warehouse.backend.facade.access import enable_customer_warehouse_access
from products.managed_warehouse.backend.facade.contracts import TrinoExpansionMode
from products.managed_warehouse.backend.facade.feature_flags import MANAGED_TRINO_QUERY_FLAG
from products.managed_warehouse.backend.trino_compiler import TrinoTargetUnavailable, compile_hogql_to_trino_sql
from products.managed_warehouse.backend.trino_connection import connect_managed_warehouse_trino

if TYPE_CHECKING:
    from posthog.hogql.constants import HogQLGlobalSettings
    from posthog.hogql.timings import HogQLTimings

    from posthog.models import Team, User


def validate_managed_trino_raw_access(team: Team, user: User | None) -> None:
    if user is None or not user.is_authenticated:
        raise ExposedHogQLError("Sign in to run a raw query against hosted Trino.")
    # Token credentials can restrict projects while the warehouse grant spans the organization.
    if get_query_tags().access_method is not None:
        raise ExposedHogQLError(
            "Raw Trino queries require a browser session. Turn off Send raw query to use HogQL with a token."
        )
    if not UserAccessControl(user=user, team=team).has_project_access:
        raise ExposedHogQLError("You do not have access to this project.")


def validate_managed_trino_query(query: HogQLQuery, team: Team, user: User | None) -> None:
    if user is None or not user.is_authenticated:
        raise ExposedHogQLError("Sign in to run a query against hosted Trino.")
    org_id = str(team.organization_id)
    try:
        enabled = posthoganalytics.feature_enabled(
            MANAGED_TRINO_QUERY_FLAG,
            org_id,
            groups={"organization": org_id},
            group_properties={"organization": {"id": org_id}},
            only_evaluate_locally=False,
            send_feature_flag_events=False,
        )
    except Exception:
        enabled = False
    if not enable_customer_warehouse_access(team.organization_id, trino_enabled=bool(enabled)):
        raise ExposedHogQLError(
            "Hosted Trino queries are not enabled for this organization. Select PostHog to run this query."
        )
    if query.connectionId:
        raise ExposedHogQLError("Hosted Trino uses the managed warehouse. Clear the external connection.")
    if query.sendRawQuery:
        validate_managed_trino_raw_access(team, user)


def execute_managed_trino_query(
    query: HogQLQuery,
    *,
    team: Team,
    user: User | None,
    user_access_control: UserAccessControl | None,
    settings: HogQLGlobalSettings,
    timings: HogQLTimings,
    limit_context: LimitContext | None,
    canary: bool = False,
) -> HogQLQueryResponse:
    validate_managed_trino_query(query, team, user)
    assert user is not None
    paginator: HogQLHasMorePaginator | None = None

    def paginate(parsed: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
        nonlocal paginator
        paginator = HogQLHasMorePaginator.for_hogql_query(
            parsed, limit_context=limit_context or LimitContext.QUERY, offset=query.offset
        )
        return paginator.paginate(parsed) if paginator is not None else parsed

    if query.sendRawQuery:
        sql = query.query
        values = None
        hogql = None
    else:
        try:
            with timings.measure("trino_compile"):
                compiled = compile_hogql_to_trino_sql(
                    team.pk,
                    query,
                    team=team,
                    user=user,
                    user_access_control=user_access_control,
                    expansion_mode=TrinoExpansionMode.DJANGO,
                    include_hogql=True,
                    limit_top_select=True,
                    limit_context=limit_context,
                    select_transform=paginate,
                )
        except TrinoTargetUnavailable as error:
            raise ExposedHogQLError("Hosted Trino is not ready for this project. Try again later.") from error
        sql, values, hogql = compiled.sql, compiled.values, compiled.hogql
    tags = get_query_tags()
    cancellation_token = (
        build_direct_query_cancellation_token(tags.client_query_id, str(tags.celery_task_id))
        if tags.client_query_id is not None and tags.celery_task_id is not None
        else None
    )
    result = execute_trino_query(
        TrinoQueryRequest(
            sql=ensure_read_only_raw_trino_statement(sql),
            values=values,
            timeout_seconds=settings.max_execution_time or DIRECT_TRINO_DEFAULT_STATEMENT_TIMEOUT_SECONDS,
            timings=timings,
            team_id=team.pk,
            cancellation_token=cancellation_token,
        ),
        connect_managed_warehouse_trino(
            str(team.organization_id),
            principal=f"posthog:sql-editor:team:{team.pk}:user:{user.pk}",
            canary=canary,
        ),
    )
    response = HogQLQueryResponse(
        query=query.query,
        hogql=hogql,
        clickhouse=sql,
        results=result.results,
        columns=result.print_columns,
        types=result.types,
        error=result.error,
        timings=timings.to_list(),
        modifiers=query.modifiers,
    )
    if paginator is not None:
        paginator.response = response
        return response.model_copy(update={**paginator.response_params(), "results": paginator.trim_results()})
    return response