Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hogql): select statements #14131

Merged
merged 7 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -134,6 +134,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 @@ -170,12 +177,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)?;
Comment on lines -40 to +39
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and CH doesn't support BOTH a "with ties" and a "by x" ending on "limit" at the same time, though the parser seemed to allow it. Fixed.

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