Skip to content

Commit

Permalink
table field for metadata expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Oct 2, 2023
1 parent 30f66d2 commit f4884ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion posthog/hogql/hogql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def translate_hogql(
query: str,
context: HogQLContext,
dialect: Literal["hogql", "clickhouse"] = "clickhouse",
table: str = "events",
*,
events_table_alias: Optional[str] = None,
placeholders: Optional[Dict[str, ast.Expr]] = None,
Expand All @@ -29,7 +30,7 @@ def translate_hogql(
raise ValueError("Cannot translate HogQL for a filter with no team specified")
context.database = create_hogql_database(context.team_id)
node = parse_expr(query, placeholders=placeholders)
select_query = ast.SelectQuery(select=[node], select_from=ast.JoinExpr(table=ast.Field(chain=["events"])))
select_query = ast.SelectQuery(select=[node], select_from=ast.JoinExpr(table=ast.Field(chain=[table])))
if events_table_alias is not None:
select_query.select_from.alias = events_table_alias
prepared_select_query: ast.SelectQuery = cast(
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_hogql_metadata(
try:
if isinstance(query.expr, str):
context = HogQLContext(team_id=team.pk)
translate_hogql(query.expr, context=context)
translate_hogql(query.expr, context=context, table=query.table or "events")
elif isinstance(query.select, str):
context = HogQLContext(team_id=team.pk, enable_select_queries=True)

Expand Down
19 changes: 17 additions & 2 deletions posthog/hogql/test/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
class TestMetadata(ClickhouseTestMixin, APIBaseTest):
maxDiff = None

def _expr(self, query: str) -> HogQLMetadataResponse:
return get_hogql_metadata(query=HogQLMetadata(kind="HogQLMetadata", expr=query, response=None), team=self.team)
def _expr(self, query: str, table: str = "events") -> HogQLMetadataResponse:
return get_hogql_metadata(
query=HogQLMetadata(kind="HogQLMetadata", expr=query, table=table, response=None), team=self.team
)

def _select(self, query: str) -> HogQLMetadataResponse:
return get_hogql_metadata(
Expand Down Expand Up @@ -111,6 +113,19 @@ def test_metadata_expr_resolve_error(self):
},
)

def test_metadata_table(self):
metadata = self._expr("timestamp", "events")
self.assertEqual(metadata.isValid, True)

metadata = self._expr("timestamp", "persons")
self.assertEqual(metadata.isValid, False)

metadata = self._expr("is_identified", "events")
self.assertEqual(metadata.isValid, False)

metadata = self._expr("is_identified", "persons")
self.assertEqual(metadata.isValid, True)

def test_metadata_in_cohort(self):
cohort = Cohort.objects.create(team=self.team, name="cohort_name")
query = (
Expand Down

0 comments on commit f4884ed

Please sign in to comment.