Skip to content

Commit

Permalink
feat(hogql): support constant values in HogQLQuery (#17733)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Oct 3, 2023
1 parent 70379e7 commit d0310b7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,10 @@
"response": {
"$ref": "#/definitions/HogQLQueryResponse",
"description": "Cached query response"
},
"values": {
"description": "Constant values that can be referenced with the {placeholder} syntax in the query",
"type": "object"
}
},
"required": ["kind", "query"],
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export interface HogQLQuery extends DataNode {
kind: NodeKind.HogQLQuery
query: string
filters?: HogQLFilters
/** Constant values that can be referenced with the {placeholder} syntax in the query */
values?: Record<string, any>
response?: HogQLQueryResponse
}

Expand Down
7 changes: 7 additions & 0 deletions posthog/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from posthog.api.routing import StructuredViewSetMixin
from posthog.clickhouse.query_tagging import tag_queries
from posthog.errors import ExposedCHQueryError
from posthog.hogql import ast
from posthog.hogql.ai import PromptUnclear, write_sql_from_prompt
from posthog.hogql.database.database import create_hogql_database, serialize_database
from posthog.hogql.errors import HogQLException
Expand Down Expand Up @@ -222,11 +223,17 @@ def process_query(
return _unwrap_pydantic_dict(events_response)
elif query_kind == "HogQLQuery":
hogql_query = HogQLQuery.model_validate(query_json)
values = (
{key: ast.Constant(value=value) for key, value in hogql_query.values.items()}
if hogql_query.values
else None
)
hogql_response = execute_hogql_query(
query_type="HogQLQuery",
query=hogql_query.query,
team=team,
filters=hogql_query.filters,
placeholders=values,
default_limit=default_limit,
)
return _unwrap_pydantic_dict(hogql_response)
Expand Down
19 changes: 19 additions & 0 deletions posthog/api/test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,22 @@ def test_full_hogql_query_view(self):
["sign out", "4", "test_val3"],
],
)

def test_full_hogql_query_values(self):
random_uuid = str(UUIDT())
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(team=self.team, event="sign up", distinct_id=random_uuid, properties={"key": "test_val1"})
flush_persons_and_events()

with freeze_time("2020-01-10 12:14:00"):
response = process_query(
team=self.team,
query_json={
"kind": "HogQLQuery",
"query": "select count() from events where distinct_id = {random_uuid}",
"values": {"random_uuid": random_uuid},
},
)

self.assertEqual(response.get("results", [])[0][0], 20)
3 changes: 3 additions & 0 deletions posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ class HogQLQuery(BaseModel):
kind: Literal["HogQLQuery"] = "HogQLQuery"
query: str
response: Optional[HogQLQueryResponse] = Field(default=None, description="Cached query response")
values: Optional[Dict[str, Any]] = Field(
default=None, description="Constant values that can be referenced with the {placeholder} syntax in the query"
)


class PersonsNode(BaseModel):
Expand Down

0 comments on commit d0310b7

Please sign in to comment.