-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product-assistant): actor's property taxonomy query (#25539)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ef1a63f
commit 07218a5
Showing
13 changed files
with
651 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
posthog/hogql_queries/ai/actors_property_taxonomy_query_runner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from typing import Optional | ||
|
||
from posthog.hogql import ast | ||
from posthog.hogql.printer import to_printed_hogql | ||
from posthog.hogql.query import execute_hogql_query | ||
from posthog.hogql_queries.ai.utils import TaxonomyCacheMixin | ||
from posthog.hogql_queries.query_runner import QueryRunner | ||
from posthog.schema import ( | ||
ActorsPropertyTaxonomyQuery, | ||
ActorsPropertyTaxonomyQueryResponse, | ||
CachedActorsPropertyTaxonomyQueryResponse, | ||
) | ||
|
||
|
||
class ActorsPropertyTaxonomyQueryRunner(TaxonomyCacheMixin, QueryRunner): | ||
query: ActorsPropertyTaxonomyQuery | ||
response: ActorsPropertyTaxonomyQueryResponse | ||
cached_response: CachedActorsPropertyTaxonomyQueryResponse | ||
|
||
def calculate(self): | ||
query = self.to_query() | ||
hogql = to_printed_hogql(query, self.team) | ||
|
||
response = execute_hogql_query( | ||
query_type="ActorsPropertyTaxonomyQuery", | ||
query=query, | ||
team=self.team, | ||
timings=self.timings, | ||
modifiers=self.modifiers, | ||
limit_context=self.limit_context, | ||
) | ||
|
||
results = ( | ||
{ | ||
"sample_values": response.results[0][0], | ||
"sample_count": response.results[0][1], | ||
} | ||
if response.results | ||
else { | ||
"sample_values": [], | ||
"sample_count": 0, | ||
} | ||
) | ||
|
||
return ActorsPropertyTaxonomyQueryResponse( | ||
results=results, | ||
timings=response.timings, | ||
hogql=hogql, | ||
modifiers=self.modifiers, | ||
) | ||
|
||
def to_query(self) -> ast.SelectQuery | ast.SelectUnionQuery: | ||
query = ast.SelectQuery( | ||
select=[ | ||
ast.Call(name="groupArray", args=[ast.Field(chain=["prop"])], params=[ast.Constant(value=5)]), | ||
ast.Call(name="count", args=[]), | ||
], | ||
select_from=ast.JoinExpr(table=self._get_subquery()), | ||
) | ||
|
||
return query | ||
|
||
@property | ||
def _actor_type(self) -> str: | ||
if self.query.group_type_index is not None: | ||
return "group" | ||
return "person" | ||
|
||
@property | ||
def _origin(self) -> str: | ||
if self._actor_type == "person": | ||
return "persons" | ||
return "groups" | ||
|
||
def _subquery_filter(self) -> Optional[ast.Expr]: | ||
field_filter = ast.Call( | ||
name="isNotNull", | ||
args=[ast.Field(chain=["prop"])], | ||
) | ||
|
||
if self._actor_type == "group": | ||
return ast.And( | ||
exprs=[ | ||
field_filter, | ||
ast.CompareOperation( | ||
left=ast.Field(chain=["index"]), | ||
op=ast.CompareOperationOp.Eq, | ||
right=ast.Constant(value=self.query.group_type_index), | ||
), | ||
] | ||
) | ||
|
||
return field_filter | ||
|
||
def _get_subquery(self) -> ast.SelectQuery: | ||
query = ast.SelectQuery( | ||
select=[ast.Alias(expr=ast.Field(chain=["properties", self.query.property]), alias="prop")], | ||
distinct=True, | ||
select_from=ast.JoinExpr(table=ast.Field(chain=[self._origin])), | ||
where=self._subquery_filter(), | ||
order_by=[ast.OrderExpr(expr=ast.Field(chain=["created_at"]), order="DESC")], | ||
) | ||
return query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.