From e6cf4b2f98073b392785ee93636e9807af406be5 Mon Sep 17 00:00:00 2001
From: "kyle.cao" <kyle.cao@vesoft.com>
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>
---
 src/parser/parser.yy                     | 17 +++++++++++++++--
 src/parser/test/ParserTest.cpp           | 11 +++++++++++
 tests/tck/features/aggregate/Agg.feature |  7 +++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/src/parser/parser.yy b/src/parser/parser.yy
index 2cfa4658..718b5363 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"
 
@@ -1292,10 +1293,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 c7204e81..91eb8820 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/aggregate/Agg.feature b/tests/tck/features/aggregate/Agg.feature
index 7bb2f02f..c1fa3a5f 100644
--- a/tests/tck/features/aggregate/Agg.feature
+++ b/tests/tck/features/aggregate/Agg.feature
@@ -611,6 +611,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:
 # """
-- 
GitLab