| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.apache.hadoop.hive.contrib.udaf.example; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Collections; |
| 24 |
|
import java.util.Comparator; |
| 25 |
|
import java.util.List; |
| 26 |
|
|
| 27 |
|
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
|
|
|
| 81.6% |
Uncovered Elements: 9 (49) |
Complexity: 18 |
Complexity Density: 0.67 |
|
| 32 |
|
public final class UDAFExampleMaxMinNUtil { |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 40 |
|
public static class State { |
| 41 |
|
ArrayList<Double> a; |
| 42 |
|
int n; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
|
|
|
| 93.5% |
Uncovered Elements: 3 (46) |
Complexity: 14 |
Complexity Density: 0.58 |
|
| 49 |
|
public abstract static class Evaluator implements UDAFEvaluator { |
| 50 |
|
|
| 51 |
|
private State state; |
| 52 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 53 |
16
|
public Evaluator() {... |
| 54 |
16
|
state = new State(); |
| 55 |
16
|
init(); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 61 |
20
|
public void init() { ... |
| 62 |
20
|
state.a = new ArrayList<Double>(); |
| 63 |
20
|
state.n = 0; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
protected abstract boolean getAscending(); |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 6 |
Complexity Density: 0.43 |
|
| 76 |
2000
|
public boolean iterate(Double o, int n) {... |
| 77 |
2000
|
boolean ascending = getAscending(); |
| 78 |
2000
|
state.n = n; |
| 79 |
2000
|
if (o != null) { |
| 80 |
1500
|
boolean doInsert = state.a.size() < n; |
| 81 |
1500
|
if (!doInsert) { |
| 82 |
1460
|
Double last = state.a.get(state.a.size()-1); |
| 83 |
1460
|
if (ascending) { |
| 84 |
734
|
doInsert = o < last; |
| 85 |
|
} else { |
| 86 |
726
|
doInsert = o > last; |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
1500
|
if (doInsert) { |
| 90 |
180
|
binaryInsert(state.a, o, ascending); |
| 91 |
180
|
if (state.a.size() > n) { |
| 92 |
140
|
state.a.remove(state.a.size()-1); |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
2000
|
return true; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 102 |
4
|
public State terminatePartial() {... |
| 103 |
|
|
| 104 |
4
|
return state.a.size() == 0 ? null : state; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 111 |
4
|
public boolean merge(State o) {... |
| 112 |
4
|
if (o != null) { |
| 113 |
4
|
state.n = o.n; |
| 114 |
4
|
state.a = sortedMerge(o.a, state.a, getAscending(), o.n); |
| 115 |
|
} |
| 116 |
4
|
return true; |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 122 |
4
|
public ArrayList<Double> terminate() {... |
| 123 |
|
|
| 124 |
4
|
return state.a.size() == 0 ? null : state.a; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 133 |
184
|
static <T extends Comparable<T>> Comparator<T> getComparator(boolean ascending, T dummy) {... |
| 134 |
184
|
Comparator<T> comp; |
| 135 |
184
|
if (ascending) { |
| 136 |
88
|
comp = new Comparator<T>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 137 |
276
|
public int compare(T o1, T o2) {... |
| 138 |
276
|
return o1.compareTo(o2); |
| 139 |
|
} |
| 140 |
|
}; |
| 141 |
|
} else { |
| 142 |
96
|
comp = new Comparator<T>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
285
|
public int compare(T o1, T o2) {... |
| 144 |
285
|
return o2.compareTo(o1); |
| 145 |
|
} |
| 146 |
|
}; |
| 147 |
|
} |
| 148 |
184
|
return comp; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 158 |
180
|
static <T extends Comparable<T>> void binaryInsert(List<T> list, T value, boolean ascending) {... |
| 159 |
|
|
| 160 |
180
|
int position = Collections.binarySearch(list, value, getComparator(ascending, (T)null)); |
| 161 |
180
|
if (position < 0) { |
| 162 |
154
|
position = (-position) - 1; |
| 163 |
|
} |
| 164 |
180
|
list.add(position, value); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
@param |
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
|
|
|
| 71.4% |
Uncovered Elements: 8 (28) |
Complexity: 11 |
Complexity Density: 0.69 |
|
| 173 |
4
|
static <T extends Comparable<T>> ArrayList<T> sortedMerge(List<T> a1, List<T> a2,... |
| 174 |
|
boolean ascending, int n) { |
| 175 |
|
|
| 176 |
4
|
Comparator<T> comparator = getComparator(ascending, (T)null); |
| 177 |
|
|
| 178 |
4
|
int n1 = a1.size(); |
| 179 |
4
|
int n2 = a2.size(); |
| 180 |
4
|
int p1 = 0; |
| 181 |
4
|
int p2 = 0; |
| 182 |
|
|
| 183 |
4
|
ArrayList<T> output = new ArrayList<T>(n); |
| 184 |
|
|
| 185 |
40
|
while (output.size() < n && (p1 < n1 || p2 < n2)) { |
| 186 |
40
|
if (p1 < n1) { |
| 187 |
40
|
if (p2 == n2 || comparator.compare(a1.get(p1), a2.get(p2)) < 0) { |
| 188 |
40
|
output.add(a1.get(p1++)); |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
40
|
if (output.size() == n) { |
| 192 |
4
|
break; |
| 193 |
|
} |
| 194 |
36
|
if (p2 < n2) { |
| 195 |
0
|
if (p1 == n1 || comparator.compare(a2.get(p2), a1.get(p1)) < 0) { |
| 196 |
0
|
output.add(a2.get(p2++)); |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
} |
| 200 |
|
|
| 201 |
4
|
return output; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 205 |
0
|
private UDAFExampleMaxMinNUtil() {... |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
} |
| 209 |
|
|