: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
fix(data-modeling): order dependent saved queries deterministically across DAGs (#102932)
Vardhman Bengani5d3e2a78f443
diff --git a/products/data_modeling/backend/logic/saved_query_dag_sync.py b/products/data_modeling/backend/logic/saved_query_dag_sync.pyindex e047cc1372ba..f3a1b0f9c7b0 100644--- a/products/data_modeling/backend/logic/saved_query_dag_sync.py+++ b/products/data_modeling/backend/logic/saved_query_dag_sync.py@@ -374,14 +374,19 @@ def get_dependent_saved_queries(saved_query: "DataWarehouseSavedQuery") -> list[Every node of the saved query counts, not just one: the delete removes all of them, so adependent hanging off a second DAG's node would lose its edge without ever blocking the delete.- A dependent that reads the query in more than one DAG is reported once.+ A dependent that reads the query in more than one DAG is reported once, at its oldest node, so+ the returned order is deterministic even when the query has nodes in several DAGs."""nodes = Node.objects.filter(team=saved_query.team, saved_query=saved_query)- dependent_nodes = Node.objects.filter(- team=saved_query.team,- incoming_edges__source__in=nodes,- saved_query__isnull=False,- ).select_related("saved_query")+ dependent_nodes = (+ Node.objects.filter(+ team=saved_query.team,+ incoming_edges__source__in=nodes,+ saved_query__isnull=False,+ )+ .select_related("saved_query")+ .order_by("created_at", "id")+ )dependents: dict[str, DataWarehouseSavedQuery] = {}for dependent_node in dependent_nodes:dependent = dependent_node.saved_querydiff --git a/products/data_modeling/backend/test/test_saved_query_dag_sync.py b/products/data_modeling/backend/test/test_saved_query_dag_sync.pyindex fbf7c4578679..987a5c118a3a 100644--- a/products/data_modeling/backend/test/test_saved_query_dag_sync.py+++ b/products/data_modeling/backend/test/test_saved_query_dag_sync.py@@ -666,6 +666,49 @@ class TestGetDependents(BaseTest):self.assertEqual([d.name for d in dependents], ["downstream_view"])+ def test_get_dependents_dedupes_and_orders_across_dags(self):+ upstream = DataWarehouseSavedQuery.objects.create(+ name="upstream_view",+ team=self.team,+ query={"query": "SELECT * FROM events", "kind": "HogQLQuery"},+ )+ sync_saved_query_to_dag(upstream)+ upstream_node = Node.objects.get(team=self.team, saved_query=upstream)+ downstream = DataWarehouseSavedQuery.objects.create(+ name="downstream_view",+ team=self.team,+ query={"query": "SELECT * FROM upstream_view", "kind": "HogQLQuery"},+ )+ sync_saved_query_to_dag(downstream)+ other = DataWarehouseSavedQuery.objects.create(+ name="other_view",+ team=self.team,+ query={"query": "SELECT 1", "kind": "HogQLQuery"},+ )+ other_node = Node.objects.create(team=self.team, dag=upstream_node.dag, saved_query=other, type=NodeType.VIEW)+ Edge.objects.create(team=self.team, dag=upstream_node.dag, source=upstream_node, target=other_node)++ # downstream again, reachable through a second DAG: distinct Node rows, same saved query.+ second_dag = DAG.objects.create(team=self.team, name="second-dag")+ upstream_node2 = Node.objects.create(team=self.team, dag=second_dag, saved_query=upstream, type=NodeType.VIEW)+ downstream_node2 = Node.objects.create(+ team=self.team, dag=second_dag, saved_query=downstream, type=NodeType.VIEW+ )+ Edge.objects.create(team=self.team, dag=second_dag, source=upstream_node2, target=downstream_node2)++ # Deterministic node order (created_at, id): downstream's first-DAG node, then other's node.+ # downstream's second-DAG node is a duplicate and must not repeat it.+ self.assertEqual(+ [d.name for d in get_dependent_saved_queries(upstream)],+ ["downstream_view", "other_view"],+ )++ with self.assertRaises(HasDependentsError) as context:+ delete_node_from_dag(upstream)+ names = [d.name for d in context.exception.dependents]+ self.assertEqual(names.count("downstream_view"), 1)+ self.assertEqual(names.count("other_view"), 1)+def test_get_dependents_returns_empty_when_no_node(self):saved_query = DataWarehouseSavedQuery.objects.create(name="test_view",