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(data-warehouse): say what to do when a join matches no sampled rows (#99742)

Tom Owers268e0fb0fcdc
diff --git a/frontend/src/scenes/data-warehouse/viewLinkLogic.test.tsx b/frontend/src/scenes/data-warehouse/viewLinkLogic.test.tsx
index 60178ad6076b..6a15b0338122 100644
--- a/frontend/src/scenes/data-warehouse/viewLinkLogic.test.tsx
+++ b/frontend/src/scenes/data-warehouse/viewLinkLogic.test.tsx
@@ -69,12 +69,19 @@ describe('viewLinkLogic', () => {
validateHandler.mockReturnValue([
200,
- { is_valid: true, msg: 'Validation query returned no results', hogql: null, results: [] },
+ {
+ is_valid: true,
+ msg: 'None of the sampled rows matched on these keys. Check that both key columns hold the same values, then validate again.',
+ hogql: null,
+ results: [],
+ },
])
logic.actions.setViewLinkValue('joining_table_key', 'id')
await expectLogic(logic).toDispatchActions(['validateJoinSuccess'])
expect(logic.values.saveDisabledReason).toBeNull()
- expect(logic.values.joinValidation.msg).toBe('Validation query returned no results')
+ expect(logic.values.joinValidation.msg).toBe(
+ 'None of the sampled rows matched on these keys. Check that both key columns hold the same values, then validate again.'
+ )
})
it('buckets tables into picker groups and keeps unknown types reachable', async () => {
diff --git a/products/data_warehouse/backend/presentation/views/view_link.py b/products/data_warehouse/backend/presentation/views/view_link.py
index 3690c115617a..5379018a9a0e 100644
--- a/products/data_warehouse/backend/presentation/views/view_link.py
+++ b/products/data_warehouse/backend/presentation/views/view_link.py
@@ -31,6 +31,11 @@ from products.data_tools.backend.facade.models import DataWarehouseJoin
MATCH_RATE_SAMPLE_SIZE = 10_000
PREVIEW_SCAN_ROWS = 1000
PREVIEW_DISTINCT_PAIRS = 5
+NO_MATCHING_ROWS_MSG = (
+ "None of the sampled rows matched on these keys. Check that both key columns hold the same values, "
+ "then validate again."
+)
+
# The match-rate stats query tests each sampled source key against the joining table's key column,
# which reads that column in full. Cap its execution so this best-effort scan can't tie up warehouse
# query capacity; over-limit runs raise and fall back to null stats rather than sampling the joining
@@ -388,7 +393,7 @@ class ViewLinkViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewset
response_data["results"] = query_response.results
response_data["is_valid"] = True
if len(query_response.results) == 0:
- response_data["msg"] = "Validation query returned no results"
+ response_data["msg"] = NO_MATCHING_ROWS_MSG
total_rows, matched_rows, match_rate = _join_match_stats(
team=self.team,
database=database,
diff --git a/products/data_warehouse/backend/tests/api/test_view_link.py b/products/data_warehouse/backend/tests/api/test_view_link.py
index 3d4bead85e6f..f886dfd9a1c0 100644
--- a/products/data_warehouse/backend/tests/api/test_view_link.py
+++ b/products/data_warehouse/backend/tests/api/test_view_link.py
@@ -12,6 +12,7 @@ from posthog.hogql.database.database import Database
from posthog.hogql.query import HogQLQueryExecutor
from products.data_tools.backend.models.join import DataWarehouseJoin
+from products.data_warehouse.backend.presentation.views.view_link import NO_MATCHING_ROWS_MSG
from products.warehouse_sources.backend.facade.models import (
DataWarehouseCredential,
DataWarehouseTable,
@@ -370,6 +371,14 @@ def _mock_execute_hogql_with_stats_side_effect(*args, **kwargs):
return query_response
+def _mock_execute_hogql_no_matching_rows_side_effect(*args, **kwargs):
+ """Like the plain side effect, but the sampled join finds no matching rows."""
+ query_response = _mock_execute_hogql_side_effect(*args, **kwargs)
+ if "countIf" not in (query_response.hogql or ""):
+ query_response.results = []
+ return query_response
+
+
def _mock_execute_hogql_stats_error_side_effect(*args, **kwargs):
query_response = _mock_execute_hogql_side_effect(*args, **kwargs)
if "countIf" in (query_response.hogql or ""):
@@ -457,6 +466,23 @@ class TestViewLinkValidation(APIBaseTest):
f"SELECT DISTINCT source_key, joining_key FROM (SELECT {payload['source_table_key']} AS source_key, validation.{payload['joining_table_key']} AS joining_key FROM {payload['source_table_name']} LIMIT 1000) LIMIT 5",
)
+ @patch(f"{PATH}.execute_hogql_query", side_effect=_mock_execute_hogql_no_matching_rows_side_effect)
+ def test_validate_warns_when_no_sampled_rows_match(self, _):
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/warehouse_view_links/validate/",
+ {
+ "source_table_name": "events",
+ "source_table_key": "uuid",
+ "joining_table_name": "persons",
+ "joining_table_key": "id",
+ },
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
+ data = response.json()
+ self.assertTrue(data["is_valid"])
+ self.assertEqual(data["msg"], NO_MATCHING_ROWS_MSG)
+
@patch(f"{PATH}.execute_hogql_query", side_effect=_mock_execute_hogql_with_stats_side_effect)
def test_validate_returns_columns_and_match_stats(self, _):
response = self.client.post(