From 82b4d63d779db4f2de1c40eacc6636db7d5248d9 Mon Sep 17 00:00:00 2001 From: Alexander Spicer Date: Fri, 25 Oct 2024 11:20:25 -0700 Subject: [PATCH] clause --- hogql_parser/HogQLParser.cpp | 40 ++++++++++----------- hogql_parser/HogQLParser.h | 14 ++++---- hogql_parser/HogQLParser.interp | 2 +- hogql_parser/HogQLParserBaseVisitor.h | 2 +- hogql_parser/HogQLParserVisitor.h | 2 +- hogql_parser/parser.cpp | 2 +- posthog/hogql/grammar/HogQLParser.g4 | 4 +-- posthog/hogql/grammar/HogQLParser.interp | 2 +- posthog/hogql/grammar/HogQLParser.py | 26 +++++++------- posthog/hogql/grammar/HogQLParserVisitor.py | 4 +-- posthog/hogql/parser.py | 5 +-- 11 files changed, 50 insertions(+), 53 deletions(-) diff --git a/hogql_parser/HogQLParser.cpp b/hogql_parser/HogQLParser.cpp index d090088bcf360..d964e54b51e2c 100644 --- a/hogql_parser/HogQLParser.cpp +++ b/hogql_parser/HogQLParser.cpp @@ -56,7 +56,7 @@ void hogqlparserParserInitialize() { "statement", "returnStmt", "throwStmt", "catchBlock", "tryCatchStmt", "ifStmt", "whileStmt", "forStmt", "forInStmt", "funcStmt", "varAssignment", "exprStmt", "emptyStmt", "block", "kvPair", "kvPairList", "select", - "selectStmtWithParens", "subsequentSelectSetStmt", "selectSetStmt", + "selectStmtWithParens", "subsequentSelectSetClause", "selectSetStmt", "selectStmt", "withClause", "topClause", "fromClause", "arrayJoinClause", "windowClause", "prewhereClause", "whereClause", "groupByClause", "havingClause", "orderByClause", "projectionOrderByClause", "limitAndOffsetClause", @@ -2837,48 +2837,48 @@ HogQLParser::SelectStmtWithParensContext* HogQLParser::selectStmtWithParens() { return _localctx; } -//----------------- SubsequentSelectSetStmtContext ------------------------------------------------------------------ +//----------------- SubsequentSelectSetClauseContext ------------------------------------------------------------------ -HogQLParser::SubsequentSelectSetStmtContext::SubsequentSelectSetStmtContext(ParserRuleContext *parent, size_t invokingState) +HogQLParser::SubsequentSelectSetClauseContext::SubsequentSelectSetClauseContext(ParserRuleContext *parent, size_t invokingState) : ParserRuleContext(parent, invokingState) { } -HogQLParser::SelectStmtWithParensContext* HogQLParser::SubsequentSelectSetStmtContext::selectStmtWithParens() { +HogQLParser::SelectStmtWithParensContext* HogQLParser::SubsequentSelectSetClauseContext::selectStmtWithParens() { return getRuleContext(0); } -tree::TerminalNode* HogQLParser::SubsequentSelectSetStmtContext::EXCEPT() { +tree::TerminalNode* HogQLParser::SubsequentSelectSetClauseContext::EXCEPT() { return getToken(HogQLParser::EXCEPT, 0); } -tree::TerminalNode* HogQLParser::SubsequentSelectSetStmtContext::UNION() { +tree::TerminalNode* HogQLParser::SubsequentSelectSetClauseContext::UNION() { return getToken(HogQLParser::UNION, 0); } -tree::TerminalNode* HogQLParser::SubsequentSelectSetStmtContext::ALL() { +tree::TerminalNode* HogQLParser::SubsequentSelectSetClauseContext::ALL() { return getToken(HogQLParser::ALL, 0); } -tree::TerminalNode* HogQLParser::SubsequentSelectSetStmtContext::INTERSECT() { +tree::TerminalNode* HogQLParser::SubsequentSelectSetClauseContext::INTERSECT() { return getToken(HogQLParser::INTERSECT, 0); } -size_t HogQLParser::SubsequentSelectSetStmtContext::getRuleIndex() const { - return HogQLParser::RuleSubsequentSelectSetStmt; +size_t HogQLParser::SubsequentSelectSetClauseContext::getRuleIndex() const { + return HogQLParser::RuleSubsequentSelectSetClause; } -std::any HogQLParser::SubsequentSelectSetStmtContext::accept(tree::ParseTreeVisitor *visitor) { +std::any HogQLParser::SubsequentSelectSetClauseContext::accept(tree::ParseTreeVisitor *visitor) { if (auto parserVisitor = dynamic_cast(visitor)) - return parserVisitor->visitSubsequentSelectSetStmt(this); + return parserVisitor->visitSubsequentSelectSetClause(this); else return visitor->visitChildren(this); } -HogQLParser::SubsequentSelectSetStmtContext* HogQLParser::subsequentSelectSetStmt() { - SubsequentSelectSetStmtContext *_localctx = _tracker.createInstance(_ctx, getState()); - enterRule(_localctx, 46, HogQLParser::RuleSubsequentSelectSetStmt); +HogQLParser::SubsequentSelectSetClauseContext* HogQLParser::subsequentSelectSetClause() { + SubsequentSelectSetClauseContext *_localctx = _tracker.createInstance(_ctx, getState()); + enterRule(_localctx, 46, HogQLParser::RuleSubsequentSelectSetClause); #if __cplusplus > 201703L auto onExit = finally([=, this] { @@ -2938,12 +2938,12 @@ HogQLParser::SelectStmtWithParensContext* HogQLParser::SelectSetStmtContext::sel return getRuleContext(0); } -std::vector HogQLParser::SelectSetStmtContext::subsequentSelectSetStmt() { - return getRuleContexts(); +std::vector HogQLParser::SelectSetStmtContext::subsequentSelectSetClause() { + return getRuleContexts(); } -HogQLParser::SubsequentSelectSetStmtContext* HogQLParser::SelectSetStmtContext::subsequentSelectSetStmt(size_t i) { - return getRuleContext(i); +HogQLParser::SubsequentSelectSetClauseContext* HogQLParser::SelectSetStmtContext::subsequentSelectSetClause(size_t i) { + return getRuleContext(i); } @@ -2982,7 +2982,7 @@ HogQLParser::SelectSetStmtContext* HogQLParser::selectSetStmt() { || _la == HogQLParser::INTERSECT || _la == HogQLParser::UNION) { setState(381); - subsequentSelectSetStmt(); + subsequentSelectSetClause(); setState(386); _errHandler->sync(this); _la = _input->LA(1); diff --git a/hogql_parser/HogQLParser.h b/hogql_parser/HogQLParser.h index 7012c22b17504..4c59a3bdc4209 100644 --- a/hogql_parser/HogQLParser.h +++ b/hogql_parser/HogQLParser.h @@ -52,7 +52,7 @@ class HogQLParser : public antlr4::Parser { RuleForStmt = 12, RuleForInStmt = 13, RuleFuncStmt = 14, RuleVarAssignment = 15, RuleExprStmt = 16, RuleEmptyStmt = 17, RuleBlock = 18, RuleKvPair = 19, RuleKvPairList = 20, RuleSelect = 21, RuleSelectStmtWithParens = 22, - RuleSubsequentSelectSetStmt = 23, RuleSelectSetStmt = 24, RuleSelectStmt = 25, + RuleSubsequentSelectSetClause = 23, RuleSelectSetStmt = 24, RuleSelectStmt = 25, RuleWithClause = 26, RuleTopClause = 27, RuleFromClause = 28, RuleArrayJoinClause = 29, RuleWindowClause = 30, RulePrewhereClause = 31, RuleWhereClause = 32, RuleGroupByClause = 33, RuleHavingClause = 34, RuleOrderByClause = 35, @@ -114,7 +114,7 @@ class HogQLParser : public antlr4::Parser { class KvPairListContext; class SelectContext; class SelectStmtWithParensContext; - class SubsequentSelectSetStmtContext; + class SubsequentSelectSetClauseContext; class SelectSetStmtContext; class SelectStmtContext; class WithClauseContext; @@ -583,9 +583,9 @@ class HogQLParser : public antlr4::Parser { SelectStmtWithParensContext* selectStmtWithParens(); - class SubsequentSelectSetStmtContext : public antlr4::ParserRuleContext { + class SubsequentSelectSetClauseContext : public antlr4::ParserRuleContext { public: - SubsequentSelectSetStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState); + SubsequentSelectSetClauseContext(antlr4::ParserRuleContext *parent, size_t invokingState); virtual size_t getRuleIndex() const override; SelectStmtWithParensContext *selectStmtWithParens(); antlr4::tree::TerminalNode *EXCEPT(); @@ -598,15 +598,15 @@ class HogQLParser : public antlr4::Parser { }; - SubsequentSelectSetStmtContext* subsequentSelectSetStmt(); + SubsequentSelectSetClauseContext* subsequentSelectSetClause(); class SelectSetStmtContext : public antlr4::ParserRuleContext { public: SelectSetStmtContext(antlr4::ParserRuleContext *parent, size_t invokingState); virtual size_t getRuleIndex() const override; SelectStmtWithParensContext *selectStmtWithParens(); - std::vector subsequentSelectSetStmt(); - SubsequentSelectSetStmtContext* subsequentSelectSetStmt(size_t i); + std::vector subsequentSelectSetClause(); + SubsequentSelectSetClauseContext* subsequentSelectSetClause(size_t i); virtual std::any accept(antlr4::tree::ParseTreeVisitor *visitor) override; diff --git a/hogql_parser/HogQLParser.interp b/hogql_parser/HogQLParser.interp index b46bd3628a093..4308ee42ae9b9 100644 --- a/hogql_parser/HogQLParser.interp +++ b/hogql_parser/HogQLParser.interp @@ -352,7 +352,7 @@ kvPair kvPairList select selectStmtWithParens -subsequentSelectSetStmt +subsequentSelectSetClause selectSetStmt selectStmt withClause diff --git a/hogql_parser/HogQLParserBaseVisitor.h b/hogql_parser/HogQLParserBaseVisitor.h index a2ceb5bac83bf..59f4a7883406e 100644 --- a/hogql_parser/HogQLParserBaseVisitor.h +++ b/hogql_parser/HogQLParserBaseVisitor.h @@ -107,7 +107,7 @@ class HogQLParserBaseVisitor : public HogQLParserVisitor { return visitChildren(ctx); } - virtual std::any visitSubsequentSelectSetStmt(HogQLParser::SubsequentSelectSetStmtContext *ctx) override { + virtual std::any visitSubsequentSelectSetClause(HogQLParser::SubsequentSelectSetClauseContext *ctx) override { return visitChildren(ctx); } diff --git a/hogql_parser/HogQLParserVisitor.h b/hogql_parser/HogQLParserVisitor.h index 4b9062830f766..7e1728b26a1de 100644 --- a/hogql_parser/HogQLParserVisitor.h +++ b/hogql_parser/HogQLParserVisitor.h @@ -65,7 +65,7 @@ class HogQLParserVisitor : public antlr4::tree::AbstractParseTreeVisitor { virtual std::any visitSelectStmtWithParens(HogQLParser::SelectStmtWithParensContext *context) = 0; - virtual std::any visitSubsequentSelectSetStmt(HogQLParser::SubsequentSelectSetStmtContext *context) = 0; + virtual std::any visitSubsequentSelectSetClause(HogQLParser::SubsequentSelectSetClauseContext *context) = 0; virtual std::any visitSelectSetStmt(HogQLParser::SelectSetStmtContext *context) = 0; diff --git a/hogql_parser/parser.cpp b/hogql_parser/parser.cpp index db8fe26ac3199..177b4ec72b4b0 100644 --- a/hogql_parser/parser.cpp +++ b/hogql_parser/parser.cpp @@ -863,7 +863,7 @@ class HogQLParseTreeConverter : public HogQLParserBaseVisitor { } try { - for (auto subsequent : ctx->subsequentSelectSetStmt()) { + for (auto subsequent : ctx->subsequentSelectSetClause()) { char* set_operator; if (subsequent->UNION() && subsequent->ALL()) { set_operator = "UNION ALL"; diff --git a/posthog/hogql/grammar/HogQLParser.g4 b/posthog/hogql/grammar/HogQLParser.g4 index a0ccdc139660c..7f0e482e31498 100644 --- a/posthog/hogql/grammar/HogQLParser.g4 +++ b/posthog/hogql/grammar/HogQLParser.g4 @@ -55,8 +55,8 @@ select: (selectSetStmt | selectStmt | hogqlxTagElement) EOF; selectStmtWithParens: selectStmt | LPAREN selectSetStmt RPAREN | placeholder; -subsequentSelectSetStmt: (EXCEPT | UNION ALL | INTERSECT) selectStmtWithParens; -selectSetStmt: selectStmtWithParens (subsequentSelectSetStmt)*; +subsequentSelectSetClause: (EXCEPT | UNION ALL | INTERSECT) selectStmtWithParens; +selectSetStmt: selectStmtWithParens (subsequentSelectSetClause)*; selectStmt: with=withClause? diff --git a/posthog/hogql/grammar/HogQLParser.interp b/posthog/hogql/grammar/HogQLParser.interp index b46bd3628a093..4308ee42ae9b9 100644 --- a/posthog/hogql/grammar/HogQLParser.interp +++ b/posthog/hogql/grammar/HogQLParser.interp @@ -352,7 +352,7 @@ kvPair kvPairList select selectStmtWithParens -subsequentSelectSetStmt +subsequentSelectSetClause selectSetStmt selectStmt withClause diff --git a/posthog/hogql/grammar/HogQLParser.py b/posthog/hogql/grammar/HogQLParser.py index e1a5062c25b79..70f7e139b7999 100644 --- a/posthog/hogql/grammar/HogQLParser.py +++ b/posthog/hogql/grammar/HogQLParser.py @@ -643,7 +643,7 @@ class HogQLParser ( Parser ): RULE_kvPairList = 20 RULE_select = 21 RULE_selectStmtWithParens = 22 - RULE_subsequentSelectSetStmt = 23 + RULE_subsequentSelectSetClause = 23 RULE_selectSetStmt = 24 RULE_selectStmt = 25 RULE_withClause = 26 @@ -713,7 +713,7 @@ class HogQLParser ( Parser ): "tryCatchStmt", "ifStmt", "whileStmt", "forStmt", "forInStmt", "funcStmt", "varAssignment", "exprStmt", "emptyStmt", "block", "kvPair", "kvPairList", "select", "selectStmtWithParens", - "subsequentSelectSetStmt", "selectSetStmt", "selectStmt", + "subsequentSelectSetClause", "selectSetStmt", "selectStmt", "withClause", "topClause", "fromClause", "arrayJoinClause", "windowClause", "prewhereClause", "whereClause", "groupByClause", "havingClause", "orderByClause", "projectionOrderByClause", @@ -2599,7 +2599,7 @@ def selectStmtWithParens(self): return localctx - class SubsequentSelectSetStmtContext(ParserRuleContext): + class SubsequentSelectSetClauseContext(ParserRuleContext): __slots__ = 'parser' def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): @@ -2623,21 +2623,21 @@ def INTERSECT(self): return self.getToken(HogQLParser.INTERSECT, 0) def getRuleIndex(self): - return HogQLParser.RULE_subsequentSelectSetStmt + return HogQLParser.RULE_subsequentSelectSetClause def accept(self, visitor:ParseTreeVisitor): - if hasattr( visitor, "visitSubsequentSelectSetStmt" ): - return visitor.visitSubsequentSelectSetStmt(self) + if hasattr( visitor, "visitSubsequentSelectSetClause" ): + return visitor.visitSubsequentSelectSetClause(self) else: return visitor.visitChildren(self) - def subsequentSelectSetStmt(self): + def subsequentSelectSetClause(self): - localctx = HogQLParser.SubsequentSelectSetStmtContext(self, self._ctx, self.state) - self.enterRule(localctx, 46, self.RULE_subsequentSelectSetStmt) + localctx = HogQLParser.SubsequentSelectSetClauseContext(self, self._ctx, self.state) + self.enterRule(localctx, 46, self.RULE_subsequentSelectSetClause) try: self.enterOuterAlt(localctx, 1) self.state = 376 @@ -2682,11 +2682,11 @@ def selectStmtWithParens(self): return self.getTypedRuleContext(HogQLParser.SelectStmtWithParensContext,0) - def subsequentSelectSetStmt(self, i:int=None): + def subsequentSelectSetClause(self, i:int=None): if i is None: - return self.getTypedRuleContexts(HogQLParser.SubsequentSelectSetStmtContext) + return self.getTypedRuleContexts(HogQLParser.SubsequentSelectSetClauseContext) else: - return self.getTypedRuleContext(HogQLParser.SubsequentSelectSetStmtContext,i) + return self.getTypedRuleContext(HogQLParser.SubsequentSelectSetClauseContext,i) def getRuleIndex(self): @@ -2715,7 +2715,7 @@ def selectSetStmt(self): _la = self._input.LA(1) while _la==27 or _la==47 or _la==98: self.state = 381 - self.subsequentSelectSetStmt() + self.subsequentSelectSetClause() self.state = 386 self._errHandler.sync(self) _la = self._input.LA(1) diff --git a/posthog/hogql/grammar/HogQLParserVisitor.py b/posthog/hogql/grammar/HogQLParserVisitor.py index cdfe27c32cc03..98cef12617913 100644 --- a/posthog/hogql/grammar/HogQLParserVisitor.py +++ b/posthog/hogql/grammar/HogQLParserVisitor.py @@ -124,8 +124,8 @@ def visitSelectStmtWithParens(self, ctx:HogQLParser.SelectStmtWithParensContext) return self.visitChildren(ctx) - # Visit a parse tree produced by HogQLParser#subsequentSelectSetStmt. - def visitSubsequentSelectSetStmt(self, ctx:HogQLParser.SubsequentSelectSetStmtContext): + # Visit a parse tree produced by HogQLParser#subsequentSelectSetClause. + def visitSubsequentSelectSetClause(self, ctx:HogQLParser.SubsequentSelectSetClauseContext): return self.visitChildren(ctx) diff --git a/posthog/hogql/parser.py b/posthog/hogql/parser.py index 947f2a887a8fc..29a0d4dd09130 100644 --- a/posthog/hogql/parser.py +++ b/posthog/hogql/parser.py @@ -330,15 +330,12 @@ def visitBlock(self, ctx: HogQLParser.BlockContext): def visitSelect(self, ctx: HogQLParser.SelectContext): return self.visit(ctx.selectSetStmt() or ctx.selectStmt() or ctx.hogqlxTagElement()) - def visitSubsequentSelectSetStmt(self, ctx: HogQLParser.SubsequentSelectSetStmtContext): - pass - def visitSelectSetStmt(self, ctx: HogQLParser.SelectSetStmtContext): select_queries: list[SelectSetNode] = [] initial_query = self.visit(ctx.selectStmtWithParens()) - for subsequent in ctx.subsequentSelectSetStmt(): + for subsequent in ctx.subsequentSelectSetClause(): if subsequent.UNION() and subsequent.ALL(): union_type = "UNION ALL" elif subsequent.INTERSECT():