Skip to content

Commit

Permalink
feat(hogql): select statements (#14131)
Browse files Browse the repository at this point in the history
* feat(hogql): select statements

* visitor

* cleanup

* parse limit by

* parse limit by

* merge limit clauses

* Update snapshots

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
mariusandra and github-actions[bot] authored Feb 13, 2023
1 parent b70b27c commit 6764620
Show file tree
Hide file tree
Showing 19 changed files with 1,794 additions and 1,206 deletions.
57 changes: 29 additions & 28 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1600,34 +1600,8 @@
"type": "array"
},
"response": {
"additionalProperties": false,
"description": "Cached query response",
"properties": {
"columns": {
"items": {
"type": "string"
},
"type": "array"
},
"hasMore": {
"type": "boolean"
},
"results": {
"items": {
"items": {},
"type": "array"
},
"type": "array"
},
"types": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": ["columns", "types", "results"],
"type": "object"
"$ref": "#/definitions/EventsQueryResponse",
"description": "Cached query response"
},
"select": {
"description": "Return a limited set of data. Required.",
Expand All @@ -1647,6 +1621,33 @@
"required": ["kind", "select"],
"type": "object"
},
"EventsQueryResponse": {
"additionalProperties": false,
"properties": {
"columns": {
"items": {},
"type": "array"
},
"hasMore": {
"type": "boolean"
},
"results": {
"items": {
"items": {},
"type": "array"
},
"type": "array"
},
"types": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": ["columns", "types", "results"],
"type": "object"
},
"FeaturePropertyFilter": {
"additionalProperties": false,
"properties": {
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ export interface NewEntityNode extends EntityNode {
event?: string | null
}

export interface EventsQueryResponse {
columns: any[]
types: string[]
results: any[][]
hasMore?: boolean
}

export interface EventsQuery extends DataNode {
kind: NodeKind.EventsQuery
/** Return a limited set of data. Required. */
Expand Down Expand Up @@ -172,12 +179,7 @@ export interface EventsQuery extends DataNode {
/** Columns to order by */
orderBy?: string[]

response?: {
columns: string[]
types: string[]
results: any[][]
hasMore?: boolean
}
response?: EventsQueryResponse
}

export interface PersonsNode extends DataNode {
Expand Down
30 changes: 29 additions & 1 deletion posthog/hogql/ast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from enum import Enum
from typing import Any, List, Literal
from typing import Any, List, Literal, Optional, Union

from pydantic import BaseModel, Extra

Expand Down Expand Up @@ -102,3 +102,31 @@ class Placeholder(Expr):
class Call(Expr):
name: str
args: List[Expr]


class JoinExpr(Expr):
table: Optional[Union["SelectQuery", Field]] = None
table_final: Optional[bool] = None
alias: Optional[str] = None
join_type: Optional[str] = None
join_constraint: Optional[Expr] = None
join_expr: Optional["JoinExpr"] = None


class SelectQuery(Expr):
select: List[Expr]
distinct: Optional[bool] = None
select_from: Optional[JoinExpr] = None
where: Optional[Expr] = None
prewhere: Optional[Expr] = None
having: Optional[Expr] = None
group_by: Optional[List[Expr]] = None
order_by: Optional[List[OrderExpr]] = None
limit: Optional[Expr] = None
limit_by: Optional[List[Expr]] = None
limit_with_ties: Optional[bool] = None
offset: Optional[Expr] = None


JoinExpr.update_forward_refs(SelectQuery=SelectQuery)
JoinExpr.update_forward_refs(JoinExpr=JoinExpr)
3 changes: 3 additions & 0 deletions posthog/hogql/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@
"person.created_at",
"person.properties",
]

# Never return more rows than this in top level HogQL SELECT statements
MAX_SELECT_RETURNED_ROWS = 65535
5 changes: 5 additions & 0 deletions posthog/hogql/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ class HogQLContext:
field_access_logs: List[HogQLFieldAccess] = field(default_factory=list)
# Did the last calls to translate_hogql since setting these to False contain any of the following
found_aggregation: bool = False
# Do we need to join the persons table or not
using_person_on_events: bool = True
# If set, allows printing full SELECT queries in ClickHouse
select_team_id: Optional[int] = None
# Do we apply a limit of MAX_SELECT_RETURNED_ROWS=65535 to the topmost select query?
limit_top_select: bool = True
4 changes: 1 addition & 3 deletions posthog/hogql/grammar/HogQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ selectStmt:
groupByClause? (WITH (CUBE | ROLLUP))? (WITH TOTALS)?
havingClause?
orderByClause?
limitByClause?
limitClause?
settingsClause?
;
Expand All @@ -37,8 +36,7 @@ groupByClause: GROUP BY ((CUBE | ROLLUP) LPAREN columnExprList RPAREN | columnEx
havingClause: HAVING columnExpr;
orderByClause: ORDER BY orderExprList;
projectionOrderByClause: ORDER BY columnExprList;
limitByClause: LIMIT limitExpr BY columnExprList;
limitClause: LIMIT limitExpr (WITH TIES)?;
limitClause: LIMIT limitExpr ((WITH TIES) | BY columnExprList)?;
settingsClause: SETTINGS settingExprList;

joinExpr
Expand Down
3 changes: 1 addition & 2 deletions posthog/hogql/grammar/HogQLParser.interp

Large diffs are not rendered by default.

Loading

0 comments on commit 6764620

Please sign in to comment.