1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.expression.window.ranking; | |
8 | ||
9 | import static java.util.Collections.emptyList; | |
10 | ||
11 | import java.util.List; | |
12 | import java.util.stream.Collectors; | |
13 | import org.apache.commons.lang3.tuple.Pair; | |
14 | import org.opensearch.sql.data.model.ExprIntegerValue; | |
15 | import org.opensearch.sql.data.model.ExprValue; | |
16 | import org.opensearch.sql.data.type.ExprCoreType; | |
17 | import org.opensearch.sql.data.type.ExprType; | |
18 | import org.opensearch.sql.expression.Expression; | |
19 | import org.opensearch.sql.expression.FunctionExpression; | |
20 | import org.opensearch.sql.expression.env.Environment; | |
21 | import org.opensearch.sql.expression.function.FunctionName; | |
22 | import org.opensearch.sql.expression.window.WindowDefinition; | |
23 | import org.opensearch.sql.expression.window.WindowFunctionExpression; | |
24 | import org.opensearch.sql.expression.window.frame.CurrentRowWindowFrame; | |
25 | import org.opensearch.sql.expression.window.frame.WindowFrame; | |
26 | import org.opensearch.sql.storage.bindingtuple.BindingTuple; | |
27 | ||
28 | /** | |
29 | * Ranking window function base class that captures same info across different ranking functions, | |
30 | * such as same return type (integer), same argument list (no arg). | |
31 | */ | |
32 | public abstract class RankingWindowFunction extends FunctionExpression | |
33 | implements WindowFunctionExpression { | |
34 | ||
35 | /** | |
36 | * Current rank number assigned. | |
37 | */ | |
38 | protected int rank; | |
39 | ||
40 | public RankingWindowFunction(FunctionName functionName) { | |
41 | super(functionName, emptyList()); | |
42 | } | |
43 | ||
44 | @Override | |
45 | public ExprType type() { | |
46 |
1
1. type : replaced return value with null for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::type → KILLED |
return ExprCoreType.INTEGER; |
47 | } | |
48 | ||
49 | @Override | |
50 | public WindowFrame createWindowFrame(WindowDefinition definition) { | |
51 |
1
1. createWindowFrame : replaced return value with null for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::createWindowFrame → KILLED |
return new CurrentRowWindowFrame(definition); |
52 | } | |
53 | ||
54 | @Override | |
55 | public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | |
56 |
1
1. valueOf : replaced return value with null for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::valueOf → KILLED |
return new ExprIntegerValue(rank((CurrentRowWindowFrame) valueEnv)); |
57 | } | |
58 | ||
59 | /** | |
60 | * Rank logic that sub-class needs to implement. | |
61 | * @param frame window frame | |
62 | * @return rank number | |
63 | */ | |
64 | protected abstract int rank(CurrentRowWindowFrame frame); | |
65 | ||
66 | /** | |
67 | * Check sort field to see if current value is different from previous. | |
68 | * @param frame window frame | |
69 | * @return true if different, false if same or no sort list defined | |
70 | */ | |
71 | protected boolean isSortFieldValueDifferent(CurrentRowWindowFrame frame) { | |
72 |
1
1. isSortFieldValueDifferent : negated conditional → KILLED |
if (isSortItemsNotDefined(frame)) { |
73 |
1
1. isSortFieldValueDifferent : replaced boolean return with true for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::isSortFieldValueDifferent → KILLED |
return false; |
74 | } | |
75 | ||
76 | List<Expression> sortItems = frame.getWindowDefinition() | |
77 | .getSortList() | |
78 | .stream() | |
79 | .map(Pair::getRight) | |
80 | .collect(Collectors.toList()); | |
81 | ||
82 | List<ExprValue> previous = resolve(frame, sortItems, frame.previous()); | |
83 | List<ExprValue> current = resolve(frame, sortItems, frame.current()); | |
84 |
2
1. isSortFieldValueDifferent : negated conditional → KILLED 2. isSortFieldValueDifferent : replaced boolean return with true for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::isSortFieldValueDifferent → KILLED |
return !current.equals(previous); |
85 | } | |
86 | ||
87 | private boolean isSortItemsNotDefined(CurrentRowWindowFrame frame) { | |
88 |
2
1. isSortItemsNotDefined : replaced boolean return with false for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::isSortItemsNotDefined → SURVIVED 2. isSortItemsNotDefined : replaced boolean return with true for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::isSortItemsNotDefined → KILLED |
return frame.getWindowDefinition().getSortList().isEmpty(); |
89 | } | |
90 | ||
91 | private List<ExprValue> resolve(WindowFrame frame, List<Expression> expressions, ExprValue row) { | |
92 | BindingTuple valueEnv = row.bindingTuples(); | |
93 |
1
1. resolve : replaced return value with Collections.emptyList for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::resolve → KILLED |
return expressions.stream() |
94 |
1
1. lambda$resolve$0 : replaced return value with null for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::lambda$resolve$0 → KILLED |
.map(expr -> expr.valueOf(valueEnv)) |
95 | .collect(Collectors.toList()); | |
96 | } | |
97 | ||
98 | @Override | |
99 | public String toString() { | |
100 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/window/ranking/RankingWindowFunction::toString → KILLED |
return getFunctionName() + "()"; |
101 | } | |
102 | } | |
Mutations | ||
46 |
1.1 |
|
51 |
1.1 |
|
56 |
1.1 |
|
72 |
1.1 |
|
73 |
1.1 |
|
84 |
1.1 2.2 |
|
88 |
1.1 2.2 |
|
93 |
1.1 |
|
94 |
1.1 |
|
100 |
1.1 |