|
@@ -0,0 +1,115 @@
|
|
|
+/*
|
|
|
+ * 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.xpack.esql.expression.function.aggregate;
|
|
|
+
|
|
|
+import com.carrotsearch.randomizedtesting.annotations.Name;
|
|
|
+import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
|
|
+
|
|
|
+import org.elasticsearch.xpack.esql.core.expression.Expression;
|
|
|
+import org.elasticsearch.xpack.esql.core.tree.Source;
|
|
|
+import org.elasticsearch.xpack.esql.core.type.DataType;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.AbstractAggregationTestCase;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.MultiRowTestCaseSupplier;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
|
|
|
+import org.hamcrest.Matcher;
|
|
|
+import org.hamcrest.Matchers;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.hasSize;
|
|
|
+
|
|
|
+public class RateTests extends AbstractAggregationTestCase {
|
|
|
+ public RateTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
|
|
|
+ this.testCase = testCaseSupplier.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ParametersFactory
|
|
|
+ public static Iterable<Object[]> parameters() {
|
|
|
+ var suppliers = new ArrayList<TestCaseSupplier>();
|
|
|
+
|
|
|
+ var valuesSuppliers = List.of(
|
|
|
+ MultiRowTestCaseSupplier.longCases(1, 1000, 0, 1000_000_000, true),
|
|
|
+ MultiRowTestCaseSupplier.intCases(1, 1000, 0, 1000_000_000, true),
|
|
|
+ MultiRowTestCaseSupplier.doubleCases(1, 1000, 0, 1000_000_000, true)
|
|
|
+
|
|
|
+ );
|
|
|
+ for (List<TestCaseSupplier.TypedDataSupplier> valuesSupplier : valuesSuppliers) {
|
|
|
+ for (TestCaseSupplier.TypedDataSupplier fieldSupplier : valuesSupplier) {
|
|
|
+ TestCaseSupplier testCaseSupplier = makeSupplier(fieldSupplier);
|
|
|
+ suppliers.add(testCaseSupplier);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(suppliers);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Expression build(Source source, List<Expression> args) {
|
|
|
+ return new Rate(source, args.get(0), args.get(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void testAggregate() {
|
|
|
+ assumeTrue("time-series aggregation doesn't support ungrouped", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void testAggregateIntermediate() {
|
|
|
+ assumeTrue("time-series aggregation doesn't support ungrouped", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static DataType counterType(DataType type) {
|
|
|
+ return switch (type) {
|
|
|
+ case DOUBLE -> DataType.COUNTER_DOUBLE;
|
|
|
+ case LONG -> DataType.COUNTER_LONG;
|
|
|
+ case INTEGER -> DataType.COUNTER_INTEGER;
|
|
|
+ default -> throw new AssertionError("unknown type for counter: " + type);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier fieldSupplier) {
|
|
|
+ DataType type = counterType(fieldSupplier.type());
|
|
|
+ return new TestCaseSupplier(fieldSupplier.name(), List.of(type, DataType.DATETIME), () -> {
|
|
|
+ TestCaseSupplier.TypedData fieldTypedData = fieldSupplier.get();
|
|
|
+ List<Object> dataRows = fieldTypedData.multiRowData();
|
|
|
+ fieldTypedData = TestCaseSupplier.TypedData.multiRow(dataRows, type, fieldTypedData.name());
|
|
|
+ List<Long> timestamps = new ArrayList<>();
|
|
|
+ long lastTimestamp = randomLongBetween(0, 1_000_000);
|
|
|
+ for (int row = 0; row < dataRows.size(); row++) {
|
|
|
+ lastTimestamp += randomLongBetween(1, 10_000);
|
|
|
+ timestamps.add(lastTimestamp);
|
|
|
+ }
|
|
|
+ TestCaseSupplier.TypedData timestampsField = TestCaseSupplier.TypedData.multiRow(
|
|
|
+ timestamps.reversed(),
|
|
|
+ DataType.DATETIME,
|
|
|
+ "timestamps"
|
|
|
+ );
|
|
|
+ final Matcher<?> matcher;
|
|
|
+ if (dataRows.size() < 2) {
|
|
|
+ matcher = Matchers.nullValue();
|
|
|
+ } else {
|
|
|
+ // TODO: check the value?
|
|
|
+ matcher = Matchers.allOf(Matchers.greaterThanOrEqualTo(0.0), Matchers.lessThan(Double.POSITIVE_INFINITY));
|
|
|
+ }
|
|
|
+ return new TestCaseSupplier.TestCase(
|
|
|
+ List.of(fieldTypedData, timestampsField),
|
|
|
+ "Rate[field=Attribute[channel=0],timestamp=Attribute[channel=1]]",
|
|
|
+ DataType.DOUBLE,
|
|
|
+ matcher
|
|
|
+ );
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<DataType> signatureTypes(List<DataType> testCaseTypes) {
|
|
|
+ assertThat(testCaseTypes, hasSize(2));
|
|
|
+ assertThat(testCaseTypes.get(1), equalTo(DataType.DATETIME));
|
|
|
+ return List.of(testCaseTypes.get(0));
|
|
|
+ }
|
|
|
+}
|