1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.expression.aggregation; | |
8 | ||
9 | import static org.opensearch.sql.utils.ExpressionUtils.format; | |
10 | ||
11 | import java.util.HashSet; | |
12 | import java.util.List; | |
13 | import java.util.Locale; | |
14 | import java.util.Set; | |
15 | import org.opensearch.sql.data.model.ExprValue; | |
16 | import org.opensearch.sql.data.model.ExprValueUtils; | |
17 | import org.opensearch.sql.data.type.ExprCoreType; | |
18 | import org.opensearch.sql.expression.Expression; | |
19 | import org.opensearch.sql.expression.aggregation.CountAggregator.CountState; | |
20 | import org.opensearch.sql.expression.function.BuiltinFunctionName; | |
21 | ||
22 | public class CountAggregator extends Aggregator<CountState> { | |
23 | ||
24 | public CountAggregator(List<Expression> arguments, ExprCoreType returnType) { | |
25 | super(BuiltinFunctionName.COUNT.getName(), arguments, returnType); | |
26 | } | |
27 | ||
28 | @Override | |
29 | public CountAggregator.CountState create() { | |
30 |
2
1. create : negated conditional → KILLED 2. create : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::create → KILLED |
return distinct ? new DistinctCountState() : new CountState(); |
31 | } | |
32 | ||
33 | @Override | |
34 | protected CountState iterate(ExprValue value, CountState state) { | |
35 |
1
1. iterate : removed call to org/opensearch/sql/expression/aggregation/CountAggregator$CountState::count → KILLED |
state.count(value); |
36 |
1
1. iterate : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator::iterate → SURVIVED |
return state; |
37 | } | |
38 | ||
39 | @Override | |
40 | public String toString() { | |
41 |
2
1. toString : negated conditional → KILLED 2. toString : replaced return value with "" for org/opensearch/sql/expression/aggregation/CountAggregator::toString → KILLED |
return distinct |
42 | ? String.format(Locale.ROOT, "count(distinct %s)", format(getArguments())) | |
43 | : String.format(Locale.ROOT, "count(%s)", format(getArguments())); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Count State. | |
48 | */ | |
49 | protected static class CountState implements AggregationState { | |
50 | protected int count; | |
51 | ||
52 | CountState() { | |
53 | this.count = 0; | |
54 | } | |
55 | ||
56 | public void count(ExprValue value) { | |
57 |
1
1. count : Replaced integer addition with subtraction → KILLED |
count++; |
58 | } | |
59 | ||
60 | @Override | |
61 | public ExprValue result() { | |
62 |
1
1. result : replaced return value with null for org/opensearch/sql/expression/aggregation/CountAggregator$CountState::result → KILLED |
return ExprValueUtils.integerValue(count); |
63 | } | |
64 | } | |
65 | ||
66 | protected static class DistinctCountState extends CountState { | |
67 | private final Set<ExprValue> distinctValues = new HashSet<>(); | |
68 | ||
69 | @Override | |
70 | public void count(ExprValue value) { | |
71 |
1
1. count : negated conditional → KILLED |
if (!distinctValues.contains(value)) { |
72 | distinctValues.add(value); | |
73 |
1
1. count : Replaced integer addition with subtraction → KILLED |
count++; |
74 | } | |
75 | } | |
76 | } | |
77 | } | |
Mutations | ||
30 |
1.1 2.2 |
|
35 |
1.1 |
|
36 |
1.1 |
|
41 |
1.1 2.2 |
|
57 |
1.1 |
|
62 |
1.1 |
|
71 |
1.1 |
|
73 |
1.1 |