From 607b0b9310db0b3a063c24e1ee752c0f1e7c51b1 Mon Sep 17 00:00:00 2001 From: Karan Kumar Date: Mon, 8 Aug 2022 18:31:56 +0530 Subject: [PATCH] Adding withName implementation to AggregatorFactory (#12862) * Adding agg factory with name impl * Adding test cases * Fixing test case * Fixing test case * Updated java docs. --- .../DistinctCountAggregatorFactory.java | 6 ++ .../DistinctCountGroupByQueryTest.java | 13 ++++ .../MomentSketchAggregatorFactory.java | 6 ++ .../MomentSketchMergeAggregatorFactory.java | 6 ++ .../MomentSketchAggregatorFactoryTest.java | 17 ++++++ .../movingaverage/AveragerFactoryWrapper.java | 26 ++++++++ .../averagers/AveragerFactoryWrapperTest.java | 38 ++++++++++++ .../TDigestSketchAggregatorFactory.java | 6 ++ .../TDigestSketchAggregatorFactoryTest.java | 8 +++ .../TimestampMaxAggregatorFactory.java | 6 ++ .../TimestampMinAggregatorFactory.java | 6 ++ .../TimestampMinMaxAggregatorFactoryTest.java | 12 ++++ .../hll/HllSketchBuildAggregatorFactory.java | 7 +++ .../hll/HllSketchMergeAggregatorFactory.java | 6 ++ .../DoublesSketchAggregatorFactory.java | 6 ++ .../DoublesSketchMergeAggregatorFactory.java | 6 ++ .../theta/SketchMergeAggregatorFactory.java | 13 ++++ .../OldSketchBuildAggregatorFactory.java | 7 +++ .../OldSketchMergeAggregatorFactory.java | 8 +++ ...ArrayOfDoublesSketchAggregatorFactory.java | 26 +++++--- .../hll/HllSketchAggregatorFactoryTest.java | 17 ++++++ .../HllSketchMergeAggregatorFactoryTest.java | 8 +++ .../DoublesSketchAggregatorFactoryTest.java | 13 ++++ ...ublesSketchMergeAggregatorFactoryTest.java | 12 ++++ .../theta/SketchAggregatorFactoryTest.java | 7 +++ .../oldapi/OldApiSketchAggregationTest.java | 17 ++++++ ...yOfDoublesSketchAggregatorFactoryTest.java | 8 +++ .../bloom/BloomFilterAggregatorFactory.java | 6 ++ .../BloomFilterMergeAggregatorFactory.java | 6 ++ .../BloomFilterAggregatorFactoryTest.java | 21 +++++++ ...ApproximateHistogramAggregatorFactory.java | 14 +++++ ...mateHistogramFoldingAggregatorFactory.java | 14 +++++ ...ixedBucketsHistogramAggregatorFactory.java | 14 +++++ .../ApproximateHistogramAggregatorTest.java | 16 +++++ ...eHistogramFoldingVectorAggregatorTest.java | 8 +++ ...dBucketsHistogramBufferAggregatorTest.java | 16 +++++ .../variance/VarianceAggregatorFactory.java | 6 ++ .../VarianceFoldingAggregatorFactory.java | 7 +++ .../VarianceAggregatorFactoryTest.java | 16 +++++ .../query/aggregation/AggregatorFactory.java | 20 ++++++ .../aggregation/CountAggregatorFactory.java | 6 ++ .../DoubleMaxAggregatorFactory.java | 6 ++ .../DoubleMinAggregatorFactory.java | 6 ++ .../DoubleSumAggregatorFactory.java | 6 ++ .../ExpressionLambdaAggregatorFactory.java | 21 +++++++ .../FilteredAggregatorFactory.java | 6 ++ .../FloatMaxAggregatorFactory.java | 6 ++ .../FloatMinAggregatorFactory.java | 6 ++ .../FloatSumAggregatorFactory.java | 6 ++ .../GroupingAggregatorFactory.java | 6 ++ .../HistogramAggregatorFactory.java | 6 ++ .../JavaScriptAggregatorFactory.java | 13 ++++ .../aggregation/LongMaxAggregatorFactory.java | 6 ++ .../aggregation/LongMinAggregatorFactory.java | 6 ++ .../aggregation/LongSumAggregatorFactory.java | 6 ++ .../SuppressedAggregatorFactory.java | 6 ++ .../any/DoubleAnyAggregatorFactory.java | 6 ++ .../any/FloatAnyAggregatorFactory.java | 6 ++ .../any/LongAnyAggregatorFactory.java | 6 ++ .../any/StringAnyAggregatorFactory.java | 6 ++ .../CardinalityAggregatorFactory.java | 6 ++ .../first/DoubleFirstAggregatorFactory.java | 6 ++ .../first/FloatFirstAggregatorFactory.java | 6 ++ .../first/LongFirstAggregatorFactory.java | 6 ++ .../first/StringFirstAggregatorFactory.java | 6 ++ .../StringFirstFoldingAggregatorFactory.java | 7 +++ .../HyperUniquesAggregatorFactory.java | 6 ++ .../last/DoubleLastAggregatorFactory.java | 6 ++ .../last/FloatLastAggregatorFactory.java | 6 ++ .../last/LongLastAggregatorFactory.java | 6 ++ .../last/StringLastAggregatorFactory.java | 6 ++ .../StringLastFoldingAggregatorFactory.java | 7 +++ .../mean/DoubleMeanAggregatorFactory.java | 26 ++++++++ .../TestBigDecimalSumAggregatorFactory.java | 7 +++ .../aggregation/AggregatorFactoryTest.java | 61 +++++++++++++++++++ .../aggregation/CountAggregatorTest.java | 9 +++ .../query/metadata/SegmentAnalyzerTest.java | 6 ++ .../AlwaysTwoCounterAggregatorFactory.java | 10 ++- 78 files changed, 802 insertions(+), 8 deletions(-) create mode 100644 extensions-contrib/moving-average-query/src/test/java/org/apache/druid/query/movingaverage/averagers/AveragerFactoryWrapperTest.java diff --git a/extensions-contrib/distinctcount/src/main/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountAggregatorFactory.java b/extensions-contrib/distinctcount/src/main/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountAggregatorFactory.java index 7ab4a6be7d4..ce6f2f30bc9 100644 --- a/extensions-contrib/distinctcount/src/main/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountAggregatorFactory.java +++ b/extensions-contrib/distinctcount/src/main/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountAggregatorFactory.java @@ -89,6 +89,12 @@ public class DistinctCountAggregatorFactory extends AggregatorFactory } } + @Override + public AggregatorFactory withName(String newName) + { + return new DistinctCountAggregatorFactory(newName, getFieldName(), getBitMapFactory()); + } + private DimensionSelector makeDimensionSelector(final ColumnSelectorFactory columnFactory) { return columnFactory.makeDimensionSelector(new DefaultDimensionSpec(fieldName, fieldName)); diff --git a/extensions-contrib/distinctcount/src/test/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountGroupByQueryTest.java b/extensions-contrib/distinctcount/src/test/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountGroupByQueryTest.java index 8328600561f..325d5701ded 100644 --- a/extensions-contrib/distinctcount/src/test/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountGroupByQueryTest.java +++ b/extensions-contrib/distinctcount/src/test/java/org/apache/druid/query/aggregation/distinctcount/DistinctCountGroupByQueryTest.java @@ -45,6 +45,7 @@ import org.apache.druid.segment.incremental.IncrementalIndexSchema; import org.apache.druid.segment.incremental.OnheapIncrementalIndex; import org.apache.druid.testing.InitializedNullHandlingTest; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -157,4 +158,16 @@ public class DistinctCountGroupByQueryTest extends InitializedNullHandlingTest ); TestHelper.assertExpectedObjects(expectedResults, results, "distinct-count"); } + + @Test + public void testWithName() + { + DistinctCountAggregatorFactory aggregatorFactory = new DistinctCountAggregatorFactory( + "distinct", + "visitor_id", + null + ); + Assert.assertEquals(aggregatorFactory, aggregatorFactory.withName("distinct")); + Assert.assertEquals("newTest", aggregatorFactory.withName("newTest").getName()); + } } diff --git a/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactory.java b/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactory.java index b4dcda979e2..ebaab352aa3 100644 --- a/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactory.java +++ b/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactory.java @@ -264,6 +264,12 @@ public class MomentSketchAggregatorFactory extends AggregatorFactory return (k + 2) * Double.BYTES + 2 * Integer.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new MomentSketchAggregatorFactory(newName, getFieldName(), getK(), getCompress(), cacheTypeId); + } + @Override public boolean equals(final Object o) { diff --git a/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchMergeAggregatorFactory.java b/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchMergeAggregatorFactory.java index 27444589798..bdba9638e9e 100644 --- a/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchMergeAggregatorFactory.java +++ b/extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchMergeAggregatorFactory.java @@ -22,6 +22,7 @@ package org.apache.druid.query.aggregation.momentsketch.aggregator; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.druid.query.aggregation.Aggregator; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.AggregatorUtil; import org.apache.druid.query.aggregation.BufferAggregator; import org.apache.druid.query.aggregation.momentsketch.MomentSketchWrapper; @@ -59,4 +60,9 @@ public class MomentSketchMergeAggregatorFactory extends MomentSketchAggregatorFa return new MomentSketchMergeBufferAggregator(selector, getK(), getCompress()); } + @Override + public AggregatorFactory withName(String newName) + { + return new MomentSketchMergeAggregatorFactory(newName, getK(), getCompress()); + } } diff --git a/extensions-contrib/momentsketch/src/test/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactoryTest.java b/extensions-contrib/momentsketch/src/test/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactoryTest.java index 5ad88eef334..096632214cb 100644 --- a/extensions-contrib/momentsketch/src/test/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactoryTest.java +++ b/extensions-contrib/momentsketch/src/test/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactoryTest.java @@ -86,4 +86,21 @@ public class MomentSketchAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + MomentSketchAggregatorFactory sketchAggFactory = new MomentSketchAggregatorFactory( + "name", "fieldName", 128, true + ); + Assert.assertEquals(sketchAggFactory, sketchAggFactory.withName("name")); + Assert.assertEquals("newTest", sketchAggFactory.withName("newTest").getName()); + + + MomentSketchMergeAggregatorFactory sketchMergeAggregatorFactory = new MomentSketchMergeAggregatorFactory( + "name", 128, true + ); + Assert.assertEquals(sketchMergeAggregatorFactory, sketchMergeAggregatorFactory.withName("name")); + Assert.assertEquals("newTest", sketchMergeAggregatorFactory.withName("newTest").getName()); + } } diff --git a/extensions-contrib/moving-average-query/src/main/java/org/apache/druid/query/movingaverage/AveragerFactoryWrapper.java b/extensions-contrib/moving-average-query/src/main/java/org/apache/druid/query/movingaverage/AveragerFactoryWrapper.java index dff8aed7a84..3c5e30e5bdd 100644 --- a/extensions-contrib/moving-average-query/src/main/java/org/apache/druid/query/movingaverage/AveragerFactoryWrapper.java +++ b/extensions-contrib/moving-average-query/src/main/java/org/apache/druid/query/movingaverage/AveragerFactoryWrapper.java @@ -29,6 +29,7 @@ import org.apache.druid.segment.column.ColumnType; import javax.annotation.Nullable; import java.util.Comparator; import java.util.List; +import java.util.Objects; /** * A wrapper around averagers that makes them appear to be aggregators. @@ -181,4 +182,29 @@ public class AveragerFactoryWrapper extends AggregatorFactory { throw new UnsupportedOperationException("Invalid operation for AveragerFactoryWrapper."); } + + @Override + public AggregatorFactory withName(String newName) + { + return new AveragerFactoryWrapper(af, newName); + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AveragerFactoryWrapper that = (AveragerFactoryWrapper) o; + return af.equals(that.af) && prefix.equals(that.prefix); + } + + @Override + public int hashCode() + { + return Objects.hash(af, prefix); + } } diff --git a/extensions-contrib/moving-average-query/src/test/java/org/apache/druid/query/movingaverage/averagers/AveragerFactoryWrapperTest.java b/extensions-contrib/moving-average-query/src/test/java/org/apache/druid/query/movingaverage/averagers/AveragerFactoryWrapperTest.java new file mode 100644 index 00000000000..0d9e4f0c885 --- /dev/null +++ b/extensions-contrib/moving-average-query/src/test/java/org/apache/druid/query/movingaverage/averagers/AveragerFactoryWrapperTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.query.movingaverage.averagers; + +import org.apache.druid.query.movingaverage.AveragerFactoryWrapper; +import org.junit.Assert; +import org.junit.Test; + +public class AveragerFactoryWrapperTest +{ + @Test + public void testWithName() + { + AveragerFactoryWrapper factoryWrapper = new AveragerFactoryWrapper( + new DoubleMaxAveragerFactory("double", 1, 1, "test"), + "test" + ); + Assert.assertEquals(factoryWrapper, factoryWrapper.withName("test")); + Assert.assertEquals("newTestdouble", factoryWrapper.withName("newTest").getName()); + } +} diff --git a/extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactory.java b/extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactory.java index a1067bd9f1f..58d0dcc4211 100644 --- a/extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactory.java +++ b/extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactory.java @@ -233,6 +233,12 @@ public class TDigestSketchAggregatorFactory extends AggregatorFactory return TDigestSketchUtils.getMaxIntermdiateTDigestSize(compression); } + @Override + public AggregatorFactory withName(String newName) + { + return new TDigestSketchAggregatorFactory(newName, getFieldName(), getCompression(), cacheTypeId); + } + @Override public AggregateCombiner makeAggregateCombiner() { diff --git a/extensions-contrib/tdigestsketch/src/test/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactoryTest.java b/extensions-contrib/tdigestsketch/src/test/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactoryTest.java index be3a8029023..f28fb9ad7b7 100644 --- a/extensions-contrib/tdigestsketch/src/test/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactoryTest.java +++ b/extensions-contrib/tdigestsketch/src/test/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestSketchAggregatorFactoryTest.java @@ -62,4 +62,12 @@ public class TDigestSketchAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + TDigestSketchAggregatorFactory factory = new TDigestSketchAggregatorFactory("tdigest", "col", null); + Assert.assertEquals(factory, factory.withName("tdigest")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMaxAggregatorFactory.java b/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMaxAggregatorFactory.java index fea1c85080d..aec9d1bf134 100644 --- a/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMaxAggregatorFactory.java +++ b/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMaxAggregatorFactory.java @@ -55,6 +55,12 @@ public class TimestampMaxAggregatorFactory extends TimestampAggregatorFactory ); } + @Override + public AggregatorFactory withName(String newName) + { + return new TimestampMaxAggregatorFactory(newName, getFieldName(), getTimeFormat()); + } + @Override public String toString() { diff --git a/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMinAggregatorFactory.java b/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMinAggregatorFactory.java index 79cb1cadfae..d463a466981 100644 --- a/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMinAggregatorFactory.java +++ b/extensions-contrib/time-min-max/src/main/java/org/apache/druid/query/aggregation/TimestampMinAggregatorFactory.java @@ -54,6 +54,12 @@ public class TimestampMinAggregatorFactory extends TimestampAggregatorFactory ); } + @Override + public AggregatorFactory withName(String newName) + { + return new TimestampMinAggregatorFactory(newName, getFieldName(), getTimeFormat()); + } + @Override public String toString() { diff --git a/extensions-contrib/time-min-max/src/test/java/org/apache/druid/query/aggregation/TimestampMinMaxAggregatorFactoryTest.java b/extensions-contrib/time-min-max/src/test/java/org/apache/druid/query/aggregation/TimestampMinMaxAggregatorFactoryTest.java index aea3ed5266d..7f3446d62df 100644 --- a/extensions-contrib/time-min-max/src/test/java/org/apache/druid/query/aggregation/TimestampMinMaxAggregatorFactoryTest.java +++ b/extensions-contrib/time-min-max/src/test/java/org/apache/druid/query/aggregation/TimestampMinMaxAggregatorFactoryTest.java @@ -119,4 +119,16 @@ public class TimestampMinMaxAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + TimestampMaxAggregatorFactory maxAgg = new TimestampMaxAggregatorFactory("timeMax", "__time", null); + Assert.assertEquals(maxAgg, maxAgg.withName("timeMax")); + Assert.assertEquals("newTest", maxAgg.withName("newTest").getName()); + + TimestampMinAggregatorFactory minAgg = new TimestampMinAggregatorFactory("timeMin", "__time", null); + Assert.assertEquals(minAgg, minAgg.withName("timeMin")); + Assert.assertEquals("newTest", minAgg.withName("newTest").getName()); + } } diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildAggregatorFactory.java index 076cea4a0f5..65d27df6e30 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchBuildAggregatorFactory.java @@ -25,6 +25,7 @@ import org.apache.datasketches.hll.HllSketch; import org.apache.datasketches.hll.TgtHllType; import org.apache.druid.java.util.common.ISE; import org.apache.druid.query.aggregation.Aggregator; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.AggregatorUtil; import org.apache.druid.query.aggregation.BufferAggregator; import org.apache.druid.query.aggregation.VectorAggregator; @@ -121,6 +122,12 @@ public class HllSketchBuildAggregatorFactory extends HllSketchAggregatorFactory return HllSketch.getMaxUpdatableSerializationBytes(getLgK(), TgtHllType.valueOf(getTgtHllType())); } + @Override + public AggregatorFactory withName(String newName) + { + return new HllSketchBuildAggregatorFactory(newName, getFieldName(), getLgK(), getTgtHllType(), isRound()); + } + private void validateInputs(@Nullable ColumnCapabilities capabilities) { if (capabilities != null) { diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactory.java index 818cbaad08d..8286f8392ff 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactory.java @@ -131,4 +131,10 @@ public class HllSketchMergeAggregatorFactory extends HllSketchAggregatorFactory return Union.getMaxSerializationBytes(getLgK()); } + @Override + public AggregatorFactory withName(String newName) + { + return new HllSketchMergeAggregatorFactory(newName, getFieldName(), getLgK(), getTgtHllType(), isRound()); + } + } diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactory.java index 145ffcf2e85..19694a5842c 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactory.java @@ -304,6 +304,12 @@ public class DoublesSketchAggregatorFactory extends AggregatorFactory return DoublesSketch.getUpdatableStorageBytes(k, rows); } + @Override + public AggregatorFactory withName(String newName) + { + return new DoublesSketchAggregatorFactory(newName, getFieldName(), getK(), getMaxStreamLength(), cacheTypeId); + } + // Quantiles sketches never stop growing, but they do so very slowly. // This size must suffice for overwhelming majority of sketches, // but some sketches may request more memory on heap and move there diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactory.java index c8cd6b0ad13..55c39cff924 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactory.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; import org.apache.datasketches.quantiles.DoublesSketch; import org.apache.druid.query.aggregation.Aggregator; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.AggregatorUtil; import org.apache.druid.query.aggregation.BufferAggregator; import org.apache.druid.segment.ColumnSelectorFactory; @@ -74,4 +75,9 @@ public class DoublesSketchMergeAggregatorFactory extends DoublesSketchAggregator return new DoublesSketchMergeBufferAggregator(selector, getK(), getMaxIntermediateSizeWithNulls()); } + @Override + public AggregatorFactory withName(String newName) + { + return new DoublesSketchMergeAggregatorFactory(newName, getK(), getMaxStreamLength()); + } } diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/SketchMergeAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/SketchMergeAggregatorFactory.java index c598f148ad3..ee5b7bfb420 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/SketchMergeAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/SketchMergeAggregatorFactory.java @@ -164,6 +164,19 @@ public class SketchMergeAggregatorFactory extends SketchAggregatorFactory return getIntermediateType(); } + @Override + public AggregatorFactory withName(String newName) + { + return new SketchMergeAggregatorFactory( + newName, + getFieldName(), + getSize(), + getShouldFinalize(), + getIsInputThetaSketch(), + getErrorBoundsStdDev() + ); + } + @Override public boolean equals(Object o) { diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchBuildAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchBuildAggregatorFactory.java index c428794faaa..2c8d21941eb 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchBuildAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchBuildAggregatorFactory.java @@ -21,6 +21,7 @@ package org.apache.druid.query.aggregation.datasketches.theta.oldapi; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.datasketches.theta.SketchMergeAggregatorFactory; /** @@ -36,4 +37,10 @@ public class OldSketchBuildAggregatorFactory extends SketchMergeAggregatorFactor { super(name, fieldName, size, true, false, null); } + + @Override + public AggregatorFactory withName(String newName) + { + return new OldSketchBuildAggregatorFactory(newName, getFieldName(), getSize()); + } } diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchMergeAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchMergeAggregatorFactory.java index 7241153bd93..e884b210a3a 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchMergeAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldSketchMergeAggregatorFactory.java @@ -21,6 +21,7 @@ package org.apache.druid.query.aggregation.datasketches.theta.oldapi; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.datasketches.theta.SketchMergeAggregatorFactory; /** @@ -37,4 +38,11 @@ public class OldSketchMergeAggregatorFactory extends SketchMergeAggregatorFactor { super(name, fieldName, size, shouldFinalize, true, null); } + + @Override + public AggregatorFactory withName(String newName) + { + return new OldSketchMergeAggregatorFactory(newName, getFieldName(), getSize(), getShouldFinalize()); + } + } diff --git a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactory.java b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactory.java index 86a6e48a3cb..f1817d79eff 100644 --- a/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactory.java +++ b/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactory.java @@ -266,17 +266,29 @@ public class ArrayOfDoublesSketchAggregatorFactory extends AggregatorFactory return ArrayOfDoublesUnion.getMaxBytes(nominalEntries, numberOfValues); } + @Override + public AggregatorFactory withName(String newName) + { + return new ArrayOfDoublesSketchAggregatorFactory( + newName, + getFieldName(), + getNominalEntries(), + getMetricColumns(), + getNumberOfValues() + ); + } + @Override public List getRequiredColumns() { return Collections.singletonList( - new ArrayOfDoublesSketchAggregatorFactory( - fieldName, - fieldName, - nominalEntries, - metricColumns, - numberOfValues - ) + new ArrayOfDoublesSketchAggregatorFactory( + fieldName, + fieldName, + nominalEntries, + metricColumns, + numberOfValues + ) ); } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactoryTest.java index 08ed792000a..2177177e124 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactoryTest.java @@ -83,6 +83,17 @@ public class HllSketchAggregatorFactoryTest Assert.assertEquals(ROUND, aggregatorFactory.isRound()); } + + @Test + public void testWithName() + { + List aggregatorFactories = target.getRequiredColumns(); + Assert.assertEquals(1, aggregatorFactories.size()); + HllSketchAggregatorFactory aggregatorFactory = (HllSketchAggregatorFactory) aggregatorFactories.get(0); + Assert.assertEquals(aggregatorFactory, aggregatorFactory.withName(aggregatorFactory.getName())); + Assert.assertEquals("newTest", aggregatorFactory.withName("newTest").getName()); + } + @Test public void testFinalizeComputationNull() { @@ -362,5 +373,11 @@ public class HllSketchAggregatorFactoryTest { return DUMMY_SIZE; } + + @Override + public AggregatorFactory withName(String newName) + { + return new TestHllSketchAggregatorFactory(newName, getFieldName(), getLgK(), getTgtHllType(), isRound()); + } } } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactoryTest.java index 4e9961ee8e5..25d0726b779 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeAggregatorFactoryTest.java @@ -156,4 +156,12 @@ public class HllSketchMergeAggregatorFactoryTest HllSketchAggregatorFactory result = (HllSketchAggregatorFactory) targetRound.getMergingFactory(targetRound); Assert.assertTrue(result.isRound()); } + + @Test + public void testWithName() throws Exception + { + HllSketchAggregatorFactory factory = (HllSketchAggregatorFactory) targetRound.getMergingFactory(targetRound); + Assert.assertEquals(factory, factory.withName(targetRound.getName())); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactoryTest.java index d4867c7a19d..fb09425341b 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchAggregatorFactoryTest.java @@ -153,4 +153,17 @@ public class DoublesSketchAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + final DoublesSketchAggregatorFactory factory = new DoublesSketchAggregatorFactory( + "myFactory", + "myField", + 1024, + 1000L + ); + Assert.assertEquals(factory, factory.withName("myFactory")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactoryTest.java index 55d0b124624..facf45b1874 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/quantiles/DoublesSketchMergeAggregatorFactoryTest.java @@ -60,4 +60,16 @@ public class DoublesSketchMergeAggregatorFactoryTest ); Assert.assertEquals(factory, fromJson); } + + @Test + public void testWithName() + { + final DoublesSketchMergeAggregatorFactory factory = new DoublesSketchMergeAggregatorFactory( + "myFactory", + 1024, + 1000L + ); + Assert.assertEquals(factory, factory.withName("myFactory")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/SketchAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/SketchAggregatorFactoryTest.java index 1762ef3b484..d64b4263ac1 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/SketchAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/SketchAggregatorFactoryTest.java @@ -160,4 +160,11 @@ public class SketchAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + Assert.assertEquals(AGGREGATOR_16384, AGGREGATOR_16384.withName("x")); + Assert.assertEquals("newTest", AGGREGATOR_16384.withName("newTest").getName()); + } } diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldApiSketchAggregationTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldApiSketchAggregationTest.java index ed8cf36db3b..550af02a8d4 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldApiSketchAggregationTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/theta/oldapi/OldApiSketchAggregationTest.java @@ -242,6 +242,23 @@ public class OldApiSketchAggregationTest extends InitializedNullHandlingTest Assert.assertEquals(holders[0].getEstimate(), holders[1].getEstimate(), 0); } + @Test + public void testWithNameMerge() + { + OldSketchMergeAggregatorFactory factory = new OldSketchMergeAggregatorFactory("name", "fieldName", 16, null); + Assert.assertEquals(factory, factory.withName("name")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } + + + @Test + public void testWithNameBuild() + { + OldSketchBuildAggregatorFactory factory = new OldSketchBuildAggregatorFactory("name", "fieldName", 16); + Assert.assertEquals(factory, factory.withName("name")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } + private void assertPostAggregatorSerde(PostAggregator agg) throws Exception { Assert.assertEquals( diff --git a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactoryTest.java b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactoryTest.java index 9e3b5e1110e..10f2afe2248 100644 --- a/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactoryTest.java +++ b/extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/tuple/ArrayOfDoublesSketchAggregatorFactoryTest.java @@ -110,4 +110,12 @@ public class ArrayOfDoublesSketchAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + AggregatorFactory factory = new ArrayOfDoublesSketchAggregatorFactory("name", "", null, null, null); + Assert.assertEquals(factory, factory.withName("name")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactory.java b/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactory.java index 0ccf139e387..374f9ef3e6f 100644 --- a/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactory.java +++ b/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactory.java @@ -207,6 +207,12 @@ public class BloomFilterAggregatorFactory extends AggregatorFactory return BloomKFilter.computeSizeBytes(maxNumEntries); } + @Override + public AggregatorFactory withName(String newName) + { + return new BloomFilterAggregatorFactory(newName, getField(), getMaxNumEntries()); + } + @Override public byte[] getCacheKey() { diff --git a/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterMergeAggregatorFactory.java b/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterMergeAggregatorFactory.java index 7d74432a87c..233dc287c03 100644 --- a/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterMergeAggregatorFactory.java +++ b/extensions-core/druid-bloom-filter/src/main/java/org/apache/druid/query/aggregation/bloom/BloomFilterMergeAggregatorFactory.java @@ -71,6 +71,12 @@ public class BloomFilterMergeAggregatorFactory extends BloomFilterAggregatorFact .build(); } + @Override + public AggregatorFactory withName(String newName) + { + return new BloomFilterMergeAggregatorFactory(newName, fieldName, getMaxNumEntries()); + } + private BloomFilterMergeAggregator makeMergeAggregator(ColumnSelectorFactory metricFactory) { final BaseNullableColumnValueSelector selector = metricFactory.makeColumnValueSelector(fieldName); diff --git a/extensions-core/druid-bloom-filter/src/test/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactoryTest.java b/extensions-core/druid-bloom-filter/src/test/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactoryTest.java index 692494f5988..144141419f9 100644 --- a/extensions-core/druid-bloom-filter/src/test/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactoryTest.java +++ b/extensions-core/druid-bloom-filter/src/test/java/org/apache/druid/query/aggregation/bloom/BloomFilterAggregatorFactoryTest.java @@ -69,4 +69,25 @@ public class BloomFilterAggregatorFactoryTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithNameBloomFilterAggFactory() + { + BloomFilterAggregatorFactory factory = new BloomFilterAggregatorFactory( + "bloom", + DefaultDimensionSpec.of("col"), + 1024 + ); + Assert.assertEquals(factory, factory.withName("bloom")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } + + + @Test + public void testWithNameBloomFilterMergeAggFactory() + { + BloomFilterMergeAggregatorFactory factory = new BloomFilterMergeAggregatorFactory("bloomMerge", "bloom", 1024); + Assert.assertEquals(factory, factory.withName("bloomMerge")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorFactory.java b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorFactory.java index abd278cb28a..38291d96345 100644 --- a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorFactory.java +++ b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorFactory.java @@ -347,6 +347,20 @@ public class ApproximateHistogramAggregatorFactory extends AggregatorFactory return new ApproximateHistogram(resolution).getMaxStorageSize(); } + @Override + public AggregatorFactory withName(String newName) + { + return new ApproximateHistogramAggregatorFactory( + newName, + getFieldName(), + getResolution(), + getNumBuckets(), + getLowerLimit(), + getUpperLimit(), + finalizeAsBase64Binary + ); + } + @Override public String toString() { diff --git a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingAggregatorFactory.java b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingAggregatorFactory.java index 9d1d4e1423f..1d4ff7eeb42 100644 --- a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingAggregatorFactory.java +++ b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingAggregatorFactory.java @@ -142,6 +142,20 @@ public class ApproximateHistogramFoldingAggregatorFactory extends ApproximateHis return builder.build(); } + @Override + public AggregatorFactory withName(String newName) + { + return new ApproximateHistogramFoldingAggregatorFactory( + newName, + getFieldName(), + getResolution(), + getNumBuckets(), + getLowerLimit(), + getUpperLimit(), + finalizeAsBase64Binary + ); + } + @Override public String toString() { diff --git a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramAggregatorFactory.java b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramAggregatorFactory.java index 37fcf051db8..964a20dd977 100644 --- a/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramAggregatorFactory.java +++ b/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramAggregatorFactory.java @@ -302,6 +302,20 @@ public class FixedBucketsHistogramAggregatorFactory extends AggregatorFactory return FixedBucketsHistogram.SERDE_HEADER_SIZE + FixedBucketsHistogram.getFullStorageSize(numBuckets); } + @Override + public AggregatorFactory withName(String newName) + { + return new FixedBucketsHistogramAggregatorFactory( + newName, + getFieldName(), + getNumBuckets(), + getLowerLimit(), + getUpperLimit(), + getOutlierHandlingMode(), + isFinalizeAsBase64Binary() + ); + } + @Override public byte[] getCacheKey() { diff --git a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorTest.java b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorTest.java index 572b2e6c772..e1a1b6c6b86 100644 --- a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorTest.java +++ b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramAggregatorTest.java @@ -155,4 +155,20 @@ public class ApproximateHistogramAggregatorTest extends InitializedNullHandlingT new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + ApproximateHistogramAggregatorFactory factory = new ApproximateHistogramAggregatorFactory( + "approxHisto", + "col", + null, + null, + null, + null, + false + ); + Assert.assertEquals(factory, factory.withName("approxHisto")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingVectorAggregatorTest.java b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingVectorAggregatorTest.java index 3f813a5a196..44a31ae9f51 100644 --- a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingVectorAggregatorTest.java +++ b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramFoldingVectorAggregatorTest.java @@ -123,6 +123,14 @@ public class ApproximateHistogramFoldingVectorAggregatorTest } + @Test + public void testWithName() + { + ApproximateHistogramFoldingAggregatorFactory factory = buildHistogramFactory(); + Assert.assertEquals(factory, factory.withName("approximateHistoFold")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } + private ApproximateHistogramFoldingAggregatorFactory buildHistogramFactory() { return buildHistogramFactory("field"); diff --git a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramBufferAggregatorTest.java b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramBufferAggregatorTest.java index 36a78891c0e..fbe27856854 100644 --- a/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramBufferAggregatorTest.java +++ b/extensions-core/histogram/src/test/java/org/apache/druid/query/aggregation/histogram/FixedBucketsHistogramBufferAggregatorTest.java @@ -176,4 +176,20 @@ public class FixedBucketsHistogramBufferAggregatorTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + FixedBucketsHistogramAggregatorFactory factory = new FixedBucketsHistogramAggregatorFactory( + "billy", + "billy", + 5, + 0, + 50, + FixedBucketsHistogram.OutlierHandlingMode.OVERFLOW, + false + ); + Assert.assertEquals(factory, factory.withName("billy")); + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactory.java b/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactory.java index dcd420801d3..47eccfbffd5 100644 --- a/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactory.java +++ b/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactory.java @@ -117,6 +117,12 @@ public class VarianceAggregatorFactory extends AggregatorFactory return VarianceAggregatorCollector.getMaxIntermediateSize(); } + @Override + public AggregatorFactory withName(String newName) + { + return new VarianceAggregatorFactory(newName, getFieldName(), getEstimator(), inputType); + } + @Override public Aggregator factorize(ColumnSelectorFactory metricFactory) { diff --git a/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceFoldingAggregatorFactory.java b/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceFoldingAggregatorFactory.java index c9e15269c91..6a84cfac1f8 100644 --- a/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceFoldingAggregatorFactory.java +++ b/extensions-core/stats/src/main/java/org/apache/druid/query/aggregation/variance/VarianceFoldingAggregatorFactory.java @@ -21,6 +21,7 @@ package org.apache.druid.query.aggregation.variance; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonTypeName; +import org.apache.druid.query.aggregation.AggregatorFactory; import javax.annotation.Nullable; @@ -37,4 +38,10 @@ public class VarianceFoldingAggregatorFactory extends VarianceAggregatorFactory { super(name, fieldName, estimator, "variance"); } + + @Override + public AggregatorFactory withName(String newName) + { + return new VarianceFoldingAggregatorFactory(newName, getFieldName(), getEstimator()); + } } diff --git a/extensions-core/stats/src/test/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactoryTest.java b/extensions-core/stats/src/test/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactoryTest.java index d3fded4dfb8..20123aac1de 100644 --- a/extensions-core/stats/src/test/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactoryTest.java +++ b/extensions-core/stats/src/test/java/org/apache/druid/query/aggregation/variance/VarianceAggregatorFactoryTest.java @@ -85,4 +85,20 @@ public class VarianceAggregatorFactoryTest extends InitializedNullHandlingTest VarianceAggregatorFactory target = new VarianceAggregatorFactory("test", "test", null, null); Assert.assertEquals(NullHandling.defaultDoubleValue(), target.finalizeComputation(null)); } + + @Test + public void testWithName() + { + VarianceAggregatorFactory varianceAggregatorFactory = new VarianceAggregatorFactory("variance", "col"); + Assert.assertEquals(varianceAggregatorFactory, varianceAggregatorFactory.withName("variance")); + Assert.assertEquals("newTest", varianceAggregatorFactory.withName("newTest").getName()); + + VarianceFoldingAggregatorFactory varianceFoldingAggregatorFactory = new VarianceFoldingAggregatorFactory( + "varianceFold", + "col", + null + ); + Assert.assertEquals(varianceFoldingAggregatorFactory, varianceFoldingAggregatorFactory.withName("varianceFold")); + Assert.assertEquals("newTest", varianceFoldingAggregatorFactory.withName("newTest").getName()); + } } diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/AggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/AggregatorFactory.java index 12d7fbe1c1a..023cb1654e0 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/AggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/AggregatorFactory.java @@ -227,6 +227,9 @@ public abstract class AggregatorFactory implements Cacheable @Nullable public abstract Object finalizeComputation(@Nullable Object object); + /** + * @return output name of the aggregator column. + */ public abstract String getName(); /** @@ -347,6 +350,23 @@ public abstract class AggregatorFactory implements Cacheable return this; } + /** + * Used in cases where we want to change the output name of the aggregator to something else. For eg: if we have + * a query `select a, sum(b) as total group by a from table` the aggregator returned from the native group by query is "a0" set in + * {@link org.apache.druid.sql.calcite.rel.DruidQuery#computeAggregations}. We can use withName("total") to set the output name + * of the aggregator to "total". + *

+ * As all implementations of this interface method may not exist, callers of this method are advised to handle such a case. + * + * @param newName newName of the output for aggregator factory + * @return AggregatorFactory with the output name set as the input param. + */ + @SuppressWarnings("unused") + public AggregatorFactory withName(String newName) + { + throw new UOE("Cannot change output name for AggregatorFactory[%s].", this.getClass().getName()); + } + /** * Merges the list of AggregatorFactory[] (presumable from metadata of some segments being merged) and * returns merged AggregatorFactory[] (for the metadata for merged segment). diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/CountAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/CountAggregatorFactory.java index 566adedb520..24d8a9ce117 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/CountAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/CountAggregatorFactory.java @@ -109,6 +109,12 @@ public class CountAggregatorFactory extends AggregatorFactory return object; } + @Override + public AggregatorFactory withName(String newName) + { + return new CountAggregatorFactory(newName); + } + @Nullable @Override public Object finalizeComputation(@Nullable Object object) diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMaxAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMaxAggregatorFactory.java index 79bd6c6aaaf..10c93c4e80c 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMaxAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMaxAggregatorFactory.java @@ -117,6 +117,12 @@ public class DoubleMaxAggregatorFactory extends SimpleDoubleAggregatorFactory return Collections.singletonList(new DoubleMaxAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleMaxAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMinAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMinAggregatorFactory.java index 124200cdf45..29ffb1a23f3 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMinAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleMinAggregatorFactory.java @@ -117,6 +117,12 @@ public class DoubleMinAggregatorFactory extends SimpleDoubleAggregatorFactory return Collections.singletonList(new DoubleMinAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleMinAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleSumAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleSumAggregatorFactory.java index 1958c0deddf..86d846f0b04 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/DoubleSumAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/DoubleSumAggregatorFactory.java @@ -117,6 +117,12 @@ public class DoubleSumAggregatorFactory extends SimpleDoubleAggregatorFactory return Collections.singletonList(new DoubleSumAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleSumAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/ExpressionLambdaAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/ExpressionLambdaAggregatorFactory.java index 588e45db3d3..7629a4708da 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/ExpressionLambdaAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/ExpressionLambdaAggregatorFactory.java @@ -452,6 +452,27 @@ public class ExpressionLambdaAggregatorFactory extends AggregatorFactory return getIntermediateType().isNumeric() ? 2 + Long.BYTES : maxSizeBytes.getBytesInInt(); } + @Override + public AggregatorFactory withName(String newName) + { + return new ExpressionLambdaAggregatorFactory( + newName, + fields, + accumulatorId, + initialValueExpressionString, + initialCombineValueExpressionString, + isNullUnlessAggregated, + shouldAggregateNullInputs, + shouldCombineAggregateNullInputs, + foldExpressionString, + combineExpressionString, + compareExpressionString, + finalizeExpressionString, + maxSizeBytes, + macroTable + ); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/FilteredAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/FilteredAggregatorFactory.java index d08b0649d31..722bfe12ff8 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/FilteredAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/FilteredAggregatorFactory.java @@ -165,6 +165,12 @@ public class FilteredAggregatorFactory extends AggregatorFactory return name; } + @Override + public AggregatorFactory withName(String newName) + { + return new FilteredAggregatorFactory(delegate.withName(newName), dimFilter, newName); + } + @Override public List requiredFields() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/FloatMaxAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/FloatMaxAggregatorFactory.java index 8b6dccf9206..09c67e22829 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/FloatMaxAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/FloatMaxAggregatorFactory.java @@ -117,6 +117,12 @@ public class FloatMaxAggregatorFactory extends SimpleFloatAggregatorFactory return Collections.singletonList(new FloatMaxAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatMaxAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/FloatMinAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/FloatMinAggregatorFactory.java index cf35efaecee..bdf74005c9e 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/FloatMinAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/FloatMinAggregatorFactory.java @@ -117,6 +117,12 @@ public class FloatMinAggregatorFactory extends SimpleFloatAggregatorFactory return Collections.singletonList(new FloatMinAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatMinAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/FloatSumAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/FloatSumAggregatorFactory.java index 7d47a36d62e..6cc674a37a0 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/FloatSumAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/FloatSumAggregatorFactory.java @@ -117,6 +117,12 @@ public class FloatSumAggregatorFactory extends SimpleFloatAggregatorFactory return Collections.singletonList(new FloatSumAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatSumAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/GroupingAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/GroupingAggregatorFactory.java index 793fe913d32..18b188389b4 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/GroupingAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/GroupingAggregatorFactory.java @@ -224,6 +224,12 @@ public class GroupingAggregatorFactory extends AggregatorFactory return Long.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new GroupingAggregatorFactory(newName, groupings, keyDimensions); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregatorFactory.java index 5d3a12f3e81..c3bbeae1519 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/HistogramAggregatorFactory.java @@ -229,6 +229,12 @@ public class HistogramAggregatorFactory extends AggregatorFactory return Long.BYTES * (breaks.length + 1) + Float.BYTES * 2; } + @Override + public AggregatorFactory withName(String newName) + { + return new HistogramAggregatorFactory(newName, fieldName, breaksList); + } + @Override public String toString() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/JavaScriptAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/JavaScriptAggregatorFactory.java index 00bcefd8ab1..2a43fb3c249 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/JavaScriptAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/JavaScriptAggregatorFactory.java @@ -285,6 +285,19 @@ public class JavaScriptAggregatorFactory extends AggregatorFactory return Double.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new JavaScriptAggregatorFactory( + newName, + getFieldNames(), + getFnAggregate(), + getFnReset(), + getFnCombine(), + config + ); + } + @Override public String toString() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/LongMaxAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/LongMaxAggregatorFactory.java index 9260b6d453a..926ad339c7e 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/LongMaxAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/LongMaxAggregatorFactory.java @@ -117,6 +117,12 @@ public class LongMaxAggregatorFactory extends SimpleLongAggregatorFactory return Collections.singletonList(new LongMaxAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new LongMaxAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/LongMinAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/LongMinAggregatorFactory.java index 5509014bfe5..f16e2fcc8b6 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/LongMinAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/LongMinAggregatorFactory.java @@ -117,6 +117,12 @@ public class LongMinAggregatorFactory extends SimpleLongAggregatorFactory return Collections.singletonList(new LongMinAggregatorFactory(fieldName, fieldName, expression, macroTable)); } + @Override + public AggregatorFactory withName(String newName) + { + return new LongMinAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/LongSumAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/LongSumAggregatorFactory.java index 3fd8cf0fb27..4e22ad106f8 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/LongSumAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/LongSumAggregatorFactory.java @@ -105,6 +105,12 @@ public class LongSumAggregatorFactory extends SimpleLongAggregatorFactory return new LongSumAggregateCombiner(); } + @Override + public AggregatorFactory withName(String newName) + { + return new LongSumAggregatorFactory(newName, getFieldName(), getExpression(), macroTable); + } + @Override public AggregatorFactory getCombiningFactory() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/SuppressedAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/SuppressedAggregatorFactory.java index 7702fd3a450..eb9605e8e3c 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/SuppressedAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/SuppressedAggregatorFactory.java @@ -169,6 +169,12 @@ public class SuppressedAggregatorFactory extends AggregatorFactory return this; } + @Override + public AggregatorFactory withName(String newName) + { + return new SuppressedAggregatorFactory(delegate.withName(newName)); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/any/DoubleAnyAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/any/DoubleAnyAggregatorFactory.java index 06103ac02f6..ca4c1d65d6a 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/any/DoubleAnyAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/any/DoubleAnyAggregatorFactory.java @@ -226,6 +226,12 @@ public class DoubleAnyAggregatorFactory extends AggregatorFactory return Double.BYTES + Byte.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleAnyAggregatorFactory(newName, getFieldName()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/any/FloatAnyAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/any/FloatAnyAggregatorFactory.java index 902447bba08..97ecab10228 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/any/FloatAnyAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/any/FloatAnyAggregatorFactory.java @@ -224,6 +224,12 @@ public class FloatAnyAggregatorFactory extends AggregatorFactory return Float.BYTES + Byte.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatAnyAggregatorFactory(newName, getFieldName()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/any/LongAnyAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/any/LongAnyAggregatorFactory.java index d4e0ae60637..6d6197f06ed 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/any/LongAnyAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/any/LongAnyAggregatorFactory.java @@ -222,6 +222,12 @@ public class LongAnyAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new LongAnyAggregatorFactory(newName, getFieldName()); + } + @Override public boolean equals(Object o) diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/any/StringAnyAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/any/StringAnyAggregatorFactory.java index 68f6dfc21ae..94b19e2743c 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/any/StringAnyAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/any/StringAnyAggregatorFactory.java @@ -196,6 +196,12 @@ public class StringAnyAggregatorFactory extends AggregatorFactory return Integer.BYTES + maxStringBytes; } + @Override + public AggregatorFactory withName(String newName) + { + return new StringAnyAggregatorFactory(newName, getFieldName(), getMaxStringBytes()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/cardinality/CardinalityAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/cardinality/CardinalityAggregatorFactory.java index 65040502d7b..39af58c8b35 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/cardinality/CardinalityAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/cardinality/CardinalityAggregatorFactory.java @@ -338,6 +338,12 @@ public class CardinalityAggregatorFactory extends AggregatorFactory return HyperLogLogCollector.getLatestNumBytesForDenseStorage(); } + @Override + public AggregatorFactory withName(String newName) + { + return new CardinalityAggregatorFactory(newName, null, getFields(), byRow, round); + } + @Override public boolean equals(final Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/first/DoubleFirstAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/first/DoubleFirstAggregatorFactory.java index 444ade46139..d575b263f0f 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/first/DoubleFirstAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/first/DoubleFirstAggregatorFactory.java @@ -302,6 +302,12 @@ public class DoubleFirstAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Double.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleFirstAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/first/FloatFirstAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/first/FloatFirstAggregatorFactory.java index f8a592d5868..be6a0f6aad9 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/first/FloatFirstAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/first/FloatFirstAggregatorFactory.java @@ -297,6 +297,12 @@ public class FloatFirstAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Float.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatFirstAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/first/LongFirstAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/first/LongFirstAggregatorFactory.java index bfc8ae48eaa..695d01b3a4a 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/first/LongFirstAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/first/LongFirstAggregatorFactory.java @@ -295,6 +295,12 @@ public class LongFirstAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Long.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new LongFirstAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstAggregatorFactory.java index 1e0c9bedbae..f7624f4541b 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstAggregatorFactory.java @@ -260,6 +260,12 @@ public class StringFirstAggregatorFactory extends AggregatorFactory return Long.BYTES + Integer.BYTES + maxStringBytes; } + @Override + public AggregatorFactory withName(String newName) + { + return new StringFirstAggregatorFactory(newName, getFieldName(), getTimeColumn(), getMaxStringBytes()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstFoldingAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstFoldingAggregatorFactory.java index 5441df00594..8c6c06856d8 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstFoldingAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/first/StringFirstFoldingAggregatorFactory.java @@ -20,6 +20,7 @@ package org.apache.druid.query.aggregation.first; import com.fasterxml.jackson.annotation.JsonCreator; +import org.apache.druid.query.aggregation.AggregatorFactory; /** * For backwards compatibility; equivalent to a regular StringFirstAggregatorFactory. @@ -31,4 +32,10 @@ public class StringFirstFoldingAggregatorFactory extends StringFirstAggregatorFa { super(name, fieldName, null, maxStringBytes); } + + @Override + public AggregatorFactory withName(String newName) + { + return new StringFirstFoldingAggregatorFactory(newName, getFieldName(), getMaxStringBytes()); + } } diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java index 3e21ea9fc21..3cb97045037 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/hyperloglog/HyperUniquesAggregatorFactory.java @@ -284,6 +284,12 @@ public class HyperUniquesAggregatorFactory extends AggregatorFactory return HyperLogLogCollector.getLatestNumBytesForDenseStorage(); } + @Override + public AggregatorFactory withName(String newName) + { + return new HyperUniquesAggregatorFactory(newName, getFieldName(), getIsInputHyperUnique(), isRound()); + } + @Override public String toString() { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/last/DoubleLastAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/last/DoubleLastAggregatorFactory.java index 37a69cf1125..5e3fa666792 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/last/DoubleLastAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/last/DoubleLastAggregatorFactory.java @@ -330,6 +330,12 @@ public class DoubleLastAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Double.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleLastAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/last/FloatLastAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/last/FloatLastAggregatorFactory.java index 451b6ce74ee..ff23c3d96dc 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/last/FloatLastAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/last/FloatLastAggregatorFactory.java @@ -325,6 +325,12 @@ public class FloatLastAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Float.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new FloatLastAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/last/LongLastAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/last/LongLastAggregatorFactory.java index dd844f9e3e1..a5304fe1092 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/last/LongLastAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/last/LongLastAggregatorFactory.java @@ -322,6 +322,12 @@ public class LongLastAggregatorFactory extends AggregatorFactory return Long.BYTES + Byte.BYTES + Long.BYTES; } + @Override + public AggregatorFactory withName(String newName) + { + return new LongLastAggregatorFactory(newName, getFieldName(), getTimeColumn()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastAggregatorFactory.java index 39c5b29647c..e1b39edc4ad 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastAggregatorFactory.java @@ -275,6 +275,12 @@ public class StringLastAggregatorFactory extends AggregatorFactory return Long.BYTES + Integer.BYTES + maxStringBytes; } + @Override + public AggregatorFactory withName(String newName) + { + return new StringLastAggregatorFactory(newName, getFieldName(), getTimeColumn(), getMaxStringBytes()); + } + @Override public boolean equals(Object o) { diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastFoldingAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastFoldingAggregatorFactory.java index c15bfaa41b2..ad18eeebadc 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastFoldingAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/last/StringLastFoldingAggregatorFactory.java @@ -20,6 +20,7 @@ package org.apache.druid.query.aggregation.last; import com.fasterxml.jackson.annotation.JsonCreator; +import org.apache.druid.query.aggregation.AggregatorFactory; /** * For backwards compatibility; equivalent to a regular StringLastAggregatorFactory. @@ -31,4 +32,10 @@ public class StringLastFoldingAggregatorFactory extends StringLastAggregatorFact { super(name, fieldName, null, maxStringBytes); } + + @Override + public AggregatorFactory withName(String newName) + { + return new StringLastFoldingAggregatorFactory(newName, getFieldName(), getMaxStringBytes()); + } } diff --git a/processing/src/main/java/org/apache/druid/query/aggregation/mean/DoubleMeanAggregatorFactory.java b/processing/src/main/java/org/apache/druid/query/aggregation/mean/DoubleMeanAggregatorFactory.java index 2507ae17af2..dc939e14323 100644 --- a/processing/src/main/java/org/apache/druid/query/aggregation/mean/DoubleMeanAggregatorFactory.java +++ b/processing/src/main/java/org/apache/druid/query/aggregation/mean/DoubleMeanAggregatorFactory.java @@ -41,6 +41,7 @@ import javax.annotation.Nullable; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Objects; /** */ @@ -100,6 +101,12 @@ public class DoubleMeanAggregatorFactory extends AggregatorFactory return DoubleMeanHolder.MAX_INTERMEDIATE_SIZE; } + @Override + public AggregatorFactory withName(String newName) + { + return new DoubleMeanAggregatorFactory(newName, getFieldName()); + } + @Override public Aggregator factorize(ColumnSelectorFactory metricFactory) { @@ -196,4 +203,23 @@ public class DoubleMeanAggregatorFactory extends AggregatorFactory .appendString(fieldName) .build(); } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DoubleMeanAggregatorFactory that = (DoubleMeanAggregatorFactory) o; + return Objects.equals(name, that.name) && Objects.equals(fieldName, that.fieldName); + } + + @Override + public int hashCode() + { + return Objects.hash(name, fieldName); + } } diff --git a/processing/src/test/java/org/apache/druid/query/TestBigDecimalSumAggregatorFactory.java b/processing/src/test/java/org/apache/druid/query/TestBigDecimalSumAggregatorFactory.java index 859ed62feba..e640f0aa75b 100644 --- a/processing/src/test/java/org/apache/druid/query/TestBigDecimalSumAggregatorFactory.java +++ b/processing/src/test/java/org/apache/druid/query/TestBigDecimalSumAggregatorFactory.java @@ -19,6 +19,7 @@ package org.apache.druid.query; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory; import javax.annotation.Nullable; @@ -45,6 +46,12 @@ public class TestBigDecimalSumAggregatorFactory extends DoubleSumAggregatorFacto } } + @Override + public AggregatorFactory withName(String newName) + { + return new TestBigDecimalSumAggregatorFactory(newName, getFieldName()); + } + @Override public Object deserialize(Object object) { diff --git a/processing/src/test/java/org/apache/druid/query/aggregation/AggregatorFactoryTest.java b/processing/src/test/java/org/apache/druid/query/aggregation/AggregatorFactoryTest.java index 1b2f80231b6..87d0e3dfdd8 100644 --- a/processing/src/test/java/org/apache/druid/query/aggregation/AggregatorFactoryTest.java +++ b/processing/src/test/java/org/apache/druid/query/aggregation/AggregatorFactoryTest.java @@ -50,6 +50,7 @@ import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -267,4 +268,64 @@ public class AggregatorFactoryTest extends InitializedNullHandlingTest new TimeseriesQueryQueryToolChest().resultArraySignature(query) ); } + + @Test + public void testWithName() + { + List aggregatorFactories = Arrays.asList( + new CountAggregatorFactory("col"), + new JavaScriptAggregatorFactory( + "col", + ImmutableList.of("col"), + "function(a,b) { return a + b; }", + "function() { return 0; }", + "function(a,b) { return a + b }", + new JavaScriptConfig(true) + ), + // long aggs + new LongSumAggregatorFactory("col", "long-col"), + new LongMinAggregatorFactory("col", "long-col"), + new LongMaxAggregatorFactory("col", "long-col"), + new LongFirstAggregatorFactory("col", "long-col", null), + new LongLastAggregatorFactory("col", "long-col", null), + new LongAnyAggregatorFactory("col", "long-col"), + // double aggs + new DoubleSumAggregatorFactory("col", "double-col"), + new DoubleMinAggregatorFactory("col", "double-col"), + new DoubleMaxAggregatorFactory("col", "double-col"), + new DoubleFirstAggregatorFactory("col", "double-col", null), + new DoubleLastAggregatorFactory("col", "double-col", null), + new DoubleAnyAggregatorFactory("col", "double-col"), + new DoubleMeanAggregatorFactory("col", "double-col"), + // float aggs + new FloatSumAggregatorFactory("col", "float-col"), + new FloatMinAggregatorFactory("col", "float-col"), + new FloatMaxAggregatorFactory("col", "float-col"), + new FloatFirstAggregatorFactory("col", "float-col", null), + new FloatLastAggregatorFactory("col", "float-col", null), + new FloatAnyAggregatorFactory("col", "float-col"), + // string aggregators + new StringFirstAggregatorFactory("col", "col", null, 1024), + new StringLastAggregatorFactory("col", "col", null, 1024), + new StringAnyAggregatorFactory("col", "col", 1024), + // sketch aggs + new CardinalityAggregatorFactory("col", ImmutableList.of(DefaultDimensionSpec.of("some-col")), false), + new HyperUniquesAggregatorFactory("col", "hyperunique"), + new HistogramAggregatorFactory("col", "histogram", ImmutableList.of(0.25f, 0.5f, 0.75f)), + // delegate aggs + new FilteredAggregatorFactory( + new HyperUniquesAggregatorFactory("col", "hyperunique"), + new SelectorDimFilter("col", "hello", null), + "col" + ), + new SuppressedAggregatorFactory( + new HyperUniquesAggregatorFactory("col", "hyperunique") + ) + ); + + for (AggregatorFactory aggregatorFactory : aggregatorFactories) { + Assert.assertEquals(aggregatorFactory, aggregatorFactory.withName("col")); + Assert.assertEquals("newTest", aggregatorFactory.withName("newTest").getName()); + } + } } diff --git a/processing/src/test/java/org/apache/druid/query/aggregation/CountAggregatorTest.java b/processing/src/test/java/org/apache/druid/query/aggregation/CountAggregatorTest.java index 71500d46bc5..361a699022d 100644 --- a/processing/src/test/java/org/apache/druid/query/aggregation/CountAggregatorTest.java +++ b/processing/src/test/java/org/apache/druid/query/aggregation/CountAggregatorTest.java @@ -61,4 +61,13 @@ public class CountAggregatorTest Assert.assertEquals(0, comp.compare(agg.get(), agg.get())); Assert.assertEquals(1, comp.compare(agg.get(), first)); } + + @Test + public void testWithName() + { + CountAggregatorFactory factory = new CountAggregatorFactory("test"); + Assert.assertEquals(factory, factory.withName("test")); + + Assert.assertEquals("newTest", factory.withName("newTest").getName()); + } } diff --git a/processing/src/test/java/org/apache/druid/query/metadata/SegmentAnalyzerTest.java b/processing/src/test/java/org/apache/druid/query/metadata/SegmentAnalyzerTest.java index b8c35917c3f..be6466520d0 100644 --- a/processing/src/test/java/org/apache/druid/query/metadata/SegmentAnalyzerTest.java +++ b/processing/src/test/java/org/apache/druid/query/metadata/SegmentAnalyzerTest.java @@ -476,6 +476,12 @@ public class SegmentAnalyzerTest extends InitializedNullHandlingTest return 0; } + @Override + public AggregatorFactory withName(String newName) + { + return new InvalidAggregatorFactory(newName, fieldName); + } + @Override public byte[] getCacheKey() { diff --git a/processing/src/test/java/org/apache/druid/segment/virtual/AlwaysTwoCounterAggregatorFactory.java b/processing/src/test/java/org/apache/druid/segment/virtual/AlwaysTwoCounterAggregatorFactory.java index 56f62cb00c5..465d15bc09a 100644 --- a/processing/src/test/java/org/apache/druid/segment/virtual/AlwaysTwoCounterAggregatorFactory.java +++ b/processing/src/test/java/org/apache/druid/segment/virtual/AlwaysTwoCounterAggregatorFactory.java @@ -21,6 +21,7 @@ package org.apache.druid.segment.virtual; import com.google.common.collect.ImmutableList; import org.apache.druid.query.aggregation.Aggregator; +import org.apache.druid.query.aggregation.AggregatorFactory; import org.apache.druid.query.aggregation.BufferAggregator; import org.apache.druid.query.aggregation.CountAggregatorFactory; import org.apache.druid.query.aggregation.CountVectorAggregator; @@ -83,7 +84,8 @@ public class AlwaysTwoCounterAggregatorFactory extends CountAggregatorFactory return new AlwaysTwoCounterVectorAggregator(selectorFactory.makeMultiValueDimensionSelector( DefaultDimensionSpec.of(fieldName))); } - return new AlwaysTwoCounterVectorAggregator(selectorFactory.makeSingleValueDimensionSelector(DefaultDimensionSpec.of(fieldName))); + return new AlwaysTwoCounterVectorAggregator(selectorFactory.makeSingleValueDimensionSelector( + DefaultDimensionSpec.of(fieldName))); } return new AlwaysTwoCounterVectorAggregator(selectorFactory.makeObjectSelector(fieldName)); default: @@ -91,6 +93,12 @@ public class AlwaysTwoCounterAggregatorFactory extends CountAggregatorFactory } } + @Override + public AggregatorFactory withName(String newName) + { + return new AlwaysTwoCounterAggregatorFactory(newName, fieldName); + } + public static class AlwaysTwoCounterVectorAggregator extends CountVectorAggregator { @Nullable