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.

feat(data-warehouse): list raw Trino views only under hosted Trino (#126456)

Jovan Sakovic6ec1e8cebda4
diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts
index 9dcaf0af7821..d7f2c196aeda 100644
--- a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts
@@ -1198,6 +1198,61 @@ describe('queryDatabaseLogic', () => {
expect(logic.values.displayedTreeData.map((item) => item.name)).toEqual(['public'])
})
+ it.each([
+ { scope: null, schemaFails: false, expectedViews: ['clickhouse_view'] },
+ { scope: MANAGED_TRINO_SCHEMA_SCOPE, schemaFails: false, expectedViews: ['trino_view'] },
+ { scope: MANAGED_TRINO_SCHEMA_SCOPE, schemaFails: true, expectedViews: ['trino_view'] },
+ { scope: null, schemaFails: false, search: 'trino', expectedViews: [] },
+ { scope: null, schemaFails: false, onlyTrino: true, expectedViews: [] },
+ ])(
+ 'lists raw Trino views only under hosted Trino (scope $scope, schema fails $schemaFails)',
+ async ({ scope, schemaFails, search, onlyTrino, expectedViews }) => {
+ const viewsLogic = dataWarehouseViewsLogic.findMounted()!
+ await expectLogic(viewsLogic).toFinishAllListeners()
+ viewsLogic.actions.loadDataWarehouseSavedQueriesSuccess(
+ [
+ { name: 'trino_view', is_raw_managed_trino: true },
+ { name: 'clickhouse_view', is_raw_managed_trino: false },
+ ]
+ .filter((view) => !onlyTrino || view.is_raw_managed_trino)
+ .map((view) => ({
+ ...view,
+ id: view.name,
+ status: 'Completed',
+ columns: [],
+ managed_viewset_kind: null,
+ })) as unknown as DataWarehouseSavedQuery[]
+ )
+ const databaseLogic = databaseTableListLogic.findMounted()!
+ databaseLogic.actions.setConnection(scope)
+ if (schemaFails) {
+ databaseLogic.actions.loadDatabaseFailure('Connection failed.')
+ } else {
+ databaseLogic.actions.loadDatabaseSuccess({ tables: {}, joins: [] })
+ }
+
+ if (search) {
+ logic.actions.setSearchTerm(search)
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.displayedTreeData.map((item) => item.name)).not.toContain('Views')
+ }
+ const viewsSection = logic.values.displayedTreeData.find((item) => item.name === 'Views')
+ const viewNames: string[] = []
+ const collect = (items: typeof logic.values.displayedTreeData): void =>
+ items.forEach((item) => {
+ if (item.record?.type === 'view') {
+ viewNames.push(item.name)
+ }
+ collect(item.children ?? [])
+ })
+ collect(viewsSection?.children ?? [])
+ expect(viewNames).toEqual(expectedViews)
+ if (onlyTrino) {
+ expect(viewsSection?.children?.map((item) => item.name)).toEqual(['Empty folder'])
+ }
+ }
+ )
+
it('shows an empty native catalog without a source configuration link', () => {
const databaseLogic = databaseTableListLogic.findMounted()!
databaseLogic.actions.setConnection(MANAGED_TRINO_SCHEMA_SCOPE)
diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx
index a3f796a8b43c..249c0f170821 100644
--- a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx
@@ -1560,15 +1560,55 @@ const createTopLevelFolderNode = (
}
}
-const flattenViewNodes = (nodes: TreeDataItem[], flattenedViews: TreeDataItem[]): void => {
+const isRawTrinoViewNode = (node: TreeDataItem): boolean =>
+ node.record?.type === 'view' && !!node.record?.view?.is_raw_managed_trino
+
+// HogQL cannot reference a raw Trino view, so the ClickHouse tree must not offer one.
+const withoutRawTrinoViews = (nodes: TreeDataItem[]): TreeDataItem[] =>
+ nodes.flatMap((node): TreeDataItem[] => {
+ if (isRawTrinoViewNode(node)) {
+ return []
+ }
+ if (!node.children?.length) {
+ return [node]
+ }
+ const children = withoutRawTrinoViews(node.children)
+ if (children.length > 0) {
+ return [{ ...node, children }]
+ }
+ // A folder or search section that held only raw Trino views goes away.
+ // The browsing Views section stays, with the same placeholder an empty section gets.
+ if (node.record?.type === 'views' && !node.id.startsWith('search-')) {
+ return [
+ {
+ ...node,
+ children: [
+ {
+ id: 'views-folder-empty/',
+ name: 'Empty folder',
+ type: 'empty-folder',
+ record: { type: 'empty-folder' },
+ },
+ ],
+ },
+ ]
+ }
+ return []
+ })
+
+const flattenViewNodes = (
+ nodes: TreeDataItem[],
+ flattenedViews: TreeDataItem[],
+ includeRawTrinoViews: boolean
+): void => {
nodes.forEach((node) => {
- if (node.record?.type === 'view-table') {
+ if (node.record?.type === 'view-table' || (includeRawTrinoViews && isRawTrinoViewNode(node))) {
flattenedViews.push(node)
return
}
if (node.record?.type === 'folder' && node.record?.folderType === 'view-folder') {
- flattenViewNodes(node.children ?? [], flattenedViews)
+ flattenViewNodes(node.children ?? [], flattenedViews, includeRawTrinoViews)
}
})
}
@@ -3686,7 +3726,10 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([
const sourceData = searchText ? searchTreeData : treeData
if (!shouldUseDirectConnectionTree(connectionId)) {
- return filterMaterializedViewsByStatus(sourceData, materializationStatusFilters)
+ return filterMaterializedViewsByStatus(
+ withoutRawTrinoViews(sourceData),
+ materializationStatusFilters
+ )
}
const flattenedTables: TreeDataItem[] = []
@@ -3711,7 +3754,11 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([
if (item.record?.type === 'views') {
// In direct-connection mode, hide saved-query and managed view sections,
// and only keep DB-backed view nodes if they are present in schema.
- flattenViewNodes(item.children ?? [], flattenedViews)
+ flattenViewNodes(
+ item.children ?? [],
+ flattenedViews,
+ connectionId === MANAGED_TRINO_SCHEMA_SCOPE
+ )
return
}
@@ -3720,18 +3767,8 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([
}
})
- const hasLoadedTables = Object.keys(allTablesMap).length > 0
- if (!databaseLoading && databaseLoadError) {
- return createSchemaErrorNodes('direct-connection', () => actions.refreshDatabaseSchema())
- }
- if (!databaseLoading && !hasLoadedTables) {
- const emptyNodes = createDirectConnectionEmptyNodes(connectionId)
- return connectionId === MANAGED_TRINO_SCHEMA_SCOPE ? emptyNodes.slice(0, 1) : emptyNodes
- }
-
- return [
- ...groupDirectConnectionTableNodesBySchema(flattenedTables, !!searchText, defaultSchemaName),
- ...(flattenedViews.length > 0
+ const viewsSection: TreeDataItem[] =
+ flattenedViews.length > 0
? [
{
id: searchText ? 'search-views' : 'views',
@@ -3742,7 +3779,28 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([
children: flattenedViews,
},
]
- : []),
+ : []
+
+ // Saved Trino views come from the saved-query list, so they stay reachable when the schema fails.
+ // Views from the schema itself may be stale after a failed refresh, so they are not shown.
+ const savedTrinoViews = flattenedViews.filter(isRawTrinoViewNode)
+ const hasLoadedTables = Object.keys(allTablesMap).length > 0
+ if (!databaseLoading && databaseLoadError) {
+ return [
+ ...createSchemaErrorNodes('direct-connection', () => actions.refreshDatabaseSchema()),
+ ...(savedTrinoViews.length > 0 ? [{ ...viewsSection[0], children: savedTrinoViews }] : []),
+ ]
+ }
+ if (!databaseLoading && !hasLoadedTables) {
+ const emptyNodes = createDirectConnectionEmptyNodes(connectionId)
+ return connectionId === MANAGED_TRINO_SCHEMA_SCOPE
+ ? [...emptyNodes.slice(0, 1), ...viewsSection]
+ : emptyNodes
+ }
+
+ return [
+ ...groupDirectConnectionTableNodesBySchema(flattenedTables, !!searchText, defaultSchemaName),
+ ...viewsSection,
]
},
],
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 55ccf805a7f2..58daa3ead04b 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -6305,6 +6305,8 @@ export interface DataWarehouseSavedQuery {
/** Whether the view is set up to update incrementally. A run can still rebuild the whole table,
* for example on its first run or after the query changes. */
is_incremental?: boolean
+ /** The view stores raw Trino SQL. Only hosted Trino can run it, and HogQL cannot reference it. */
+ is_raw_managed_trino?: boolean
/** Engine → suspension details */
suspended?: DataWarehouseSavedQueryApiSuspended
created_by?: UserBasicType | null
diff --git a/products/data_modeling/backend/models/datawarehouse_saved_query.py b/products/data_modeling/backend/models/datawarehouse_saved_query.py
index cc4ef4b3b3ec..1053a7fb0e6e 100644
--- a/products/data_modeling/backend/models/datawarehouse_saved_query.py
+++ b/products/data_modeling/backend/models/datawarehouse_saved_query.py
@@ -123,7 +123,10 @@ class SchedulingUnavailableError(Exception):
def is_raw_managed_trino_query(query: object) -> bool:
"""A raw Trino view stores Trino SQL that only hosted Trino can run, so ClickHouse and lineage skip it."""
return (
- isinstance(query, dict) and query.get("executionTarget") == "managed_trino" and bool(query.get("sendRawQuery"))
+ isinstance(query, dict)
+ and query.get("executionTarget") == "managed_trino"
+ # The list endpoint matches JSON `true` in SQL, so a truthy string must not count here either.
+ and query.get("sendRawQuery") is True
)
diff --git a/products/data_warehouse/backend/presentation/views/saved_query/editing.py b/products/data_warehouse/backend/presentation/views/saved_query/editing.py
index 1b6677877190..28cac8b96d52 100644
--- a/products/data_warehouse/backend/presentation/views/saved_query/editing.py
+++ b/products/data_warehouse/backend/presentation/views/saved_query/editing.py
@@ -248,6 +248,10 @@ class DataWarehouseSavedQuerySerializer(
has_incremental_history = serializers.SerializerMethodField(
help_text="Whether incremental settings participated in any materialization run."
)
+ is_raw_managed_trino = serializers.BooleanField(
+ read_only=True,
+ help_text="Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it.",
+ )
class Meta:
model = DataWarehouseSavedQuery
@@ -281,6 +285,7 @@ class DataWarehouseSavedQuerySerializer(
"expires_at",
"user_access_level",
"suspended",
+ "is_raw_managed_trino",
]
read_only_fields = [
"id",
diff --git a/products/data_warehouse/backend/presentation/views/saved_query/view_state.py b/products/data_warehouse/backend/presentation/views/saved_query/view_state.py
index 96aadcdb8899..ec18df8060eb 100644
--- a/products/data_warehouse/backend/presentation/views/saved_query/view_state.py
+++ b/products/data_warehouse/backend/presentation/views/saved_query/view_state.py
@@ -169,6 +169,10 @@ class DataWarehouseSavedQueryMinimalSerializer(
help_text="Whether this view is set up to update incrementally. A run can still rebuild the "
"whole table, for example on the first run or after the query changes.",
)
+ is_raw_managed_trino = serializers.SerializerMethodField(
+ read_only=True,
+ help_text="Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it.",
+ )
class Meta:
model = DataWarehouseSavedQuery
@@ -194,5 +198,10 @@ class DataWarehouseSavedQueryMinimalSerializer(
"is_test",
"expires_at",
"user_access_level",
+ "is_raw_managed_trino",
]
read_only_fields = fields
+
+ def get_is_raw_managed_trino(self, view: DataWarehouseSavedQuery) -> bool:
+ # The list defers `query`, so reading the model property here would load it once per view.
+ return bool(getattr(view, "raw_managed_trino", False))
diff --git a/products/data_warehouse/backend/presentation/views/saved_query/viewset.py b/products/data_warehouse/backend/presentation/views/saved_query/viewset.py
index 8914118b13f5..521668fb6093 100644
--- a/products/data_warehouse/backend/presentation/views/saved_query/viewset.py
+++ b/products/data_warehouse/backend/presentation/views/saved_query/viewset.py
@@ -3,7 +3,7 @@
from dataclasses import dataclass
from typing import Any, cast
-from django.db.models import Model, Prefetch
+from django.db.models import BooleanField, Expression, ExpressionWrapper, Model, Prefetch, Q, Value
import structlog
from asgiref.sync import async_to_sync
@@ -47,6 +47,7 @@ from products.data_modeling.backend.facade.models import (
Node,
NodeType,
)
+from products.managed_warehouse.backend.facade.sql_editor import managed_trino_query_flag_enabled
from products.warehouse_sources.backend.facade.models import sync_frequency_to_sync_frequency_interval
from . import editing, incremental_config, lifecycle, lineage, sync_cadence, view_state
@@ -249,6 +250,16 @@ class DataWarehouseSavedQueryViewSet(TeamAndOrgViewSetMixin, AccessControlViewSe
base_queryset = base_queryset.exclude(origin=DataWarehouseSavedQuery.Origin.ENDPOINT).defer(
"query", "external_tables", "incremental_state"
)
+ # Reading the two keys still detoasts `query`, so only orgs that can save raw Trino views pay for it.
+ # The flag check is read-only. The editor's access check can promote the warehouse lifecycle, which a list must not do.
+ if managed_trino_query_flag_enabled(self.team.organization_id):
+ raw_managed_trino: Expression = ExpressionWrapper(
+ Q(query__executionTarget="managed_trino", query__sendRawQuery=True),
+ output_field=BooleanField(),
+ )
+ else:
+ raw_managed_trino = Value(False, output_field=BooleanField())
+ base_queryset = base_queryset.annotate(raw_managed_trino=raw_managed_trino)
if self.action == "list" and not self._include_columns:
base_queryset = base_queryset.defer("columns")
diff --git a/products/data_warehouse/backend/tests/api/test_saved_query.py b/products/data_warehouse/backend/tests/api/test_saved_query.py
index 8a73b4061aa3..b3fe6c81c366 100644
--- a/products/data_warehouse/backend/tests/api/test_saved_query.py
+++ b/products/data_warehouse/backend/tests/api/test_saved_query.py
@@ -41,6 +41,7 @@ from products.data_modeling.backend.facade.models import (
Edge,
Node,
NodeType,
+ is_raw_managed_trino_query,
)
from products.data_modeling.backend.facade.tasks import refresh_downstream_view_columns
from products.data_tools.backend.facade.models import DataWarehouseJoin
@@ -198,6 +199,8 @@ class TestSavedQuery(APIBaseTest):
format="json",
)
self.assertEqual(response.status_code, 201, response.content)
+ # The sidebar adds the created view from this response, before any list reload.
+ self.assertTrue(response.json()["is_raw_managed_trino"])
view = DataWarehouseSavedQuery.objects.get(id=response.json()["id"])
assert view.query is not None and view.columns is not None
self.assertEqual(view.query["query"], trino_sql)
@@ -205,6 +208,13 @@ class TestSavedQuery(APIBaseTest):
self.assertEqual(set(view.columns), {"hub_id", "occupied_at"})
self.assertFalse(Node.objects.filter(saved_query=view).exists())
+ # The sidebar lists raw Trino views only under hosted Trino, so the list must mark them.
+ for flag_enabled in (True, False):
+ with patch("posthoganalytics.feature_enabled", return_value=flag_enabled):
+ listed = self.client.get(f"/api/environments/{self.team.id}/warehouse_saved_queries/").json()
+ flags = {row["name"]: row["is_raw_managed_trino"] for row in listed["results"]}
+ self.assertEqual(flags["hub_occupancy"], flag_enabled)
+
# HogQL on ClickHouse cannot expand Trino SQL, so the view stays out of the HogQL database.
response = self.client.post(
f"/api/environments/{self.team.id}/query/",
@@ -2966,6 +2976,20 @@ class TestSavedQuery(APIBaseTest):
assert view.query != new_query
+class TestIsRawManagedTrinoQuery(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("raw", True, True),
+ ("hogql", None, False),
+ ("string_false", "false", False),
+ ("string_true", "true", False),
+ ]
+ )
+ def test_only_a_json_true_marks_a_raw_trino_view(self, _name: str, send_raw: object, expected: bool) -> None:
+ query = {"query": "SELECT 1", "executionTarget": "managed_trino", "sendRawQuery": send_raw}
+ self.assertEqual(is_raw_managed_trino_query(query), expected)
+
+
class TestSavedQueryNameValidation(SimpleTestCase):
def test_unchanged_legacy_root_name_is_allowed_by_serializer(self) -> None:
instance = DataWarehouseSavedQuery(name="models", origin=DataWarehouseSavedQuery.Origin.DATA_WAREHOUSE)
diff --git a/products/data_warehouse/frontend/generated/api.schemas.ts b/products/data_warehouse/frontend/generated/api.schemas.ts
index 56f59fc7e59d..58bcd8d087fd 100644
--- a/products/data_warehouse/frontend/generated/api.schemas.ts
+++ b/products/data_warehouse/frontend/generated/api.schemas.ts
@@ -1677,6 +1677,8 @@ export interface DataWarehouseSavedQueryMinimalApi {
* @nullable
*/
readonly user_access_level: string | null
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino: boolean
}
export interface PaginatedDataWarehouseSavedQueryMinimalListApi {
@@ -2003,6 +2005,8 @@ export interface DataWarehouseSavedQueryApi {
readonly user_access_level: string | null
/** Engines this query's materialization is suspended for after repeated failures. Suspended engines are skipped by scheduled runs until the query is resumed. */
readonly suspended: DataWarehouseSavedQueryApiSuspended
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino: boolean
}
export type PatchedDataWarehouseSavedQueryApiQueryKind =
@@ -2127,6 +2131,8 @@ export interface PatchedDataWarehouseSavedQueryApi {
readonly user_access_level?: string | null
/** Engines this query's materialization is suspended for after repeated failures. Suspended engines are skipped by scheduled runs until the query is resumed. */
readonly suspended?: PatchedDataWarehouseSavedQueryApiSuspended
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino?: boolean
}
/**
diff --git a/products/managed_warehouse/backend/facade/sql_editor.py b/products/managed_warehouse/backend/facade/sql_editor.py
index 1562d8a0bd35..019ae27facd0 100644
--- a/products/managed_warehouse/backend/facade/sql_editor.py
+++ b/products/managed_warehouse/backend/facade/sql_editor.py
@@ -1,12 +1,16 @@
from typing import TYPE_CHECKING
-from products.managed_warehouse.backend.logic.sql_editor import managed_warehouse_sql_editor_uses_trino
+from products.managed_warehouse.backend.logic.sql_editor import (
+ managed_trino_query_flag_enabled,
+ managed_warehouse_sql_editor_uses_trino,
+)
from products.managed_warehouse.backend.logic.sql_editor_credentials import (
resolve_managed_warehouse_postgres_connection,
)
__all__ = [
"managed_trino_adapter",
+ "managed_trino_query_flag_enabled",
"managed_warehouse_sql_editor_uses_trino",
"resolve_managed_warehouse_postgres_connection",
]
diff --git a/products/managed_warehouse/backend/logic/sql_editor.py b/products/managed_warehouse/backend/logic/sql_editor.py
index 2223de8c6e45..a0bbfc87278f 100644
--- a/products/managed_warehouse/backend/logic/sql_editor.py
+++ b/products/managed_warehouse/backend/logic/sql_editor.py
@@ -6,9 +6,9 @@ from products.managed_warehouse.backend.facade.access import enable_customer_war
from products.managed_warehouse.backend.facade.feature_flags import MANAGED_TRINO_QUERY_FLAG
-def managed_warehouse_sql_editor_uses_trino(organization_id: UUID | str) -> bool:
+def managed_trino_query_flag_enabled(organization_id: UUID | str) -> bool:
org_id = str(organization_id)
- enabled = feature_enabled_or_false(
+ return feature_enabled_or_false(
MANAGED_TRINO_QUERY_FLAG,
org_id,
groups={"organization": org_id},
@@ -16,4 +16,9 @@ def managed_warehouse_sql_editor_uses_trino(organization_id: UUID | str) -> bool
only_evaluate_locally=False,
send_feature_flag_events=False,
)
- return enable_customer_warehouse_access(organization_id, trino_enabled=enabled)
+
+
+def managed_warehouse_sql_editor_uses_trino(organization_id: UUID | str) -> bool:
+ return enable_customer_warehouse_access(
+ organization_id, trino_enabled=managed_trino_query_flag_enabled(organization_id)
+ )
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 79c746113fad..5199a52a6191 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -33072,6 +33072,8 @@ export namespace Schemas {
readonly user_access_level: string | null;
/** Engines this query's materialization is suspended for after repeated failures. Suspended engines are skipped by scheduled runs until the query is resumed. */
readonly suspended: DataWarehouseSavedQuerySuspended;
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino: boolean;
}
/**
@@ -33215,6 +33217,8 @@ export namespace Schemas {
* @nullable
*/
readonly user_access_level: string | null;
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino: boolean;
}
export type DataWarehouseSourceCategory = typeof DataWarehouseSourceCategory[keyof typeof DataWarehouseSourceCategory];
@@ -80115,6 +80119,8 @@ export namespace Schemas {
readonly user_access_level?: string | null;
/** Engines this query's materialization is suspended for after repeated failures. Suspended engines are skipped by scheduled runs until the query is resumed. */
readonly suspended?: PatchedDataWarehouseSavedQuerySuspended;
+ /** Whether this view stores raw Trino SQL. Only hosted Trino can run it, and HogQL queries cannot reference it. */
+ readonly is_raw_managed_trino?: boolean;
}
/**