From bbe24ba7dec1945208641fb822caa7181805386d Mon Sep 17 00:00:00 2001 From: Tom Owers Date: Fri, 13 Oct 2023 11:52:24 +0100 Subject: [PATCH] fix: ensure we dont overwrite events fields with group mappings (#17965) Ensure we dont overwrite events fields with group mappings --- posthog/hogql/database/database.py | 3 ++- posthog/hogql/database/test/test_database.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py index d5ebcabddeb37..b1771404c3c2b 100644 --- a/posthog/hogql/database/database.py +++ b/posthog/hogql/database/database.py @@ -123,7 +123,8 @@ def create_hogql_database(team_id: int, modifiers: Optional[HogQLQueryModifiers] database.events.fields["person_id"] = StringDatabaseField(name="person_id") for mapping in GroupTypeMapping.objects.filter(team=team): - database.events.fields[mapping.group_type] = FieldTraverser(chain=[f"group_{mapping.group_type_index}"]) + if database.events.fields.get(mapping.group_type) is None: + database.events.fields[mapping.group_type] = FieldTraverser(chain=[f"group_{mapping.group_type_index}"]) for view in DataWarehouseViewLink.objects.filter(team_id=team.pk).exclude(deleted=True): table = database.get_table(view.table) diff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.py index 89bdd9ba31e19..1ea0583c4e349 100644 --- a/posthog/hogql/database/test/test_database.py +++ b/posthog/hogql/database/test/test_database.py @@ -7,6 +7,8 @@ from parameterized import parameterized from posthog.hogql.database.database import create_hogql_database, serialize_database +from posthog.hogql.database.models import FieldTraverser, StringDatabaseField +from posthog.models.group_type_mapping import GroupTypeMapping from posthog.test.base import BaseTest from posthog.warehouse.models import DataWarehouseTable, DataWarehouseCredential from posthog.hogql.query import execute_hogql_query @@ -62,3 +64,15 @@ def test_database_with_warehouse_tables(self, patch_execute): response.clickhouse, f"SELECT whatever.id FROM s3Cluster('posthog', %(hogql_val_0_sensitive)s, %(hogql_val_3_sensitive)s, %(hogql_val_4_sensitive)s, %(hogql_val_1)s, %(hogql_val_2)s) AS whatever LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1", ) + + def test_database_group_type_mappings(self): + GroupTypeMapping.objects.create(team=self.team, group_type="test", group_type_index=0) + db = create_hogql_database(team_id=self.team.pk) + + assert db.events.fields["test"] == FieldTraverser(chain=["group_0"]) + + def test_database_group_type_mappings_overwrite(self): + GroupTypeMapping.objects.create(team=self.team, group_type="event", group_type_index=0) + db = create_hogql_database(team_id=self.team.pk) + + assert db.events.fields["event"] == StringDatabaseField(name="event")