: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(managed-warehouse): trino backfills for team-owned base tables (#129664)
Eric Duong65f0d56b4e47
diff --git a/posthog/dags/README_DUCKLINGS.md b/posthog/dags/README_DUCKLINGS.mdindex 416ea5d6cdc7..7619443cf2b7 100644--- a/posthog/dags/README_DUCKLINGS.md+++ b/posthog/dags/README_DUCKLINGS.md@@ -32,6 +32,9 @@ Each export writes under a new file prefix, so a retry never overwrites a file tA Trino backfill connects through the normal Trino hostname.Set `use_trino_canary: true` in the run config to connect through the canary hostname instead, which needs `MANAGED_WAREHOUSE_TRINO_CANARY_DOMAIN` to be set. Without it, the run fails.The flag `managed-warehouse-trino-canary` does not apply to backfills.+A run with `register_through_trino: true` registers through Trino even when the organization is not on the flag, and scheduled runs keep following the flag.+Before each statement that removes rows from the base `events`, `persons` or `groups` table, Trino checks that no other team has rows in the range it removes, and the run refuses otherwise.+A team without a control-plane row cannot fall back to a base table that another team of the organization writes, so its backfill fails instead.One `add_files` call registers the files of an export and removes the rows they replace in a single DuckLake snapshot:@@ -48,7 +51,7 @@ To change an organization over:It widens the columns that DuckLake can widen in place, such as groups `group_type_index`, and reports the tables that still need a rebuild.It fails when the events table needs a rebuild, because no snapshot run can rebuild events.2. A column that DuckLake cannot widen, such as persons `person_version`, needs a full backfill with `rebuild_mismatched_table: true`.- Run it with the organization on the Trino flag, and without `skip_schema_validation`, because the validation finds the mismatch.+ Run it with the organization on the Trino flag or with `register_through_trino: true`, and without `skip_schema_validation`, because the validation finds the mismatch.The run replaces the table after the export and keeps the columns that the export does not write.Readers see an empty table until the run registers the files, and a run that fails after the rebuild leaves the table empty until the next full backfill.diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.pyindex 65b5caf57c51..2eae79f1bec5 100644--- a/posthog/dags/events_backfill_to_duckling.py+++ b/posthog/dags/events_backfill_to_duckling.py@@ -1046,6 +1046,8 @@ class DucklingBackfillConfig(Config):dry_run: bool = False# Opt-in per run. A backfill never evaluates the canary feature flag.use_trino_canary: bool = False+ # Opt-in per run: register through Trino even when the organization is not on the flag.+ register_through_trino: bool = Falseevents_parquet_row_group_size_bytes: int = pydantic.Field(default=DEFAULT_EVENTS_PARQUET_ROW_GROUP_SIZE_BYTES,gt=0,@@ -2643,7 +2645,9 @@ def backfill_through_trino()rebuild = Falsewith (- connect_backfill_catalog(target.organization_id, canary=config.use_trino_canary) if register else nullcontext()+ connect_backfill_catalog(target.organization_id, canary=config.use_trino_canary, team_id=target.team_id)+ if register+ else nullcontext()) as catalog:if catalog is not None:try:@@ -2865,7 +2869,7 @@ def _run_duckling_events_backfill(context: AssetExecutionContext, config: Ducklicontext.log.info(f"Backfill ready for team_id={team_id}: org={target.organization_id}, bucket={target.bucket}")- if backfills_through_trino(target.organization_id):+ if config.register_through_trino or backfills_through_trino(target.organization_id):_backfill_events_through_trino(context, config, target, dates)return@@ -3187,7 +3191,7 @@ def _run_duckling_persons_backfill(context: AssetExecutionContext, config: Ducklcontext.log.info(f"Backfill ready for team_id={team_id}: org={target.organization_id}, bucket={target.bucket}")- if backfills_through_trino(target.organization_id):+ if config.register_through_trino or backfills_through_trino(target.organization_id):_backfill_persons_through_trino(context, config, target, None if is_full else dates)returndiff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.pyindex eddedff8fc3a..09d19c2ba7d1 100644--- a/posthog/dags/test_events_backfill_to_duckling.py+++ b/posthog/dags/test_events_backfill_to_duckling.py@@ -2752,6 +2752,7 @@ class TestBackfillThroughTrino:clickhouse: _FakeExportClickHouse,config: DucklingBackfillConfig,catalog: _FakeTrinoBackfillCatalog | None = None,+ org_on_flag: bool = True,):catalog = catalog or _FakeTrinoBackfillCatalog()cluster = MagicMock()@@ -2760,7 +2761,7 @@ class TestBackfillThroughTrino:context.run.run_id = "0123456789abcdef-run"with (patch("posthog.dags.events_backfill_to_duckling._resolve_duckling_target", return_value=self.TARGET),- patch("posthog.dags.events_backfill_to_duckling.backfills_through_trino", return_value=True),+ patch("posthog.dags.events_backfill_to_duckling.backfills_through_trino", return_value=org_on_flag),patch("posthog.dags.events_backfill_to_duckling.connect_backfill_catalog") as connect_catalog,patch("posthog.dags.events_backfill_to_duckling._get_cluster", return_value=cluster),patch("posthog.dags.events_backfill_to_duckling._DuckgresSession") as duckgres,@@ -2812,7 +2813,7 @@ class TestBackfillThroughTrino:catalog, connect_catalog = self._run(run, partition_key, clickhouse, DucklingBackfillConfig())- connect_catalog.assert_called_with("org-1", canary=False)+ connect_catalog.assert_called_with("org-1", canary=False, team_id=2)assert catalog.tables == [(table, column_types)] * 2assert [(t, r) for t, _, _, r in catalog.registered] == [(table, replacement)] * 2first, second = catalog.registered@@ -2932,7 +2933,26 @@ class TestBackfillThroughTrino:run, partition_key, _FakeExportClickHouse(files_per_export=1), DucklingBackfillConfig(use_trino_canary=True))- connect_catalog.assert_called_with("org-1", canary=True)+ connect_catalog.assert_called_with("org-1", canary=True, team_id=2)++ @parameterized.expand(+ [+ ("events", _run_duckling_events_backfill, "2_2026-06-17"),+ ("persons", _run_duckling_persons_backfill, "2"),+ ]+ )+ @time_machine.travel(datetime(2026, 7, 1, tzinfo=UTC), tick=False)+ def test_opted_in_runs_register_through_trino_when_the_org_is_not_on_the_flag(self, _name, run, partition_key):+ catalog, connect_catalog = self._run(+ run,+ partition_key,+ _FakeExportClickHouse(files_per_export=1),+ DucklingBackfillConfig(register_through_trino=True),+ org_on_flag=False,+ )++ connect_catalog.assert_called_with("org-1", canary=False, team_id=2)+ assert catalog.registered@time_machine.travel(datetime(2026, 7, 1, tzinfo=UTC), tick=False)def test_dry_runs_neither_export_nor_open_the_catalog(self):diff --git a/products/managed_warehouse/backend/backfill_trino.py b/products/managed_warehouse/backend/backfill_trino.pyindex 924b1743cf75..1bbc11461edc 100644--- a/products/managed_warehouse/backend/backfill_trino.py+++ b/products/managed_warehouse/backend/backfill_trino.py@@ -78,12 +78,11 @@ def _timestamp_bound(day: date) -> str:return f"{day.isoformat()} 00:00:00+00"-def _refuse_shared_table(table: str, change: str) -> None:- if table in SHARED_TABLES:- raise ValueError(- f"Refusing to {change} the shared table '{table}' through Trino, because that would also remove the rows "- "of other teams"- )+def _day_predicate(replacement: DuckLakeDayReplacement) -> str:+ column = escape_trino_identifier(replacement.column)+ start = replacement.day.isoformat()+ end = (replacement.day + timedelta(days=1)).isoformat()+ return f"{column} >= TIMESTAMP '{start} 00:00:00 UTC' AND {column} < TIMESTAMP '{end} 00:00:00 UTC'"def _column_definitions(columns: Mapping[str, str]) -> str:@@ -107,32 +106,53 @@ def _log_retry(state: RetryCallState) -> None:)+_retry_transient = retry(+ retry=retry_if_exception(_is_transient),+ stop=stop_after_attempt(STATEMENT_ATTEMPTS),+ wait=wait_exponential(multiplier=1, min=4, max=60),+ before_sleep=_log_retry,+ reraise=True,+)++class TrinoBackfillCatalog:"""The DuckLake statements of one backfill, run in the organization's Trino catalog."""- def __init__(self, connection: Connection) -> None:+ def __init__(self, connection: Connection, *, team_id: int | None = None) -> None:self._connection = connection+ self._team_id = team_idself._catalog = escape_trino_identifier(connection.catalog)@classmethod@contextlib.contextmanager- def connect(cls, organization_id: str, *, canary: bool = False) -> Iterator[TrinoBackfillCatalog]:+ def connect(+ cls, organization_id: str, *, canary: bool = False, team_id: int | None = None+ ) -> Iterator[TrinoBackfillCatalog]:with connect_managed_warehouse_trino(organization_id, principal=BACKFILL_TRINO_PRINCIPAL, canary=canary) as connection:- yield cls(connection)+ yield cls(connection, team_id=team_id)++ def _refuse_shared_table(self, table: str, change: str, replacement: Replacement | None = None) -> None:+ # The check reads the rows right before each statement, so it also sees rows that a team without a+ # control-plane row wrote, rows written before the team was pinned, and rows written during the export.+ if table not in SHARED_TABLES:+ return+ if self._team_id is not None:+ condition = f"{escape_trino_identifier('team_id')} <> {int(self._team_id)}"+ if isinstance(replacement, DuckLakeDayReplacement):+ condition += f" AND {_day_predicate(replacement)}"+ if not self._execute(f"SELECT 1 FROM {self._table(table)} WHERE {condition} LIMIT 1"):+ return+ raise ValueError(+ f"Refusing to {change} the shared table '{table}' through Trino, because that would also remove the rows "+ "of other teams"+ )def _table(self, table: str) -> str:return f"{self._catalog}.{escape_trino_identifier(BACKFILL_SCHEMA)}.{escape_trino_identifier(table)}"- @retry(- retry=retry_if_exception(_is_transient),- stop=stop_after_attempt(STATEMENT_ATTEMPTS),- wait=wait_exponential(multiplier=1, min=4, max=60),- before_sleep=_log_retry,- reraise=True,- )- def _run(self, statement: str) -> list[list[Any]]:+ def _execute(self, statement: str) -> list[list[Any]]:# Every statement here can run again. DDL has IF [NOT] EXISTS, deleting a range twice deletes nothing more,# and add_files records its import_id, so a repeat of an applied import adds nothing.# CREATE OR REPLACE runs before the add_files that fills the table, so a repeat recreates an empty table.@@ -143,12 +163,25 @@ class TrinoBackfillCatalog:finally:cursor.close()+ @_retry_transient+ def _run(self, statement: str) -> list[list[Any]]:+ return self._execute(statement)++ @_retry_transient+ def _run_removal(self, table: str, change: str, replacement: Replacement | None, statement: str) -> list[list[Any]]:+ # A commit conflict means that another writer changed the table, so every attempt checks the rows again.+ self._refuse_shared_table(table, change, replacement)+ return self._execute(statement)+def ensure_table(self, table: str, columns: Mapping[str, str], *, drop_first: bool, create_if_missing: bool, validate: bool) -> None:if drop_first:- _refuse_shared_table(table, "drop")- self._run(f"DROP TABLE IF EXISTS {self._table(table)}")+ drop = f"DROP TABLE IF EXISTS {self._table(table)}"+ if table in SHARED_TABLES and self._columns(table):+ self._run_removal(table, "drop", None, drop)+ else:+ self._run(drop)if create_if_missing:self._run(f"CREATE SCHEMA IF NOT EXISTS {self._catalog}.{escape_trino_identifier(BACKFILL_SCHEMA)}")self._run(f"CREATE TABLE IF NOT EXISTS {self._table(table)} ({_column_definitions(columns)})")@@ -179,10 +212,14 @@ class TrinoBackfillCatalog:return {str(name): str(data_type) for name, data_type in rows}def replace_table(self, table: str, columns: Mapping[str, str]) -> None:- _refuse_shared_table(table, "rebuild")# Views can select a column that the export does not write, so the rebuilt table keeps it.kept = {name: data_type for name, data_type in self._columns(table).items() if name not in columns}- self._run(f"CREATE OR REPLACE TABLE {self._table(table)} ({_column_definitions({**columns, **kept})})")+ self._run_removal(+ table,+ "rebuild",+ None,+ f"CREATE OR REPLACE TABLE {self._table(table)} ({_column_definitions({**columns, **kept})})",+ )def validate_file_count(self, files: Sequence[str]) -> None:if len(files) > ADD_FILES_MAX_FILES:@@ -199,8 +236,6 @@ class TrinoBackfillCatalog:Repeating a call with the same import_id and files is a no-op, so a call whose response was lost can berepeated."""- if replacement is not None:- _refuse_shared_table(table, "replace rows in")self.validate_file_count(files)options = [f"files => ARRAY[{', '.join(escape_trino_string(path) for path in files)}]",@@ -216,18 +251,16 @@ class TrinoBackfillCatalog:]case DuckLakeTableReplacement():options.append("replace => true")- rows = self._run(f"ALTER TABLE {self._table(table)} EXECUTE add_files({', '.join(options)})")+ statement = f"ALTER TABLE {self._table(table)} EXECUTE add_files({', '.join(options)})"+ if replacement is None:+ rows = self._run(statement)+ else:+ rows = self._run_removal(table, "replace rows in", replacement, statement)return sum(int(row[1]) for row in rows if len(row) > 1 and row[0] == "added_data_files")def delete_rows(self, table: str, replacement: Replacement) -> None:"""Remove the rows an export replaces when the export wrote no files."""- _refuse_shared_table(table, "replace rows in")statement = f"DELETE FROM {self._table(table)}"if isinstance(replacement, DuckLakeDayReplacement):- column = escape_trino_identifier(replacement.column)- start = replacement.day.isoformat()- end = (replacement.day + timedelta(days=1)).isoformat()- statement += (- f" WHERE {column} >= TIMESTAMP '{start} 00:00:00 UTC' AND {column} < TIMESTAMP '{end} 00:00:00 UTC'"- )- self._run(statement)+ statement += f" WHERE {_day_predicate(replacement)}"+ self._run_removal(table, "replace rows in", replacement, statement)diff --git a/products/managed_warehouse/backend/facade/backfill_trino.py b/products/managed_warehouse/backend/facade/backfill_trino.pyindex 491b5ebfe948..e4c119523067 100644--- a/products/managed_warehouse/backend/facade/backfill_trino.py+++ b/products/managed_warehouse/backend/facade/backfill_trino.py@@ -15,6 +15,6 @@ def backfills_through_trino(organization_id: str) -> bool:def connect_backfill_catalog(- organization_id: str, *, canary: bool = False+ organization_id: str, *, canary: bool = False, team_id: int | None = None) -> AbstractContextManager[DuckLakeBackfillCatalog]:- return backfill_trino.TrinoBackfillCatalog.connect(organization_id, canary=canary)+ return backfill_trino.TrinoBackfillCatalog.connect(organization_id, canary=canary, team_id=team_id)diff --git a/products/managed_warehouse/backend/team_state.py b/products/managed_warehouse/backend/team_state.pyindex 04049d65ea4f..fe121a1e415d 100644--- a/products/managed_warehouse/backend/team_state.py+++ b/products/managed_warehouse/backend/team_state.py@@ -39,6 +39,22 @@ def _get_cp_row(team_id: int) -> cp_teams.CPTeam | None:return next((team for team in teams if team.team_id == team_id), None)+def _refuse_base_tables_another_team_writes(team_id: int, tables: tuple[str, ...]) -> None:+ # Rows of a second team in a base table make every Trino replacement on that table refuse. A cached read can miss+ # a team that started to write the table within the cache TTL.+ teams = cp_teams.list_org_teams(_get_org_id_for_team(team_id), use_cache=False)+ if teams is None:+ raise CPUnavailableError(f"duckgres control plane unreachable reading team state for team {team_id}")+ claimed = sorted(+ {name for team in teams for name in (team.resolved_events_table, team.resolved_persons_table)} & set(tables)+ )+ if claimed:+ raise ValueError(+ f"Team {team_id} has no control-plane row, so it would write to {claimed}, "+ "which another team of the organization writes"+ )++# --- events/persons table names (Dagster duckling backfill) -----------------------@@ -47,11 +63,13 @@ def resolve_events_persons_tables(team_id: int) -> DucklingTables:Failure posture: raises :class:`CPUnavailableError` when the control plane can'tanswer and the cache is cold — the backfill run fails and retries rather than- writing to guessed tables.+ writing to guessed tables. Raises ValueError when a team without a control-plane+ row would fall back to a base table that another team of the organization writes."""row = _get_cp_row(team_id)if row is None:# Legacy single-team ducklings without a team row share the base tables.+ _refuse_base_tables_another_team_writes(team_id, ("events", "persons"))return DucklingTables(events_table="events", persons_table="persons")events_table, persons_table = row.resolved_events_table, row.resolved_persons_tablevalidate_duckgres_identifier(events_table)diff --git a/products/managed_warehouse/backend/tests/test_backfill_trino.py b/products/managed_warehouse/backend/tests/test_backfill_trino.pyindex 612cb2d9fad9..492284720b58 100644--- a/products/managed_warehouse/backend/tests/test_backfill_trino.py+++ b/products/managed_warehouse/backend/tests/test_backfill_trino.py@@ -35,8 +35,8 @@ class _Cursor:def execute(self, statement: str) -> None:self._connection.statements.append(statement)- if self._connection.failures:- raise self._connection.failures.pop(0)+ if self._connection.failures and (failure := self._connection.failures.pop(0)) is not None:+ raise failureself._rows = self._connection.rows_for(statement)def fetchall(self) -> list[list[object]]:@@ -49,10 +49,16 @@ class _Cursor:class _FakeTrino:catalog = "warehouse"- def __init__(self, columns: dict[str, str] | None = None, failures: list[Exception] | None = None) -> None:+ def __init__(+ self,+ columns: dict[str, str] | None = None,+ failures: list[Exception | None] | None = None,+ other_team_rows: bool | list[bool] = False,+ ) -> None:self.statements: list[str] = []self.failures = failures or []self._columns = columns or {}+ self._other_team_rows = other_team_rowsdef cursor(self) -> _Cursor:return _Cursor(self)@@ -62,14 +68,24 @@ class _FakeTrino:return [[name, data_type] for name, data_type in self._columns.items()]if "EXECUTE add_files" in statement:return [["added_data_files", 2]]+ if statement.startswith("SELECT 1 FROM"):+ rows = self._other_team_rows+ return [[1]] if (rows.pop(0) if isinstance(rows, list) else rows) else []return []def _catalog(- columns: dict[str, str] | None = None, failures: list[Exception] | None = None+ columns: dict[str, str] | None = None,+ failures: list[Exception | None] | None = None,+ team_id: int | None = None,+ other_team_rows: bool | list[bool] = False,) -> tuple[TrinoBackfillCatalog, _FakeTrino]:- connection = _FakeTrino(columns, failures)- return TrinoBackfillCatalog(cast("Connection", connection)), connection+ connection = _FakeTrino(columns, failures, other_team_rows)+ return TrinoBackfillCatalog(cast("Connection", connection), team_id=team_id), connection+++def _changes(connection: _FakeTrino) -> list[str]:+ return [statement for statement in connection.statements if not statement.startswith("SELECT")]@pytest.mark.parametrize("canary", [False, True])@@ -150,6 +166,47 @@ class TestRegisterFiles:assert connection.statements == []+ @parameterized.expand(+ [+ (f"{name}_{'other_team_rows' if other_team_rows else 'own_rows_only'}", action, in_day, other_team_rows)+ for other_team_rows in (False, True)+ for name, action, in_day in (+ (+ "register_table",+ lambda c: c.register_files(+ "persons", ["s3://b/f.parquet"], import_id="i", replacement=DuckLakeTableReplacement()+ ),+ False,+ ),+ (+ "delete_day",+ lambda c: c.delete_rows("events", DuckLakeDayReplacement(column="timestamp", day=date(2024, 1, 1))),+ True,+ ),+ ("rebuild", lambda c: c.replace_table("persons", COLUMNS), False),+ (+ "drop",+ lambda c: c.ensure_table(+ "events", COLUMNS, drop_first=True, create_if_missing=False, validate=False+ ),+ False,+ ),+ )+ ]+ )+ def test_changes_a_base_table_only_when_no_other_team_has_rows_in_the_range(+ self, _name: str, action, in_day: bool, other_team_rows: bool+ ) -> None:+ catalog, connection = _catalog(COLUMNS, team_id=7, other_team_rows=other_team_rows)++ with pytest.raises(ValueError, match="shared") if other_team_rows else nullcontext():+ action(catalog)++ [probe] = [statement for statement in connection.statements if statement.startswith("SELECT 1 FROM")]+ assert '"team_id" <> 7' in probe+ assert ("TIMESTAMP '2024-01-01 00:00:00 UTC'" in probe) == in_day+ assert bool(_changes(connection)) != other_team_rows+def test_refuses_more_files_than_one_call_registers(self) -> None:catalog, connection = _catalog()files = [f"s3://bucket/f{index}.parquet" for index in range(ADD_FILES_MAX_FILES + 1)]@@ -228,7 +285,7 @@ class TestEnsureTable:with pytest.raises(ValueError, match="shared"):action(catalog, table)- assert connection.statements == []+ assert _changes(connection) == []@parameterized.expand([@@ -321,7 +378,7 @@ class TestTransientFailures:assert len(connection.statements) == 1def test_raises_the_last_transient_failure_after_bounded_attempts(self, _sleep) -> None:- failures: list[Exception] = [+ failures: list[Exception | None] = [_query_error(TrinoExternalError, "DUCKLAKE_COMMIT_CONFLICT", "EXTERNAL") for _ in range(10)]catalog, connection = _catalog(failures=failures)@@ -330,3 +387,17 @@ class TestTransientFailures:_register(catalog)assert 1 < len(connection.statements) < 10++ def test_checks_the_rows_again_when_a_base_table_change_retries(self, _sleep) -> None:+ conflict = _query_error(TrinoExternalError, "DUCKLAKE_COMMIT_CONFLICT", "EXTERNAL")+ catalog, connection = _catalog(team_id=7, failures=[None, conflict], other_team_rows=[False, True])++ with pytest.raises(ValueError, match="shared"):+ catalog.register_files(+ "events",+ ["s3://bucket/backfill/a.parquet"],+ import_id="run1_ab12",+ replacement=DuckLakeDayReplacement(column="timestamp", day=date(2024, 1, 31)),+ )++ assert [statement.split()[0] for statement in connection.statements] == ["SELECT", "ALTER", "SELECT"]diff --git a/products/managed_warehouse/backend/tests/test_groups_backfill.py b/products/managed_warehouse/backend/tests/test_groups_backfill.pyindex a6c47240aac4..211af4a91dea 100644--- a/products/managed_warehouse/backend/tests/test_groups_backfill.py+++ b/products/managed_warehouse/backend/tests/test_groups_backfill.py@@ -340,7 +340,7 @@ def test_groups_backfill_registers_through_trino_without_duckgres() -> None:inserts, catalog, connect_catalog, context = _run_groups_through_trino(partition_key, DucklingBackfillConfig())- connect_catalog.assert_called_once_with("org", canary=False)+ connect_catalog.assert_called_once_with("org", canary=False, team_id=7)assert catalog.tables == [("groups_team_7", GROUPS_TRINO_COLUMN_TYPES)][(table, files, registered_replacement)] = catalog.registeredassert (table, registered_replacement) == ("groups_team_7", replacement)@@ -355,6 +355,16 @@ def test_groups_backfill_registers_through_trino_without_duckgres(assert context.add_output_metadata.call_args.args[0]["files_registered"] == 1+@time_machine.travel(datetime(2026, 1, 1, tzinfo=UTC), tick=False)+def test_groups_backfill_opts_in_to_trino_when_the_org_is_not_on_the_flag() -> None:+ _, catalog, connect_catalog, _ = _run_groups_through_trino(+ "7", DucklingBackfillConfig(register_through_trino=True), org_on_flag=False+ )++ connect_catalog.assert_called_once()+ assert catalog.registered++@pytest.mark.parametrize("partition_key", ["7", "7_2025-12-31"])@time_machine.travel(datetime(2026, 1, 1, tzinfo=UTC), tick=False)def test_groups_export_only_writes_the_trino_types_without_registering(partition_key: str) -> None:@@ -370,7 +380,7 @@ def test_groups_export_only_writes_the_trino_types_without_registering(partitiondef _run_groups_through_trino(- partition_key: str, config: DucklingBackfillConfig+ partition_key: str, config: DucklingBackfillConfig, org_on_flag: bool = True) -> tuple[list[str], _FakeGroupsCatalog, MagicMock, MagicMock]:target = DucklingTarget(team_id=7, organization_id="org", bucket="bkt", bucket_region="us-east-1")inserts: list[str] = []@@ -394,7 +404,7 @@ def _run_groups_through_trino(with (patch("products.managed_warehouse.dags.groups_backfill._resolve_duckling_target", return_value=target),patch("products.managed_warehouse.dags.groups_backfill.resolve_groups_table", return_value="groups_team_7"),- patch("products.managed_warehouse.dags.groups_backfill.backfills_through_trino", return_value=True),+ patch("products.managed_warehouse.dags.groups_backfill.backfills_through_trino", return_value=org_on_flag),patch("products.managed_warehouse.dags.groups_backfill._DuckgresSession") as duckgres,patch("products.managed_warehouse.dags.groups_backfill.record_duckling_backfill_workload"),patch("posthog.dags.events_backfill_to_duckling._get_cluster", return_value=cluster),diff --git a/products/managed_warehouse/backend/tests/test_team_state.py b/products/managed_warehouse/backend/tests/test_team_state.pyindex 3fc892d401e7..2362bf838a9a 100644--- a/products/managed_warehouse/backend/tests/test_team_state.py+++ b/products/managed_warehouse/backend/tests/test_team_state.py@@ -119,6 +119,13 @@ class TestEventsPersonsTables:events_table="events", persons_table="persons")+ def test_without_cp_row_refuses_base_tables_another_team_writes(self) -> None:+ org, team = _team()+ owner = Team.objects.create(organization=org)+ with _patch_org_rows([_cp_row(owner, "owner_schema", events_table_name="events")]):+ with pytest.raises(ValueError, match="another team"):+ team_state.resolve_events_persons_tables(team.id)+def test_raises_when_cp_unreachable_and_cache_cold(self) -> None:org, team = _team()with _patch_org_rows(None):diff --git a/products/managed_warehouse/dags/groups_backfill.py b/products/managed_warehouse/dags/groups_backfill.pyindex cf3b52f7f377..1f18c7ab5627 100644--- a/products/managed_warehouse/dags/groups_backfill.py+++ b/products/managed_warehouse/dags/groups_backfill.py@@ -264,7 +264,7 @@ def run_groups_backfill(context: AssetExecutionContext, config: DucklingBackfilltarget = _resolve_duckling_target(team_id)table_name = resolve_groups_table(team_id)tags = dagster_tags(context)- if backfills_through_trino(target.organization_id):+ if config.register_through_trino or backfills_through_trino(target.organization_id):result = backfill_through_trino(context,config,diff --git a/products/managed_warehouse/dags/trino_conversion.py b/products/managed_warehouse/dags/trino_conversion.pyindex bae3a713ce8f..f16b4aaa26fc 100644--- a/products/managed_warehouse/dags/trino_conversion.py+++ b/products/managed_warehouse/dags/trino_conversion.py@@ -115,7 +115,7 @@ def _summarize(if plan.rebuild and kind != "events":job = f"duckling_{kind}_backfill_job"next_steps.append(- f"Rebuild {kind}: run {job} for partition {team_id} with rebuild_mismatched_table: true, with the org on the Trino flag"+ f"Rebuild {kind}: run {job} for partition {team_id} with rebuild_mismatched_table: true, with the org on the Trino flag or register_through_trino: true")summary = "\n".join([*lines, *next_steps])context.log.info(f"Trino conversion summary for team {team_id}:\n{summary}")