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.

fix(managed-warehouse): name unaliased columns in trino shadow builds (#114114)

posthog[bot]d805197eacd0
diff --git a/posthog/hogql/escape_sql.py b/posthog/hogql/escape_sql.py
index d16810aaf4b5..d6305143e426 100644
--- a/posthog/hogql/escape_sql.py
+++ b/posthog/hogql/escape_sql.py
@@ -277,7 +277,8 @@ def escape_duckdb_identifier(v: str) -> str:
def escape_trino_identifier(v: str) -> str:
- for character, label in (("%", "%"), ("?", "?"), ("\0", "NUL")):
+ # "?" is allowed because convert_pyformat_placeholders does not read inside quoted identifiers.
+ for character, label in (("%", "%"), ("\0", "NUL")):
if character in v:
raise QueryError(f'The Trino identifier "{v}" is not permitted as it contains the "{label}" character')
return '"' + v.replace('"', '""') + '"'
diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.py
index 92bb29e3e75d..dad4e439e57f 100644
--- a/posthog/hogql/printer/clickhouse.py
+++ b/posthog/hogql/printer/clickhouse.py
@@ -1,4 +1,5 @@
import re
+from collections.abc import Iterable
from datetime import date, datetime
from typing import TYPE_CHECKING, ClassVar, Optional, cast
from uuid import UUID
@@ -48,6 +49,7 @@ from posthog.hogql.restricted_properties import (
from posthog.hogql.type_system import parse_sql_runtime_type
from posthog.hogql.visitor import GetFieldsTraverser, clone_expr
+from posthog.clickhouse.client.escape import substitute_params
from posthog.clickhouse.events_json import TEMPORARY_PROPERTIES_COLUMN
from posthog.exchange_rate_constants import EXCHANGE_RATE_DECIMAL_PRECISION, EXCHANGE_RATE_DICTIONARY_NAME
from posthog.uuidt import UUIDT
@@ -1267,7 +1269,9 @@ class ClickHousePrinter(BasePrinter):
type=ast.BooleanType(),
)
- def _print_select_columns(self, columns):
+ def _prepare_select_columns(self, columns: Iterable[ast.Expr]) -> list[tuple[ast.Expr, str | None]]:
+ columns = list(columns)
+
def _alias_from_column_type(column: ast.Expr) -> str | None:
column_type = getattr(column, "type", None)
if isinstance(column_type, ast.FieldAliasType):
@@ -1285,7 +1289,7 @@ class ClickHousePrinter(BasePrinter):
if not found_aliases.get(alias.alias, None) or not alias.hidden:
found_aliases[alias.alias] = alias
- columns_sql = []
+ prepared_columns: list[tuple[ast.Expr, str | None]] = []
used_aliases: set[str] = set()
for column in columns:
printed_alias: str | None = None
@@ -1320,11 +1324,32 @@ class ClickHousePrinter(BasePrinter):
printed_alias = column_alias
else:
printed_alias = None
- columns_sql.append(self.visit(column))
+ prepared_columns.append((column, printed_alias))
if printed_alias is not None:
used_aliases.add(printed_alias)
- return columns_sql
+ return prepared_columns
+
+ def _print_select_columns(self, columns: Iterable[ast.Expr]) -> list[str]:
+ return [self.visit(column) for column, _ in self._prepare_select_columns(columns)]
+
+ def select_column_names(self, query: ast.SelectQuery) -> list[str]:
+ # ClickHouse names an unaliased expression after the SQL it receives, with bound values inlined.
+ names: list[str] = []
+ for column, _ in self._prepare_select_columns(query.select):
+ if isinstance(column, ast.Alias) and not column.hidden:
+ names.append(column.alias)
+ continue
+
+ rendered = (
+ f"({', '.join(self.visit(expression) for expression in column.exprs)})"
+ if isinstance(column, ast.Tuple)
+ else self.visit(column)
+ )
+ if re.search(r"%\([^)]*_sensitive\)s", rendered):
+ raise QueryError("An expression that reads sensitive source data needs an explicit alias")
+ names.append(substitute_params(rendered, self.context.values))
+ return names
def _get_extra_select_clauses(
self,
diff --git a/posthog/hogql/printer/test/test_trino_printer.py b/posthog/hogql/printer/test/test_trino_printer.py
index 32449094822b..97df6361055b 100644
--- a/posthog/hogql/printer/test/test_trino_printer.py
+++ b/posthog/hogql/printer/test/test_trino_printer.py
@@ -2349,7 +2349,7 @@ def test_rejects_pr_91053_shortcuts_without_matching_trino_semantics(expression:
assert error.value.feature_code == "TRINO_FUNCTION_UNSUPPORTED"
[email protected]("identifier", ["parameter?", "parameter%", "nul\0identifier"])
[email protected]("identifier", ["parameter%", "nul\0identifier"])
def test_rejects_trino_identifiers_that_can_collide_with_parameter_binding(identifier: str) -> None:
with pytest.raises(QueryError):
escape_trino_identifier(identifier)
diff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.py
index de97de08b17b..9d254d752e53 100644
--- a/posthog/hogql/printer/utils.py
+++ b/posthog/hogql/printer/utils.py
@@ -1,3 +1,4 @@
+from collections.abc import Callable
from typing import TYPE_CHECKING, cast
from posthog.schema_enums import InCohortVia, PropertyGroupsMode
@@ -97,6 +98,7 @@ def prepare_and_print_ast(
stack: list[ast.SelectQuery] | None = None,
settings: HogQLGlobalSettings | None = None,
pretty: bool = False,
+ prepared_ast_transform: Callable[[_T_AST], _T_AST] | None = None,
) -> tuple[str, _T_AST | None]:
previous_type_observability = context.type_observability
context.type_observability = create_hogql_type_observability(
@@ -116,6 +118,8 @@ def prepare_and_print_ast(
if context.type_observability is not None:
context.type_observability.result = "empty"
return "", None
+ if prepared_ast_transform is not None:
+ prepared_ast = prepared_ast_transform(prepared_ast)
if dialect == "trino":
from posthog.hogql.transforms.trino.transpiler import ( # noqa: PLC0415 -- load the optional backend only for Trino compilation
diff --git a/products/managed_warehouse/backend/tests/test_trino_compiler.py b/products/managed_warehouse/backend/tests/test_trino_compiler.py
index 658b4db83ff1..207f12f6c177 100644
--- a/products/managed_warehouse/backend/tests/test_trino_compiler.py
+++ b/products/managed_warehouse/backend/tests/test_trino_compiler.py
@@ -11,6 +11,7 @@ from structlog.testing import capture_logs
from posthog.schema import HogQLQuery
from posthog.hogql.constants import LimitContext
+from posthog.hogql.errors import QueryError
from posthog.hogql.transforms.trino.manifest import (
TrinoCatalogManifest,
TrinoManifestColumn,
@@ -34,7 +35,12 @@ from products.managed_warehouse.backend.trino_compiler import (
get_ready_trino_catalog_name,
prepare_hogql_to_trino_compiler,
)
-from products.warehouse_sources.backend.facade.models import DataWarehouseTable, ExternalDataSchema, ExternalDataSource
+from products.warehouse_sources.backend.facade.models import (
+ DataWarehouseCredential,
+ DataWarehouseTable,
+ ExternalDataSchema,
+ ExternalDataSource,
+)
from products.warehouse_sources.backend.facade.types import ExternalDataSourceType
@@ -201,6 +207,71 @@ class TestReadyTrinoCatalogName:
class TestCompileHogQLToTrinoSQL:
+ @pytest.mark.django_db
+ def test_requires_an_alias_before_a_source_credential_can_enter_an_output_name(self) -> None:
+ organization = Organization.objects.create(name="trino-sensitive-output-name")
+ team = Team.objects.create(organization=organization)
+ membership = _membership(team_id=team.pk, organization_id=str(organization.pk))
+ access_key = "example-access-key"
+ access_secret = "example-access-secret"
+ credential = DataWarehouseCredential.objects.create(
+ team=team,
+ access_key=access_key,
+ access_secret=access_secret,
+ )
+ source = ExternalDataSource.objects.create(
+ team=team,
+ source_id="source-id",
+ connection_id="connection-id",
+ status=ExternalDataSource.Status.COMPLETED,
+ source_type=ExternalDataSourceType.STRIPE,
+ prefix="",
+ )
+ table = DataWarehouseTable.objects.create(
+ team=team,
+ name="orders",
+ format=DataWarehouseTable.TableFormat.DeltaS3Wrapper,
+ url_pattern="https://example.com/orders/",
+ credential=credential,
+ external_data_source=source,
+ columns={"id": {"hogql": "IntegerDatabaseField", "clickhouse": "Int64", "schema_valid": True}},
+ )
+ ExternalDataSchema.objects.create(team=team, name="orders", source=source, table=table, should_sync=True)
+
+ with (
+ mock.patch("products.managed_warehouse.backend.cp_teams.list_org_teams", return_value=[]),
+ mock.patch(
+ "products.managed_warehouse.backend.trino_compiler.get_ready_trino_catalog_name",
+ return_value="org_catalog",
+ ),
+ mock.patch(
+ "products.managed_warehouse.backend.trino_compiler.get_org_team_membership",
+ return_value=membership,
+ ),
+ ):
+ with pytest.raises(QueryError, match="explicit alias"):
+ compile_hogql_to_trino_sql(
+ team.pk,
+ HogQLQuery(query="SELECT (SELECT max(id) FROM orders)"),
+ team=team,
+ expansion_mode=TrinoExpansionMode.DJANGO,
+ bypass_warehouse_access_control=True,
+ preserve_clickhouse_output_names=True,
+ )
+
+ compiled = compile_hogql_to_trino_sql(
+ team.pk,
+ HogQLQuery(query="SELECT (SELECT max(id) FROM orders) AS max_order_id"),
+ team=team,
+ expansion_mode=TrinoExpansionMode.DJANGO,
+ bypass_warehouse_access_control=True,
+ preserve_clickhouse_output_names=True,
+ )
+
+ assert 'AS "max_order_id"' in compiled.sql
+ assert access_key not in compiled.sql
+ assert access_secret not in compiled.sql
+
@pytest.mark.parametrize(
"limit_clause, trino_limit_clause",
[
diff --git a/products/managed_warehouse/backend/tests/test_trino_materialization.py b/products/managed_warehouse/backend/tests/test_trino_materialization.py
index 8a2123b7e0a3..8d53fee7538e 100644
--- a/products/managed_warehouse/backend/tests/test_trino_materialization.py
+++ b/products/managed_warehouse/backend/tests/test_trino_materialization.py
@@ -1,14 +1,17 @@
+import re
import datetime as dt
from typing import Any
from uuid import UUID, uuid4
from posthog.test.base import BaseTest
-from unittest.mock import MagicMock, patch
+from unittest.mock import ANY, MagicMock, patch
from parameterized import parameterized
from posthog.schema import HogQLQuery
+from posthog.hogql.parser import parse_select
+
from posthog.models import Team
from products.data_modeling.backend.facade.models import DataWarehouseSavedQuery
@@ -84,7 +87,8 @@ class TestTrinoShadowMaterialization(BaseTest):
team=self.team,
bypass_warehouse_access_control=True,
expansion_mode=TrinoExpansionMode.DJANGO,
- select_transform=None,
+ select_transform=ANY,
+ preserve_clickhouse_output_names=True,
)
connect.assert_called_once_with(
str(self.organization.pk),
@@ -164,6 +168,147 @@ class TestTrinoShadowMaterialization(BaseTest):
assert writes[1][0].endswith("LIMIT 75000")
assert writes[1][1] == ["purchase"]
+ @parameterized.expand(
+ [
+ (
+ "full_build",
+ "SELECT event, count(), max(timestamp) AS ts FROM events GROUP BY event",
+ None,
+ ("count()",),
+ ),
+ (
+ "merge_stage",
+ "SELECT event, count(), max(timestamp) AS ts FROM events GROUP BY event",
+ dt.datetime(2026, 10, 1, tzinfo=dt.UTC),
+ ("count()",),
+ ),
+ (
+ "columns_regex",
+ "SELECT COLUMNS('^event$'), count(), max(timestamp) AS ts FROM events GROUP BY event",
+ None,
+ ("count()",),
+ ),
+ (
+ "columns_list",
+ "SELECT COLUMNS(event), count(), max(timestamp) AS ts FROM events GROUP BY event",
+ dt.datetime(2026, 10, 1, tzinfo=dt.UTC),
+ ("count()",),
+ ),
+ (
+ "union_repeats_expressions",
+ "SELECT event, count(), count() AS hits, max(timestamp) AS ts FROM events GROUP BY event "
+ "UNION ALL SELECT event, count(), count(), max(timestamp) FROM events GROUP BY event",
+ None,
+ ("count()",),
+ ),
+ (
+ "union_by_name",
+ "SELECT event, count() AS hits, max(timestamp) AS ts, 1 AS one FROM events GROUP BY event "
+ "UNION ALL BY NAME SELECT 1 AS one, max(timestamp) AS ts, count() AS hits, event "
+ "FROM events GROUP BY event",
+ dt.datetime(2026, 10, 1, tzinfo=dt.UTC),
+ ("hits", "one"),
+ ),
+ (
+ "resolved_calls",
+ "SELECT max(timestamp), toStartOfDay(timestamp), toDate(timestamp), timestamp AS ts "
+ "FROM events GROUP BY timestamp",
+ None,
+ (
+ "max(toTimeZone(timestamp, 'UTC'))",
+ "toStartOfDay(toTimeZone(timestamp, 'UTC'))",
+ "toDate(toTimeZone(timestamp, 'UTC'))",
+ ),
+ ),
+ (
+ "resolved_property_and_boolean",
+ "SELECT person.properties.x, true, timestamp AS ts FROM events",
+ None,
+ ("x", "1"),
+ ),
+ (
+ "guard_names_inner_columns",
+ "SELECT event, count(), max(timestamp), timestamp AS ts FROM events GROUP BY event, timestamp",
+ dt.datetime(2026, 10, 1, tzinfo=dt.UTC),
+ ("count()", "max(timestamp)"),
+ ),
+ (
+ "string_constant",
+ "SELECT event, 'hello', max(timestamp) AS ts FROM events GROUP BY event",
+ None,
+ ("'hello'",),
+ ),
+ (
+ "tuple_constant",
+ "SELECT event, (1, 2), max(timestamp) AS ts FROM events GROUP BY event",
+ None,
+ ("(1, 2)",),
+ ),
+ (
+ "question_mark_full_build",
+ "SELECT event, countIf(match(event, 'sign.?up')), max(timestamp) AS ts FROM events GROUP BY event",
+ None,
+ ("countIf(match(event, 'sign.?up'))",),
+ ),
+ (
+ "question_mark_merge_stage",
+ "SELECT event, countIf(match(event, 'sign.?up')), timestamp AS ts FROM events GROUP BY event, timestamp",
+ dt.datetime(2026, 10, 1, tzinfo=dt.UTC),
+ ("countIf(match(event, 'sign.?up'))",),
+ ),
+ ]
+ )
+ def test_names_unaliased_columns_in_create_table_as(
+ self, _name: str, query: str, since: dt.datetime | None, expected_names: tuple[str, ...]
+ ) -> None:
+ self.query = {"kind": "HogQLQuery", "query": query}
+ membership = ManagedWarehouseTeamMembership(
+ team_id=self.team.pk,
+ organization_id=str(self.organization.pk),
+ schema_name="production",
+ enabled=True,
+ backfill_enabled=True,
+ table_names=ManagedWarehouseTableNames(
+ events_table="events_current", persons_table="persons_current", data_imports_schema="imports"
+ ),
+ earliest_event_date=None,
+ )
+ cursor = _ScriptedCursor(table_exists=True, staged=1)
+ self.connection.cursor.return_value = cursor
+ with (
+ patch("products.managed_warehouse.backend.cp_teams.list_org_teams", return_value=[]),
+ patch(
+ "products.managed_warehouse.backend.trino_compiler.get_ready_trino_catalog_name",
+ return_value="org_catalog",
+ ),
+ patch(
+ "products.managed_warehouse.backend.trino_compiler.get_org_team_membership",
+ return_value=membership,
+ ),
+ patch(
+ "products.managed_warehouse.backend.trino_materialization.connect_managed_warehouse_trino"
+ ) as connect,
+ ):
+ connect.return_value.__enter__.return_value = self.connection
+ execute_trino_shadow_materialization(
+ organization_id=str(self.organization.pk),
+ team_id=self.team.pk,
+ saved_query_id=self.saved_query_id,
+ source_query=self.query,
+ incremental=TrinoIncrementalWrite(incremental_key="ts", unique_key=("event",), since=since),
+ )
+
+ (write,) = [statement for statement, _ in cursor.statements if statement.startswith("CREATE OR REPLACE TABLE")]
+ for expected_name in expected_names:
+ assert f'AS "{expected_name}"' in write
+ assert ("__ph_incremental_stage" in write) == (since is not None)
+ if since is not None:
+ guarded = write[write.index("FROM (") :]
+ references = re.findall(r'"__ph_incremental"\.("(?:[^"]|"")*")', write)
+ assert references
+ for reference in references:
+ assert f"AS {reference}" in guarded or f".{reference}," in guarded
+
def test_compilation_failure_does_not_connect_or_write(self) -> None:
with (
patch(
@@ -193,7 +338,6 @@ class TestTrinoShadowMaterialization(BaseTest):
connect.assert_not_called()
def test_materialized_dependency_matches_downstream_locator(self) -> None:
-
backing_table = DataWarehouseTable.objects.create(
team=self.team, name="orders", format="Parquet", url_pattern="https://example.com/orders/*.parquet"
)
@@ -348,7 +492,7 @@ class TestTrinoIncrementalMaterialization(BaseTest):
assert isinstance(result, DuckLakeTableResult)
assert (result.row_count, result.watermark, result.merged) == (9, latest, False)
# The table is rebuilt from the unfiltered query.
- assert transforms[-1] is None
+ assert transforms[-1](parse_select("SELECT id, ts FROM orders")).where is None
table = f'"cat"."posthog_data_modeling_team_{self.team.pk}"."model_{self.saved_query_id.hex}"'
assert f"CREATE OR REPLACE TABLE {table} AS SELECT id, ts FROM orders" in statements
assert not any("__ph_incremental_stage" in statement for statement in statements)
diff --git a/products/managed_warehouse/backend/trino_compiler.py b/products/managed_warehouse/backend/trino_compiler.py
index 6b555d1a0930..a1526cf8b747 100644
--- a/products/managed_warehouse/backend/trino_compiler.py
+++ b/products/managed_warehouse/backend/trino_compiler.py
@@ -233,6 +233,7 @@ def compile_hogql_to_trino_sql(
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.
@@ -243,10 +244,12 @@ def compile_hogql_to_trino_sql(
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:
- raise ValueError("A select transform needs Django expansion")
+ 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,
@@ -260,10 +263,11 @@ def compile_hogql_to_trino_sql(
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 # noqa: PLC0415
+ 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
@@ -319,6 +323,30 @@ def compile_hogql_to_trino_sql(
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,
@@ -340,7 +368,63 @@ def compile_hogql_to_trino_sql(
trino_table_locators=trino_table_locators,
)
hogql_ast = clone_expr(parsed) if include_hogql else None
- trino_sql, _ = prepare_and_print_ast(parsed, trino_context, dialect="trino")
+
+ 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
+
+ trino_sql, _ = prepare_and_print_ast(
+ parsed,
+ trino_context,
+ dialect="trino",
+ prepared_ast_transform=preserve_output_names if preserve_clickhouse_output_names else None,
+ )
hogql_pretty: str | None = None
if hogql_ast is not None:
diff --git a/products/managed_warehouse/backend/trino_materialization.py b/products/managed_warehouse/backend/trino_materialization.py
index b5bc9310ba35..1fda738e7246 100644
--- a/products/managed_warehouse/backend/trino_materialization.py
+++ b/products/managed_warehouse/backend/trino_materialization.py
@@ -101,14 +101,14 @@ def execute_trino_shadow_materialization(
def _compile(team: Team, query: HogQLQuery, incremental: TrinoIncrementalWrite | None) -> Any:
- select_transform = None
- if incremental is not None:
+ def select_transform(node: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
+ if incremental is None:
+ return node
from products.data_modeling.backend.facade.api import ( # noqa: PLC0415 -- the facade loads lazily
apply_incremental_filter,
)
- def select_transform(node: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
- return apply_incremental_filter(node, incremental_key=incremental.incremental_key, since=incremental.since)
+ return apply_incremental_filter(node, incremental_key=incremental.incremental_key, since=incremental.since)
return compile_hogql_to_trino_sql(
team.pk,
@@ -117,6 +117,7 @@ def _compile(team: Team, query: HogQLQuery, incremental: TrinoIncrementalWrite |
bypass_warehouse_access_control=True,
expansion_mode=TrinoExpansionMode.DJANGO,
select_transform=select_transform,
+ preserve_clickhouse_output_names=True,
)