-
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(hogql): placeholders and cleanup (#14116)
* feat(hogql): placeholders, cleanup * this wasn't used * placeholder tests * Update snapshots * fix * use ANSI SQL compatible strings * Update snapshots * final tweaks * visitor pattern * placeholders via the visitor pattern * test everything visitor * must return --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b68bfcf
commit 3940ab5
Showing
28 changed files
with
2,580 additions
and
2,183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# fields you can select from in the events query | ||
EVENT_FIELDS = [ | ||
"id", | ||
"uuid", | ||
"event", | ||
"timestamp", | ||
"properties", | ||
"elements_chain", | ||
"created_at", | ||
"distinct_id", | ||
"team_id", | ||
] | ||
# "person.*" fields you can select from in the events query | ||
EVENT_PERSON_FIELDS = ["id", "created_at", "properties"] | ||
|
||
# HogQL -> ClickHouse allowed transformations | ||
CLICKHOUSE_FUNCTIONS = { | ||
# arithmetic | ||
"abs": "abs", | ||
"max2": "max2", | ||
"min2": "min2", | ||
# type conversions | ||
"toInt": "toInt64OrNull", | ||
"toFloat": "toFloat64OrNull", | ||
"toDecimal": "toDecimal64OrNull", | ||
"toDate": "toDateOrNull", | ||
"toDateTime": "parseDateTimeBestEffort", | ||
"toIntervalSecond": "toIntervalSecond", | ||
"toIntervalMinute": "toIntervalMinute", | ||
"toIntervalHour": "toIntervalHour", | ||
"toIntervalDay": "toIntervalDay", | ||
"toIntervalWeek": "toIntervalWeek", | ||
"toIntervalMonth": "toIntervalMonth", | ||
"toIntervalQuarter": "toIntervalQuarter", | ||
"toIntervalYear": "toIntervalYear", | ||
"toString": "toString", | ||
# date functions | ||
"now": "now", | ||
"NOW": "now", | ||
"toMonday": "toMonday", | ||
"toStartOfYear": "toStartOfYear", | ||
"toStartOfQuarter": "toStartOfQuarter", | ||
"toStartOfMonth": "toStartOfMonth", | ||
"toStartOfWeek": "toStartOfWeek", | ||
"toStartOfDay": "toStartOfDay", | ||
"toStartOfHour": "toStartOfHour", | ||
"toStartOfMinute": "toStartOfMinute", | ||
"toStartOfSecond": "toStartOfSecond", | ||
"toStartOfFiveMinutes": "toStartOfFiveMinutes", | ||
"toStartOfTenMinutes": "toStartOfTenMinutes", | ||
"toStartOfFifteenMinutes": "toStartOfFifteenMinutes", | ||
"toTimezone": "toTimezone", | ||
"age": "age", | ||
"dateDiff": "dateDiff", | ||
"dateTrunc": "dateTrunc", | ||
"formatDateTime": "formatDateTime", | ||
# string functions | ||
"length": "lengthUTF8", | ||
"empty": "empty", | ||
"notEmpty": "notEmpty", | ||
"leftPad": "leftPad", | ||
"rightPad": "rightPad", | ||
"lower": "lower", | ||
"upper": "upper", | ||
"repeat": "repeat", | ||
"format": "format", | ||
"concat": "concat", | ||
"coalesce": "coalesce", | ||
"substring": "substringUTF8", | ||
"appendTrailingCharIfAbsent": "appendTrailingCharIfAbsent", | ||
"endsWith": "endsWith", | ||
"startsWith": "startsWith", | ||
"trim": "trimBoth", | ||
"trimLeft": "trimLeft", | ||
"trimRight": "trimRight", | ||
"extractTextFromHTML": "extractTextFromHTML", | ||
"like": "like", | ||
"ilike": "ilike", | ||
"notLike": "notLike", | ||
"replace": "replace", | ||
"replaceOne": "replaceOne", | ||
# array functions | ||
"tuple": "tuple", | ||
# conditional | ||
"ifElse": "if", | ||
"multiIf": "multiIf", | ||
# rounding | ||
"round": "round", | ||
"floor": "floor", | ||
"ceil": "ceil", | ||
"trunc": "trunc", | ||
} | ||
# Permitted HogQL aggregations | ||
HOGQL_AGGREGATIONS = { | ||
"count": 0, | ||
"countIf": 1, | ||
"countDistinct": 1, | ||
"countDistinctIf": 2, | ||
"min": 1, | ||
"minIf": 2, | ||
"max": 1, | ||
"maxIf": 2, | ||
"sum": 1, | ||
"sumIf": 2, | ||
"avg": 1, | ||
"avgIf": 2, | ||
"any": 1, | ||
"anyIf": 2, | ||
} | ||
# Keywords passed to ClickHouse without transformation | ||
KEYWORDS = ["true", "false", "null"] | ||
|
||
# Allow-listed fields returned when you select "*" from events. Person and group fields will be nested later. | ||
SELECT_STAR_FROM_EVENTS_FIELDS = [ | ||
"uuid", | ||
"event", | ||
"properties", | ||
"timestamp", | ||
"team_id", | ||
"distinct_id", | ||
"elements_chain", | ||
"created_at", | ||
"person.id", | ||
"person.created_at", | ||
"person.properties", | ||
] |
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,23 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Dict, List, Literal, Optional | ||
|
||
|
||
@dataclass | ||
class HogQLFieldAccess: | ||
input: List[str] | ||
type: Optional[Literal["event", "event.properties", "person", "person.properties"]] | ||
field: Optional[str] | ||
sql: str | ||
|
||
|
||
@dataclass | ||
class HogQLContext: | ||
"""Context given to a HogQL expression printer""" | ||
|
||
# If set, will save string constants to this dict. Inlines strings into the query if None. | ||
values: Dict = field(default_factory=dict) | ||
# List of field and property accesses found in the expression | ||
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 | ||
using_person_on_events: bool = True |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.