From 63dba3d9e6a91c63d9013ddd422c3791cdb98954 Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Wed, 7 Apr 2021 10:55:30 +0800 Subject: [PATCH] Fix match where agg check (#919) add ut add tck Co-authored-by: jie.wang <38901892+jievince@users.noreply.github.com> (cherry picked from commit e6cf4b2f98073b392785ee93636e9807af406be5) --- src/parser/parser.yy | 17 +++++++++++++++-- src/parser/test/ParserTest.cpp | 11 +++++++++++ tests/tck/features/agg/Agg.feature | 7 +++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/parser/parser.yy b/src/parser/parser.yy index 1235bea0a..93dfc34b2 100644 --- a/src/parser/parser.yy +++ b/src/parser/parser.yy @@ -30,6 +30,7 @@ #include "common/expression/ReduceExpression.h" #include "util/ParserUtil.h" +#include "util/ExpressionUtils.h" #include "context/QueryContext.h" #include "util/SchemaUtil.h" @@ -1286,10 +1287,22 @@ with_clause match_clause : KW_MATCH match_path where_clause { - $$ = new MatchClause($2, $3, false/*optinal*/); + if ($3 && graph::ExpressionUtils::findAny($3->filter(),{Expression::Kind::kAggregate})) { + delete($2); + delete($3); + throw nebula::GraphParser::syntax_error(@3, "Invalid use of aggregating function in this context."); + } else { + $$ = new MatchClause($2, $3, false/*optinal*/); + } } | KW_OPTIONAL KW_MATCH match_path where_clause { - $$ = new MatchClause($3, $4, true); + if ($4 && graph::ExpressionUtils::findAny($4->filter(),{Expression::Kind::kAggregate})) { + delete($3); + delete($4); + throw nebula::GraphParser::syntax_error(@4, "Invalid use of aggregating function in this context."); + } else { + $$ = new MatchClause($3, $4, true); + } } ; diff --git a/src/parser/test/ParserTest.cpp b/src/parser/test/ParserTest.cpp index c7204e814..91eb88202 100644 --- a/src/parser/test/ParserTest.cpp +++ b/src/parser/test/ParserTest.cpp @@ -2444,6 +2444,17 @@ TEST(Parser, Match) { } } +TEST(Parser, MatchErrorCheck) { + { + std::string query = "MATCH (v:player) WHERE count(v)>1 RETURN v"; + auto result = parse(query); + ASSERT_FALSE(result.ok()); + auto error = "SyntaxError: Invalid use of aggregating " + "function in this context. near `WHERE count(v)>1'"; + ASSERT_EQ(error, result.status().toString()); + } +} + TEST(Parser, MatchMultipleTags) { { std::string query = "MATCH (a:person:player) --> (b) RETURN *"; diff --git a/tests/tck/features/agg/Agg.feature b/tests/tck/features/agg/Agg.feature index ea5a7b512..f3c2bdf7c 100644 --- a/tests/tck/features/agg/Agg.feature +++ b/tests/tck/features/agg/Agg.feature @@ -685,6 +685,13 @@ Feature: Basic Aggregate and GroupBy COUNT(serve._dst) AS id """ Then a SemanticError should be raised at runtime: `COUNT(serve._dst) AS id', not support aggregate function in go sentence. + When executing query: + """ + MATCH (v:player) + WHERE avg(v.age) > 1 + RETURN v.age + """ + Then a SyntaxError should be raised at runtime: Invalid use of aggregating function in this context. near `WHERE avg(v.age) > 1' # When executing query: # """