leventov commented on a change in pull request #6581: Moments Sketch custom 
aggregator
URL: https://github.com/apache/incubator-druid/pull/6581#discussion_r247849345
 
 

 ##########
 File path: 
extensions-contrib/momentsketch/src/main/java/org/apache/druid/query/aggregation/momentsketch/aggregator/MomentSketchAggregatorFactory.java
 ##########
 @@ -0,0 +1,290 @@
+/*
+ * 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.momentsketch.aggregator;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.commons.codec.Charsets;
+import org.apache.commons.codec.binary.Base64;
+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.AggregatorFactoryNotMergeableException;
+import org.apache.druid.query.aggregation.AggregatorUtil;
+import org.apache.druid.query.aggregation.BufferAggregator;
+import org.apache.druid.query.aggregation.momentsketch.MomentSketchWrapper;
+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.Nullable;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Aggregation operations over the moment-based quantile sketch
+ * available at: https://github.com/stanford-futuredata/momentsketch
+ */
+public class MomentSketchAggregatorFactory extends AggregatorFactory
+{
+  // Default number of moments (k) chosen for ~1% quantile error.
+  public static final int DEFAULT_K = 13;
+  // Safer to compress data with unknown ranges by default
+  public static final boolean DEFAULT_COMPRESS = true;
+
+  private final String name;
+  private final String fieldName;
+  // Number of moments tracked. Larger k allows for better estimates but 
greater resource usage
+  private final int k;
+  // Controls whether or not data is compressed onto a smaller range using 
arcsinh
+  private final boolean compress;
+  private final byte cacheTypeId;
+
+  public static final String TYPE_NAME = "momentSketch";
+
+  @JsonCreator
+  public MomentSketchAggregatorFactory(
+      @JsonProperty("name") final String name,
+      @JsonProperty("fieldName") final String fieldName,
+      @JsonProperty("k") final Integer k,
+      @JsonProperty("compress") final Boolean compress
+  )
+  {
+    this(name, fieldName, k, compress, 
AggregatorUtil.MOMENTS_SKETCH_BUILD_CACHE_TYPE_ID);
+  }
+
+  MomentSketchAggregatorFactory(
+      final String name,
+      final String fieldName,
+      @Nullable final Integer k,
+      @Nullable final Boolean compress,
+      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.k = k == null ? DEFAULT_K : k;
+    this.compress = compress == null ? DEFAULT_COMPRESS : compress;
+    this.cacheTypeId = cacheTypeId;
+  }
+
+
+  @Override
+  public byte[] getCacheKey()
+  {
+    return new CacheKeyBuilder(
+        cacheTypeId
+    ).appendString(fieldName).appendInt(k).appendBoolean(compress).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 MomentSketchBuildAggregator(selector, k, getCompress());
+    } else {
+      final ColumnValueSelector<MomentSketchWrapper> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new MomentSketchMergeAggregator(selector, k, getCompress());
+    }
+  }
+
+  @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 MomentSketchBuildBufferAggregator(selector, k, getCompress());
+    } else {
+      final ColumnValueSelector<MomentSketchWrapper> selector = 
metricFactory.makeColumnValueSelector(fieldName);
+      return new MomentSketchMergeBufferAggregator(selector, k, getCompress());
+    }
+  }
+
+  public static final Comparator<MomentSketchWrapper> COMPARATOR = 
Comparator.nullsFirst(
+      Comparator.comparingDouble(a -> a.getPowerSums()[0])
+  );
+
+  @Override
+  public Comparator getComparator()
+  {
+    return COMPARATOR;
+  }
+
+  @Override
+  public Object combine(Object lhs, Object rhs)
+  {
+    if (lhs == null) {
+      return rhs;
+    }
+    if (rhs == null) {
+      return lhs;
+    }
+    MomentSketchWrapper union = (MomentSketchWrapper) lhs;
+    union.merge((MomentSketchWrapper) rhs);
+    return union;
+  }
+
+  @Override
+  public AggregatorFactory getCombiningFactory()
+  {
+    return new MomentSketchMergeAggregatorFactory(name, k, compress);
+  }
+
+  @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 MomentSketchAggregatorFactory(
+            fieldName,
+            fieldName,
+            k,
+            compress
+        )
+    );
+  }
+
+  private MomentSketchWrapper deserializeFromByteArray(byte[] bytes)
+  {
+    return MomentSketchWrapper.fromByteArray(bytes);
+  }
+
+  @Override
+  public Object deserialize(Object serializedSketch)
+  {
+    if (serializedSketch instanceof String) {
+      String str = (String) serializedSketch;
+      return 
deserializeFromByteArray(Base64.decodeBase64(str.getBytes(Charsets.UTF_8)));
+    } else if (serializedSketch instanceof byte[]) {
+      return deserializeFromByteArray((byte[]) serializedSketch);
+    } else if (serializedSketch instanceof MomentSketchWrapper) {
+      return serializedSketch;
+    }
+    throw new ISE(
+        "Object is not of a type that can be deserialized to a Moments Sketch"
 
 Review comment:
   Add space in the end of the string

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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