|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.compute.operator.topn; |
| 9 | + |
| 10 | +import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder; |
| 11 | +import org.elasticsearch.compute.data.CompositeBlock; |
| 12 | +import org.elasticsearch.compute.data.DoubleBlock; |
| 13 | +import org.elasticsearch.compute.data.IntBlock; |
| 14 | +import org.elasticsearch.compute.operator.BreakingBytesRefBuilder; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class ValueExtractorForComposite implements ValueExtractor { |
| 19 | + private final CompositeBlock block; |
| 20 | + |
| 21 | + ValueExtractorForComposite(TopNEncoder encoder, CompositeBlock block) { |
| 22 | + assert encoder == TopNEncoder.DEFAULT_UNSORTABLE; |
| 23 | + this.block = block; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void writeValue(BreakingBytesRefBuilder values, int position) { |
| 28 | + if (block.getBlockCount() != AggregateMetricDoubleBlockBuilder.Metric.values().length) { |
| 29 | + throw new UnsupportedOperationException("Composite Blocks for non-aggregate-metric-doubles do not have value extractors"); |
| 30 | + } |
| 31 | + for (AggregateMetricDoubleBlockBuilder.Metric metric : List.of( |
| 32 | + AggregateMetricDoubleBlockBuilder.Metric.MIN, |
| 33 | + AggregateMetricDoubleBlockBuilder.Metric.MAX, |
| 34 | + AggregateMetricDoubleBlockBuilder.Metric.SUM |
| 35 | + )) { |
| 36 | + DoubleBlock doubleBlock = block.getBlock(metric.getIndex()); |
| 37 | + if (doubleBlock.isNull(position)) { |
| 38 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(false, values); |
| 39 | + } else { |
| 40 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(true, values); |
| 41 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeDouble(doubleBlock.getDouble(position), values); |
| 42 | + } |
| 43 | + } |
| 44 | + IntBlock intBlock = block.getBlock(AggregateMetricDoubleBlockBuilder.Metric.COUNT.getIndex()); |
| 45 | + if (intBlock.isNull(position)) { |
| 46 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(false, values); |
| 47 | + } else { |
| 48 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(true, values); |
| 49 | + TopNEncoder.DEFAULT_UNSORTABLE.encodeInt(intBlock.getInt(position), values); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String toString() { |
| 55 | + return "ValueExtractorForComposite"; |
| 56 | + } |
| 57 | +} |
0 commit comments