Skip to content

Commit

Permalink
Allow placeholders in place of ratio expressions in hogql
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert09 committed Oct 13, 2023
1 parent 1ecf289 commit 0a85de2
Show file tree
Hide file tree
Showing 7 changed files with 764 additions and 743 deletions.
4 changes: 2 additions & 2 deletions posthog/hogql/grammar/HogQLLexer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from HogQLLexer.g4 by ANTLR 4.13.0
# Generated from HogQLLexer.g4 by ANTLR 4.13.1
from antlr4 import *
from io import StringIO
import sys
Expand Down Expand Up @@ -1252,7 +1252,7 @@ class HogQLLexer(Lexer):

def __init__(self, input=None, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.13.0")
self.checkVersion("4.13.1")
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
self._predicates = None
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/grammar/HogQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ joinConstraintClause
sampleClause: SAMPLE ratioExpr (OFFSET ratioExpr)?;
orderExprList: orderExpr (COMMA orderExpr)*;
orderExpr: columnExpr (ASCENDING | DESCENDING | DESC)? (NULLS (FIRST | LAST))? (COLLATE STRING_LITERAL)?;
ratioExpr: numberLiteral (SLASH numberLiteral)?;
ratioExpr: PLACEHOLDER | numberLiteral (SLASH numberLiteral)?;
settingExprList: settingExpr (COMMA settingExpr)*;
settingExpr: identifier EQ_SINGLE literal;

Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/grammar/HogQLParser.interp

Large diffs are not rendered by default.

1,484 changes: 751 additions & 733 deletions posthog/hogql/grammar/HogQLParser.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion posthog/hogql/grammar/HogQLParserVisitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from HogQLParser.g4 by ANTLR 4.13.0
# Generated from HogQLParser.g4 by ANTLR 4.13.1
from antlr4 import *
if "." in __name__:
from .HogQLParser import HogQLParser
Expand Down
3 changes: 3 additions & 0 deletions posthog/hogql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ def visitOrderExpr(self, ctx: HogQLParser.OrderExprContext):
return ast.OrderExpr(expr=self.visit(ctx.columnExpr()), order=cast(Literal["ASC", "DESC"], order))

def visitRatioExpr(self, ctx: HogQLParser.RatioExprContext):
if ctx.PLACEHOLDER():
return ast.Placeholder(field=parse_string_literal(ctx.PLACEHOLDER()))

number_literals = ctx.numberLiteral()

left = number_literals[0]
Expand Down
10 changes: 5 additions & 5 deletions posthog/hogql_queries/insights/trends/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ def _get_events_subquery(self) -> ast.SelectQuery:
{aggregation_operation} AS total,
dateTrunc({interval}, toTimeZone(toDateTime(timestamp), 'UTC')) AS day_start
FROM events AS e
%s
SAMPLE {sample}
WHERE {events_filter}
GROUP BY day_start
"""
% (self._sample_value()),
""",
placeholders={
**self.query_date_range.to_placeholders(),
"events_filter": self._events_filter(),
"aggregation_operation": self._aggregation_operation(),
"sample": self._sample_value(),
},
)

Expand Down Expand Up @@ -223,9 +223,9 @@ def _aggregation_operation(self) -> ast.Expr:
# Using string interpolation for SAMPLE due to HogQL limitations with `UNION ALL` and `SAMPLE` AST nodes
def _sample_value(self) -> str:
if self.query.samplingFactor is None:
return ""
return ast.RatioExpr(left=ast.Constant(value=1))

return f"SAMPLE {self.query.samplingFactor}"
return ast.RatioExpr(left=ast.Constant(value=self.query.samplingFactor))

@cached_property
def _breakdown(self):
Expand Down

0 comments on commit 0a85de2

Please sign in to comment.