Skip to content

Commit

Permalink
fix: ensure we dont overwrite events fields with group mappings (#17965)
Browse files Browse the repository at this point in the history
Ensure we dont overwrite events fields with group mappings
  • Loading branch information
Gilbert09 authored Oct 13, 2023
1 parent 61d224c commit bbe24ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion posthog/hogql/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions posthog/hogql/database/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

0 comments on commit bbe24ba

Please sign in to comment.