jihoonson commented on a change in pull request #7331: TDigest backed sketch 
aggregators
URL: https://github.com/apache/incubator-druid/pull/7331#discussion_r279134453
 
 

 ##########
 File path: 
extensions-contrib/tdigestsketch/src/main/java/org/apache/druid/query/aggregation/tdigestsketch/TDigestBuildSketchAggregatorFactory.java
 ##########
 @@ -0,0 +1,269 @@
+/*
+ * 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.aggregation.tdigestsketch;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.tdunning.math.stats.MergingDigest;
+import com.tdunning.math.stats.TDigest;
+import org.apache.druid.query.aggregation.Aggregator;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import 
org.apache.druid.query.aggregation.AggregatorFactoryNotMergeableException;
+import org.apache.druid.query.aggregation.AggregatorUtil;
+import org.apache.druid.query.aggregation.BufferAggregator;
+import org.apache.druid.query.cache.CacheKeyBuilder;
+import org.apache.druid.segment.ColumnSelectorFactory;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.column.ValueType;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Aggregation operations over the tdigest-based quantile sketch
+ * available on <a href="https://github.com/tdunning/t-digest";>github</a> and 
described
+ * in the paper
+ * <a 
href="https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf";>
+ * Computing extremely accurate quantiles using t-digests</a>.
+ * <p>
+ * <p>
+ * At the time of writing this implementation, there are two flavors of {@link 
TDigest}
+ * available - {@link MergingDigest} and {@link 
com.tdunning.math.stats.AVLTreeDigest}.
+ * This implementation uses {@link MergingDigest} since it is more suited for 
the cases
+ * when we have to merge intermediate aggregations which Druid needs to do as
+ * part of query processing.
+ */
+public class TDigestBuildSketchAggregatorFactory extends AggregatorFactory
+{
+
+  // Default compression
+  public static final int DEFAULT_COMRESSION = 100;
+
+  @Nonnull
+  private final String name;
+  @Nonnull
+  private final String fieldName;
+  @Nonnull
+  final Integer compression;
+  @Nonnull
+  private final byte cacheTypeId;
+
+  public static final String TYPE_NAME = "buildTDigestSketch";
+
+  @JsonCreator
+  public TDigestBuildSketchAggregatorFactory(
+      @JsonProperty("name") final String name,
+      @JsonProperty("fieldName") final String fieldName,
+      @Nullable @JsonProperty("compression") final Integer compression
+  )
+  {
+    this(name, fieldName, compression, 
AggregatorUtil.TDIGEST_BUILD_SKETCH_CACHE_TYPE_ID);
+  }
+
+  TDigestBuildSketchAggregatorFactory(
+      final String name,
+      final String fieldName,
+      @Nullable final Integer compression,
+      final byte cacheTypeId
+  )
+  {
+    Objects.requireNonNull(name, "Must have a valid, non-null aggregator 
name");
+    this.name = name;
+    Objects.requireNonNull(fieldName, "Parameter fieldName must be specified");
+    this.fieldName = fieldName;
+    this.compression = compression == null ? DEFAULT_COMRESSION : compression;
+    this.cacheTypeId = cacheTypeId;
+  }
+
+
+  @Override
+  public byte[] getCacheKey()
+  {
+    return new CacheKeyBuilder(
+        cacheTypeId
+    ).appendString(fieldName).appendInt(compression).build();
+  }
+
+
+  @Override
+  public Aggregator factorize(ColumnSelectorFactory metricFactory)
+  {
+    ColumnCapabilities cap = metricFactory.getColumnCapabilities(fieldName);
+    if (cap == null || ValueType.isNumeric(cap.getType())) {
+      final ColumnValueSelector<Double> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new TDigestBuildSketchAggregator(selector, compression);
+    } else {
+      final ColumnValueSelector<MergingDigest> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new TDigestMergeSketchAggregator(selector, compression);
+    }
+  }
+
+  @Override
+  public BufferAggregator factorizeBuffered(ColumnSelectorFactory 
metricFactory)
+  {
+    ColumnCapabilities cap = metricFactory.getColumnCapabilities(fieldName);
+    if (cap == null || ValueType.isNumeric(cap.getType())) {
+      final ColumnValueSelector<Double> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new TDigestBuildSketchBufferAggregator(selector, compression);
+    } else {
+      final ColumnValueSelector<MergingDigest> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new TDigestMergeSketchBufferAggregator(selector, compression);
+    }
+  }
+
+  public static final Comparator<TDigest> COMPARATOR = Comparator.nullsFirst(
+      Comparator.comparingLong(a -> a.size())
+  );
+
+  @Override
+  public Comparator getComparator()
+  {
+    return COMPARATOR;
+  }
+
+  @Override
+  public Object combine(@Nullable Object lhs, @Nullable Object rhs)
+  {
+    if (lhs == null) {
+      return rhs;
+    }
+    if (rhs == null) {
+      return lhs;
+    }
+    TDigest union = (TDigest) lhs;
+    union.add((TDigest) rhs);
+    return union;
+  }
+
+  @Override
+  public AggregatorFactory getCombiningFactory()
+  {
+    return new TDigestMergeSketchAggregatorFactory(name, name, compression);
+  }
+
+  @Override
+  public AggregatorFactory getMergingFactory(AggregatorFactory other) throws 
AggregatorFactoryNotMergeableException
+  {
+    if (other.getName().equals(this.getName()) && this.getClass() == 
other.getClass()) {
+      return getCombiningFactory();
+    } else {
+      throw new AggregatorFactoryNotMergeableException(this, other);
+    }
+  }
+
+  @Override
+  public List<AggregatorFactory> getRequiredColumns()
+  {
+    return Collections.singletonList(
+        new TDigestBuildSketchAggregatorFactory(
+            fieldName,
+            fieldName,
+            compression
+        )
+    );
+  }
+
+  @Override
+  public Object deserialize(Object serializedSketch)
+  {
+    return TDigestSketchOperations.deserialize(serializedSketch);
+  }
+
+  @Override
+  public Object finalizeComputation(Object object)
+  {
+    return object;
+  }
+
+  @Override
+  @JsonProperty
+  public String getName()
+  {
+    return name;
+  }
+
+  @JsonProperty
+  public String getFieldName()
+  {
+    return fieldName;
+  }
+
+  @JsonProperty
+  public int getCompression()
+  {
+    return compression;
+  }
+
+  @Override
+  public List<String> requiredFields()
+  {
+    return Collections.singletonList(fieldName);
+  }
+
+  @Override
+  public String getTypeName()
+  {
+    return TYPE_NAME;
+  }
+
+  @Override
+  public int getMaxIntermediateSize()
+  {
+    //TODO: samarth need to come up with a good value here
 
 Review comment:
   Would you please raise an issue for this?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to