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): intervals with string constants #27638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1,299 changes: 665 additions & 634 deletions hogql_parser/HogQLParser.cpp

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions hogql_parser/HogQLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,16 @@ class HogQLParser : public antlr4::Parser {
virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

class ColumnExprIntervalStringContext : public ColumnExprContext {
public:
ColumnExprIntervalStringContext(ColumnExprContext *ctx);

antlr4::tree::TerminalNode *INTERVAL();
antlr4::tree::TerminalNode *STRING_LITERAL();

virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};

class ColumnExprTrimContext : public ColumnExprContext {
public:
ColumnExprTrimContext(ColumnExprContext *ctx);
Expand Down
2 changes: 1 addition & 1 deletion hogql_parser/HogQLParser.interp

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions hogql_parser/HogQLParserBaseVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ class HogQLParserBaseVisitor : public HogQLParserVisitor {
return visitChildren(ctx);
}

virtual std::any visitColumnExprIntervalString(HogQLParser::ColumnExprIntervalStringContext *ctx) override {
return visitChildren(ctx);
}

virtual std::any visitColumnExprTrim(HogQLParser::ColumnExprTrimContext *ctx) override {
return visitChildren(ctx);
}
Expand Down
2 changes: 2 additions & 0 deletions hogql_parser/HogQLParserVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class HogQLParserVisitor : public antlr4::tree::AbstractParseTreeVisitor {

virtual std::any visitColumnExprNullPropertyAccess(HogQLParser::ColumnExprNullPropertyAccessContext *context) = 0;

virtual std::any visitColumnExprIntervalString(HogQLParser::ColumnExprIntervalStringContext *context) = 0;

virtual std::any visitColumnExprTrim(HogQLParser::ColumnExprTrimContext *context) = 0;

virtual std::any visitColumnExprTagElement(HogQLParser::ColumnExprTagElementContext *context) = 0;
Expand Down
68 changes: 68 additions & 0 deletions hogql_parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,74 @@ class HogQLParseTreeConverter : public HogQLParserBaseVisitor {
RETURN_NEW_AST_NODE("Call", "{s:s,s:[N]}", "name", name, "args", visitAsPyObject(ctx->columnExpr()));
}

VISIT(ColumnExprIntervalString) {
if (!ctx->STRING_LITERAL()) {
throw NotImplementedError("Unsupported interval type: missing string literal");
}

// The text should contain something like "5 day", "2 weeks", etc.
std::string text = parse_string_literal_ctx(ctx->STRING_LITERAL());

auto space_pos = text.find(' ');
if (space_pos == std::string::npos) {
throw NotImplementedError("Unsupported interval type: must be in the format '<count> <unit>'");
}
std::string count_str = text.substr(0, space_pos);
std::string unit_str = text.substr(space_pos + 1);

for (char c : count_str) {
if (!std::isdigit(static_cast<unsigned char>(c))) {
throw NotImplementedError(("Unsupported interval count: " + count_str).c_str());
}
}
int count_int = std::stoi(count_str);

std::string name;
if (unit_str == "second" || unit_str == "seconds") {
name = "toIntervalSecond";
} else if (unit_str == "minute" || unit_str == "minutes") {
name = "toIntervalMinute";
} else if (unit_str == "hour" || unit_str == "hours") {
name = "toIntervalHour";
} else if (unit_str == "day" || unit_str == "days") {
name = "toIntervalDay";
} else if (unit_str == "week" || unit_str == "weeks") {
name = "toIntervalWeek";
} else if (unit_str == "month" || unit_str == "months") {
name = "toIntervalMonth";
} else if (unit_str == "quarter" || unit_str == "quarters") {
name = "toIntervalQuarter";
} else if (unit_str == "year" || unit_str == "years") {
name = "toIntervalYear";
} else {
throw NotImplementedError(("Unsupported interval unit: " + unit_str).c_str());
}

PyObject* py_count = PyLong_FromLong(count_int);
if (!py_count) {
throw PyInternalError();
}
PyObject* constant_count = build_ast_node("Constant", "{s:N}", "value", py_count);
if (!constant_count) {
Py_DECREF(py_count);
throw PyInternalError();
}

PyObject* ret = build_ast_node(
"Call",
"{s:s,s:[O]}", // s:[O] means "args" => [the single PyObject]
"name", name.c_str(),
"args", constant_count
);

if (!ret) {
Py_DECREF(constant_count);
throw PyInternalError();
}

return ret;
}

VISIT(ColumnExprIsNull) {
PyObject* null_constant = build_ast_node("Constant", "{s:O}", "value", Py_None);
if (!null_constant) throw PyInternalError();
Expand Down
2 changes: 1 addition & 1 deletion hogql_parser/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

setup(
name="hogql_parser",
version="1.0.47",
version="1.0.48",
url="https://github.com/PostHog/posthog/tree/master/hogql_parser",
author="PostHog Inc.",
author_email="[email protected]",
Expand Down
1 change: 1 addition & 0 deletions posthog/hogql/grammar/HogQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ columnExpr
| CAST LPAREN columnExpr AS columnTypeExpr RPAREN # ColumnExprCast
| DATE STRING_LITERAL # ColumnExprDate
// | EXTRACT LPAREN interval FROM columnExpr RPAREN # ColumnExprExtract // Interferes with a function call
| INTERVAL STRING_LITERAL # ColumnExprIntervalString
| INTERVAL columnExpr interval # ColumnExprInterval
| SUBSTRING LPAREN columnExpr FROM columnExpr (FOR columnExpr)? RPAREN # ColumnExprSubstring
| TIMESTAMP STRING_LITERAL # ColumnExprTimestamp
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/grammar/HogQLParser.interp

Large diffs are not rendered by default.

Loading
Loading