From 2b1b733b5284654d951f08610e90f3ac9e709822 Mon Sep 17 00:00:00 2001 From: "jie.wang" <38901892+jievince@users.noreply.github.com> Date: Fri, 19 Aug 2022 10:02:27 +0800 Subject: [PATCH] optimize count(*) (#4555) --- .../executor/query/AggregateExecutor.cpp | 7 +++++ tests/tck/features/aggregate/Agg.feature | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/graph/executor/query/AggregateExecutor.cpp b/src/graph/executor/query/AggregateExecutor.cpp index 76c54278359..c22c7f94997 100644 --- a/src/graph/executor/query/AggregateExecutor.cpp +++ b/src/graph/executor/query/AggregateExecutor.cpp @@ -17,6 +17,13 @@ folly::Future AggregateExecutor::execute() { auto iter = ectx_->getResult(agg->inputVar()).iter(); DCHECK(!!iter); QueryExpressionContext ctx(ectx_); + // We could directly return size of input dataset for `COUNT(*)` + if (groupKeys.empty() && groupItems.size() == 1 && groupItems[0]->toString() == "COUNT(*)") { + DataSet ds; + ds.colNames = agg->colNames(); + ds.rows.emplace_back(Row({Value(int64_t(iter->size()))})); + return finish(ResultBuilder().value(Value(std::move(ds))).build()); + } std::unordered_map>, std::hash> result; diff --git a/tests/tck/features/aggregate/Agg.feature b/tests/tck/features/aggregate/Agg.feature index 1d33b0fcaf2..2f3e7a62f59 100644 --- a/tests/tck/features/aggregate/Agg.feature +++ b/tests/tck/features/aggregate/Agg.feature @@ -47,6 +47,34 @@ Feature: Basic Aggregate and GroupBy Then the result should be, in any order, with relax comparison: | a | | 1 | + When executing query: + """ + GO FROM "Tim Duncan" OVER like YIELD like._dst AS dst | YIELD COUNT(*) + """ + Then the result should be, in any order, with relax comparison: + | COUNT(*) | + | 2 | + When executing query: + """ + GO 3 STEPS FROM "Tim Duncan" OVER like YIELD like._dst AS dst | YIELD COUNT(*) + """ + Then the result should be, in any order, with relax comparison: + | COUNT(*) | + | 5 | + When executing query: + """ + GO 1 to 3 STEPS FROM "Tony Parker" OVER serve BIDIRECT YIELD DISTINCT id($$) AS dst | YIELD COUNT(*) + """ + Then the result should be, in any order, with relax comparison: + | COUNT(*) | + | 41 | + When executing query: + """ + MATCH (v:player) RETURN COUNT(*) + """ + Then the result should be, in any order, with relax comparison: + | COUNT(*) | + | 56 | Scenario: [1] Basic GroupBy When executing query: