Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ae16694
[ES|QL] Support some stats on aggregate_double_metric
limotova Jan 16, 2025
a5be73b
addressed most comments minus tests
limotova Jan 22, 2025
80b317a
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 23, 2025
a2a6092
FromAggregateDoubleMetricTests
limotova Jan 25, 2025
9094f62
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 25, 2025
b3b27aa
bring back unsupported exceptions
limotova Jan 27, 2025
5acb5a8
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 28, 2025
71a05b2
change valueCount to Integer and revert other getValueCount()
limotova Jan 28, 2025
648e0af
and revert getFirstValueIndex()
limotova Jan 28, 2025
83612ec
test all metrics in aggregate double metric test
limotova Jan 28, 2025
38659b9
refactor things touched in this PR to all be aggregate metric double …
limotova Jan 28, 2025
bfe84c1
[CI] Auto commit changes from spotless
Jan 28, 2025
022eebe
renaming stragglers
limotova Jan 28, 2025
769e924
Update docs/changelog/120343.yaml
limotova Jan 28, 2025
6dd6e36
fix changelog
limotova Jan 28, 2025
5dd5ad9
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 28, 2025
af0e242
Add downsampled yaml test
limotova Jan 28, 2025
1838a82
address some comments
limotova Jan 28, 2025
335fe3b
change Double etc to double
limotova Jan 29, 2025
5608bce
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 29, 2025
c417b3c
add instanceof check
limotova Jan 29, 2025
b5837f3
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 29, 2025
fba979f
Merge branch 'main' into add-aggregate-double-metric-aggregates
limotova Jan 29, 2025
827eaf3
change AggMetDoubLit to record
limotova Jan 29, 2025
fbdb24d
move function to make checkstyle work
limotova Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/120343.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 120343
summary: Support some stats on aggregate_metric_double
area: "ES|QL"
type: enhancement
issues:
- 110649
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ interface BlockFactory {
SingletonOrdinalsBuilder singletonOrdinalsBuilder(SortedDocValues ordinals, int count);

// TODO support non-singleton ords

AggregateMetricDoubleBuilder aggregateMetricDoubleBuilder(int count);
}

/**
Expand Down Expand Up @@ -501,4 +503,16 @@ interface SingletonOrdinalsBuilder extends Builder {
*/
SingletonOrdinalsBuilder appendOrd(int value);
}

interface AggregateMetricDoubleBuilder extends Builder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! This is not something in server but we have to add it here for this. Huh. I suppose that's ok.


DoubleBuilder min();

DoubleBuilder max();

DoubleBuilder sum();

IntBuilder count();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public SingletonOrdsBuilder appendOrd(int value) {
}
return new SingletonOrdsBuilder();
}

@Override
public BlockLoader.AggregateMetricDoubleBuilder aggregateMetricDoubleBuilder(int count) {
return null;
}
};
}

Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ tasks.named("yamlRestCompatTestTransform").configure({ task ->
task.skipTest("esql/180_match_operator/match with disjunctions", "Disjunctions in full text functions work now")
// Expected deprecation warning to compat yaml tests:
task.addAllowedWarningRegex(".*rollup functionality will be removed in Elasticsearch.*")
task.skipTest("esql/40_tsdb/from doc with aggregate_metric_double", "TODO: support for subset of metric fields")
task.skipTest("esql/40_tsdb/stats on aggregate_metric_double", "TODO: support for subset of metric fields")
task.skipTest("esql/40_tsdb/from index pattern unsupported counter", "TODO: support for subset of metric fields")
task.skipTest("esql/40_unsupported_types/unsupported", "TODO: support for subset of metric fields")
task.skipTest("esql/40_unsupported_types/unsupported with sort", "TODO: support for subset of metric fields")
})

