|
12 | 12 | import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder; |
13 | 13 | import org.elasticsearch.compute.data.Block; |
14 | 14 | import org.elasticsearch.compute.data.DoubleBlock; |
| 15 | +import org.elasticsearch.compute.data.DoubleVector; |
15 | 16 | import org.elasticsearch.compute.data.IntBlock; |
16 | 17 | import org.elasticsearch.compute.data.LongBlock; |
17 | 18 | import org.elasticsearch.compute.data.Page; |
| 19 | +import org.elasticsearch.compute.data.Vector; |
18 | 20 | import org.elasticsearch.compute.operator.DriverContext; |
19 | 21 | import org.elasticsearch.compute.operator.EvalOperator; |
20 | 22 | import org.elasticsearch.core.Releasables; |
@@ -130,40 +132,60 @@ public EvalOperator.ExpressionEvaluator get(DriverContext context) { |
130 | 132 | final EvalOperator.ExpressionEvaluator eval = fieldEvaluator.get(context); |
131 | 133 |
|
132 | 134 | return new EvalOperator.ExpressionEvaluator() { |
| 135 | + private Block evalBlock(Block block) { |
| 136 | + int positionCount = block.getPositionCount(); |
| 137 | + DoubleBlock doubleBlock = (DoubleBlock) block; |
| 138 | + try ( |
| 139 | + AggregateMetricDoubleBlockBuilder result = context.blockFactory() |
| 140 | + .newAggregateMetricDoubleBlockBuilder(positionCount) |
| 141 | + ) { |
| 142 | + CompensatedSum sum = new CompensatedSum(); |
| 143 | + for (int p = 0; p < positionCount; p++) { |
| 144 | + int valueCount = doubleBlock.getValueCount(p); |
| 145 | + int start = doubleBlock.getFirstValueIndex(p); |
| 146 | + int end = start + valueCount; |
| 147 | + if (valueCount == 0) { |
| 148 | + result.appendNull(); |
| 149 | + continue; |
| 150 | + } |
| 151 | + double min = Double.POSITIVE_INFINITY; |
| 152 | + double max = Double.NEGATIVE_INFINITY; |
| 153 | + for (int i = start; i < end; i++) { |
| 154 | + double current = doubleBlock.getDouble(i); |
| 155 | + min = Math.min(min, current); |
| 156 | + max = Math.max(max, current); |
| 157 | + sum.add(current); |
| 158 | + } |
| 159 | + result.min().appendDouble(min); |
| 160 | + result.max().appendDouble(max); |
| 161 | + result.sum().appendDouble(sum.value()); |
| 162 | + result.count().appendInt(valueCount); |
| 163 | + sum.reset(0, 0); |
| 164 | + } |
| 165 | + return result.build(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private Block evalVector(Vector vector) { |
| 170 | + int positionCount = vector.getPositionCount(); |
| 171 | + DoubleVector doubleVector = (DoubleVector) vector; |
| 172 | + try ( |
| 173 | + AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleVectorBuilder builder = context.blockFactory() |
| 174 | + .newAggregateMetricDoubleVectorBuilder(positionCount) |
| 175 | + ) { |
| 176 | + for (int p = 0; p < positionCount; p++) { |
| 177 | + double value = doubleVector.getDouble(p); |
| 178 | + builder.appendValue(value); |
| 179 | + } |
| 180 | + return builder.build(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
133 | 184 | @Override |
134 | 185 | public Block eval(Page page) { |
135 | 186 | try (Block block = eval.eval(page)) { |
136 | | - int positionCount = block.getPositionCount(); |
137 | | - DoubleBlock doubleBlock = (DoubleBlock) block; |
138 | | - try ( |
139 | | - AggregateMetricDoubleBlockBuilder result = context.blockFactory() |
140 | | - .newAggregateMetricDoubleBlockBuilder(positionCount) |
141 | | - ) { |
142 | | - CompensatedSum sum = new CompensatedSum(); |
143 | | - for (int p = 0; p < positionCount; p++) { |
144 | | - int valueCount = doubleBlock.getValueCount(p); |
145 | | - int start = doubleBlock.getFirstValueIndex(p); |
146 | | - int end = start + valueCount; |
147 | | - if (valueCount == 0) { |
148 | | - result.appendNull(); |
149 | | - continue; |
150 | | - } |
151 | | - double min = Double.POSITIVE_INFINITY; |
152 | | - double max = Double.NEGATIVE_INFINITY; |
153 | | - for (int i = start; i < end; i++) { |
154 | | - double current = doubleBlock.getDouble(i); |
155 | | - min = Math.min(min, current); |
156 | | - max = Math.max(max, current); |
157 | | - sum.add(current); |
158 | | - } |
159 | | - result.min().appendDouble(min); |
160 | | - result.max().appendDouble(max); |
161 | | - result.sum().appendDouble(sum.value()); |
162 | | - result.count().appendInt(valueCount); |
163 | | - sum.reset(0, 0); |
164 | | - } |
165 | | - return result.build(); |
166 | | - } |
| 187 | + Vector vector = block.asVector(); |
| 188 | + return vector == null ? evalBlock(block) : evalVector(vector); |
167 | 189 | } |
168 | 190 | } |
169 | 191 |
|
|
0 commit comments