Skip to content

Commit

Permalink
Adjust translate_hogql()
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Sep 4, 2024
1 parent 4975e9d commit 9472432
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion posthog/hogql/hogql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from posthog.hogql.errors import NotImplementedError, QueryError, SyntaxError
from posthog.hogql.parser import parse_expr
from posthog.hogql.printer import prepare_ast_for_printing, print_prepared_ast
from posthog.queries.util import alias_poe_mode_for_legacy


# This is called only from "non-hogql-based" insights to translate HogQL expressions into ClickHouse SQL
Expand All @@ -22,12 +23,16 @@ def translate_hogql(
if query == "":
raise QueryError("Empty query")

# TRICKY: As `translate_hogql` is only used in legacy queries (not the all-HogQL ones), we must guard against
# the PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_JOINED being used here, as that is not supported in legacyz
actual_poe_mode = context.modifiers.personsOnEventsMode
try:
context.modifiers.personsOnEventsMode = alias_poe_mode_for_legacy(actual_poe_mode)

Check failure on line 30 in posthog/hogql/hogql.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 1 to "alias_poe_mode_for_legacy" has incompatible type "PersonsOnEventsMode | None"; expected "PersonsOnEventsMode"
# Create a fake query that selects from "events" to have fields to select from.
if context.database is None:
if context.team_id is None:
raise ValueError("Cannot translate HogQL for a filter with no team specified")
context.database = create_hogql_database(context.team_id)
context.database = create_hogql_database(context.team_id, context.modifiers)
node = parse_expr(query, placeholders=placeholders)
select_query = ast.SelectQuery(select=[node], select_from=ast.JoinExpr(table=ast.Field(chain=["events"])))

Expand All @@ -46,3 +51,5 @@ def translate_hogql(
)
except (NotImplementedError, SyntaxError):
raise
finally:
context.modifiers.personsOnEventsMode = actual_poe_mode # Restore the original value
2 changes: 1 addition & 1 deletion posthog/hogql/test/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_fields_and_properties(self):
context = HogQLContext(team_id=self.team.pk)
self.assertEqual(
self._expr("person.properties.bla", context),
"events__pdi__person.properties___bla",
"events__person.properties___bla",
)

with override_settings(PERSON_ON_EVENTS_OVERRIDE=True):
Expand Down

0 comments on commit 9472432

Please sign in to comment.