tasks.named('yamlRestCompatTest').configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
public class EsqlCorePlugin extends Plugin implements ExtensiblePlugin {

public static final FeatureFlag SEMANTIC_TEXT_FEATURE_FLAG = new FeatureFlag("esql_semantic_text");
public static final FeatureFlag AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG = new FeatureFlag("esql_aggregate_metric_double");
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ public enum DataType {
* loaded from the index and ESQL will load these fields as strings without their attached
* chunks or embeddings.
*/
SEMANTIC_TEXT(builder().esType("semantic_text").unknownSize());
SEMANTIC_TEXT(builder().esType("semantic_text").unknownSize()),

AGGREGATE_METRIC_DOUBLE(builder().esType("aggregate_metric_double").estimatedSize(Double.BYTES * 3 + Integer.BYTES));

/**
* Types that are actively being built. These types are not returned
Expand All @@ -316,7 +318,8 @@ public enum DataType {
* check that sending them to a function produces a sane error message.
*/
public static final Map<DataType, FeatureFlag> UNDER_CONSTRUCTION = Map.ofEntries(
Map.entry(SEMANTIC_TEXT, EsqlCorePlugin.SEMANTIC_TEXT_FEATURE_FLAG)
Map.entry(SEMANTIC_TEXT, EsqlCorePlugin.SEMANTIC_TEXT_FEATURE_FLAG),
Map.entry(AGGREGATE_METRIC_DOUBLE, EsqlCorePlugin.AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG)
);

private final String typeName;
Expand Down Expand Up @@ -553,6 +556,7 @@ public static boolean isRepresentable(DataType t) {
&& t != SOURCE
&& t != HALF_FLOAT
&& t != PARTIAL_AGG
&& t != AGGREGATE_METRIC_DOUBLE
&& t.isCounter() == false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.compute.data;

import org.elasticsearch.core.Releasables;
import org.elasticsearch.index.mapper.BlockLoader;

public class AggregateMetricDoubleBlockBuilder extends AbstractBlockBuilder implements BlockLoader.AggregateMetricDoubleBuilder {

private DoubleBlockBuilder minBuilder;
private DoubleBlockBuilder maxBuilder;
private DoubleBlockBuilder sumBuilder;
private IntBlockBuilder countBuilder;

public AggregateMetricDoubleBlockBuilder(int estimatedSize, BlockFactory blockFactory) {
super(blockFactory);
minBuilder = null;
maxBuilder = null;
sumBuilder = null;
countBuilder = null;
try {
minBuilder = new DoubleBlockBuilder(estimatedSize, blockFactory);
maxBuilder = new DoubleBlockBuilder(estimatedSize, blockFactory);
sumBuilder = new DoubleBlockBuilder(estimatedSize, blockFactory);
countBuilder = new IntBlockBuilder(estimatedSize, blockFactory);
} finally {
if (countBuilder == null) {
Releasables.closeWhileHandlingException(minBuilder, maxBuilder, sumBuilder, countBuilder);
}
}
}

@Override
protected int valuesLength() {
throw new UnsupportedOperationException("Not available on aggregate_metric_double");
}

@Override
protected void growValuesArray(int newSize) {
throw new UnsupportedOperationException("Not available on aggregate_metric_double");
}

@Override
protected int elementSize() {
throw new UnsupportedOperationException("Not available on aggregate_metric_double");
}

@Override
public Block.Builder copyFrom(Block block, int beginInclusive, int endExclusive) {
Block minBlock;
Block maxBlock;
Block sumBlock;
Block countBlock;
if (block.areAllValuesNull()) {
minBlock = block;
maxBlock = block;
sumBlock = block;
countBlock = block;
} else {
CompositeBlock composite = (CompositeBlock) block;
minBlock = composite.getBlock(Metric.MIN.getIndex());
maxBlock = composite.getBlock(Metric.MAX.getIndex());
sumBlock = composite.getBlock(Metric.SUM.getIndex());
countBlock = composite.getBlock(Metric.COUNT.getIndex());
}
minBuilder.copyFrom(minBlock, beginInclusive, endExclusive);
maxBuilder.copyFrom(maxBlock, beginInclusive, endExclusive);
sumBuilder.copyFrom(sumBlock, beginInclusive, endExclusive);
countBuilder.copyFrom(countBlock, beginInclusive, endExclusive);
return this;
}

@Override
public AbstractBlockBuilder appendNull() {
minBuilder.appendNull();
maxBuilder.appendNull();
sumBuilder.appendNull();
countBuilder.appendNull();
return this;
}

@Override
public Block.Builder mvOrdering(Block.MvOrdering mvOrdering) {
minBuilder.mvOrdering(mvOrdering);
maxBuilder.mvOrdering(mvOrdering);
sumBuilder.mvOrdering(mvOrdering);
countBuilder.mvOrdering(mvOrdering);
return this;
}

@Override
public Block build() {
Block[] blocks = new Block[4];
boolean success = false;
try {
finish();
blocks[Metric.MIN.getIndex()] = minBuilder.build();
blocks[Metric.MAX.getIndex()] = maxBuilder.build();
blocks[Metric.SUM.getIndex()] = sumBuilder.build();
blocks[Metric.COUNT.getIndex()] = countBuilder.build();
CompositeBlock block = new CompositeBlock(blocks);
success = true;
return block;
} finally {
if (success == false) {
Releasables.closeExpectNoException(blocks);
}
}
}

@Override
protected void extraClose() {
Releasables.closeExpectNoException(minBuilder, maxBuilder, sumBuilder, countBuilder);
}

@Override
public BlockLoader.DoubleBuilder min() {
return minBuilder;
}

@Override
public BlockLoader.DoubleBuilder max() {
return maxBuilder;
}

@Override
public BlockLoader.DoubleBuilder sum() {
return sumBuilder;
}

@Override
public BlockLoader.IntBuilder count() {
return countBuilder;
}

public enum Metric {
MIN(0),
MAX(1),
SUM(2),
COUNT(3);

private final int index;

Metric(int index) {
this.index = index;
}

public int getIndex() {
return index;
}
}

public record AggregateMetricDoubleLiteral(Double min, Double max, Double sum, Integer count) {
public AggregateMetricDoubleLiteral {
min = min.isNaN() ? null : min;
max = max.isNaN() ? null : max;
sum = sum.isNaN() ? null : sum;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,39 @@ public Block newConstantNullBlock(int positions) {
return b;
}

public AggregateMetricDoubleBlockBuilder newAggregateMetricDoubleBlockBuilder(int estimatedSize) {
return new AggregateMetricDoubleBlockBuilder(estimatedSize, this);
}

public final Block newConstantAggregateMetricDoubleBlock(
AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral value,
int positions
) {
try (AggregateMetricDoubleBlockBuilder builder = newAggregateMetricDoubleBlockBuilder(positions)) {
if (value.min() != null) {
builder.min().appendDouble(value.min());
} else {
builder.min().appendNull();
}
if (value.max() != null) {
builder.max().appendDouble(value.max());
} else {
builder.max().appendNull();
}
if (value.sum() != null) {
builder.sum().appendDouble(value.sum());
} else {
builder.sum().appendNull();
}
if (value.count() != null) {
builder.count().appendInt(value.count());
} else {
builder.count().appendNull();
}
return builder.build();
}
}

/**
* Returns the maximum number of bytes that a Block should be backed by a primitive array before switching to using BigArrays.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.Releasables;

Expand Down Expand Up @@ -233,6 +234,14 @@ private static Block constantBlock(BlockFactory blockFactory, ElementType type,
case BYTES_REF -> blockFactory.newConstantBytesRefBlockWith(toBytesRef(val), size);
case DOUBLE -> blockFactory.newConstantDoubleBlockWith((double) val, size);
case BOOLEAN -> blockFactory.newConstantBooleanBlockWith((boolean) val, size);
case COMPOSITE -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, a composite type can be more than aggregated_metric_double? Can we just leave it unsupported here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this to be able to support the unit tests and wasn't really sure of a way to work around it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If composite can be more than just aggregated_metric_double, then should aggregated_metric_double have its own element type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it really needs it's own type. Maybe there's something funny around constants though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we instanceof on the val and do an AggregateMetricDouble if it's one of those constants. Again, this feels like it's the kind of thing we'd use for just tests and ROW. Which is ok.

if (val instanceof AggregateMetricDoubleLiteral aggregateMetricDoubleLiteral) {
yield blockFactory.newConstantAggregateMetricDoubleBlock(aggregateMetricDoubleLiteral, size);
}
throw new UnsupportedOperationException(
"Composite block but received value that wasn't AggregateMetricDoubleLiteral [" + val + "]"
);
}
default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]");
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,22 @@ public int getTotalValueCount() {

@Override
public int getFirstValueIndex(int position) {
throw new UnsupportedOperationException("Composite block");
return blocks[0].getFirstValueIndex(position);
}

@Override
public int getValueCount(int position) {
throw new UnsupportedOperationException("Composite block");
return blocks[0].getValueCount(position);
}

@Override
public boolean isNull(int position) {
throw new UnsupportedOperationException("Composite block");
for (Block block : blocks) {
if (block.isNull(position) == false) {
return false;
}
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum ElementType {
/**
* Composite blocks which contain array of sub-blocks.
*/
COMPOSITE("Composite", (blockFactory, estimatedSize) -> { throw new UnsupportedOperationException("can't build composite blocks"); }),
COMPOSITE("Composite", BlockFactory::newAggregateMetricDoubleBlockBuilder),

/**
* Intermediate blocks which don't support retrieving elements.
Expand Down Expand Up @@ -73,6 +73,8 @@ public static ElementType fromJava(Class<?> type) {
elementType = BYTES_REF;
} else if (type == Boolean.class) {
elementType = BOOLEAN;
} else if (type == AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral.class) {
elementType = COMPOSITE;
} else if (type == null || type == Void.class) {
elementType = NULL;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ public BytesRefBlock constantBytes(BytesRef value) {
public BlockLoader.SingletonOrdinalsBuilder singletonOrdinalsBuilder(SortedDocValues ordinals, int count) {
return new SingletonOrdinalsBuilder(factory, ordinals, count);
}

@Override
public BlockLoader.AggregateMetricDoubleBuilder aggregateMetricDoubleBuilder(int count) {
return factory.newAggregateMetricDoubleBlockBuilder(count);
}
}

// TODO tests that mix source loaded fields and doc values in the same block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,8 @@ public static Literal randomLiteral(DataType type) {
throw new UncheckedIOException(e);
}
}
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG -> throw new IllegalArgumentException(
"can't make random values for [" + type.typeName() + "]"
);
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG, AGGREGATE_METRIC_DOUBLE ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we'll have to be able to make random AggregateMetricDoubles. But later is fine.

throw new IllegalArgumentException("can't make random values for [" + type.typeName() + "]");
}, type);
}

Expand Down
Loading