DaimonPl commented on a change in pull request #5712: HllSketch module
URL: https://github.com/apache/incubator-druid/pull/5712#discussion_r218184150
 
 

 ##########
 File path: 
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchAggregatorFactory.java
 ##########
 @@ -0,0 +1,232 @@
+/*
+ * 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.datasketches.hll;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+
+import javax.annotation.Nullable;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.yahoo.sketches.hll.HllSketch;
+import com.yahoo.sketches.hll.TgtHllType;
+import com.yahoo.sketches.hll.Union;
+
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.query.aggregation.AggregateCombiner;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import org.apache.druid.query.aggregation.ObjectAggregateCombiner;
+import org.apache.druid.query.cache.CacheKeyBuilder;
+import org.apache.druid.segment.ColumnValueSelector;
+
+/**
+ * Base class for both build and merge factories
+ * @author Alexander Saydakov
+ */
+abstract class HllSketchAggregatorFactory extends AggregatorFactory
+{
+
+  static final int DEFAULT_LG_K = 12;
+  static final TgtHllType DEFAULT_TGT_HLL_TYPE = TgtHllType.HLL_4;
+
+  static final Comparator<HllSketch> COMPARATOR = 
Comparator.nullsFirst(Comparator.comparingDouble(HllSketch::getEstimate));
+
+  private final String name;
+  private final String fieldName;
+  private final int lgK;
+  private final TgtHllType tgtHllType;
+
+  HllSketchAggregatorFactory(final String name, final String fieldName, 
@Nullable final Integer lgK, @Nullable final String tgtHllType)
+  {
+    if (name == null) {
+      throw new IAE("Aggregator name must be specified");
+    }
+    this.name = name;
+    if (fieldName == null) {
+      throw new IAE("Parameter fieldName must be specified");
+    }
+    this.fieldName = fieldName;
+    this.lgK = lgK == null ? DEFAULT_LG_K : lgK;
+    this.tgtHllType = tgtHllType == null ? DEFAULT_TGT_HLL_TYPE : 
TgtHllType.valueOf(tgtHllType);
+  }
+
+  @Override
+  @JsonProperty
+  public String getName()
+  {
+    return name;
+  }
+
+  @JsonProperty
+  public String getFieldName()
+  {
+    return fieldName;
+  }
+
+  @JsonProperty
+  public int getLgK()
+  {
+    return lgK;
+  }
+
+  @JsonProperty
+  public String getTgtHllType()
+  {
+    return tgtHllType.toString();
+  }
+
+  @Override
+  public List<String> requiredFields()
+  {
+    return Arrays.asList(fieldName);
+  }
+
+  // This is a convoluted way to return a list of input field names this 
aggregator needs.
+  // Currently the returned factories are only used to obtain a field name by 
calling getName() method.
+  @Override
+  public List<AggregatorFactory> getRequiredColumns()
+  {
+    return Arrays.asList(new HllSketchBuildAggregatorFactory(fieldName, 
fieldName, lgK, tgtHllType.toString()));
+  }
+
+  @Override
+  public HllSketch deserialize(final Object object)
+  {
+    return HllSketchMergeComplexMetricSerde.deserializeSketch(object);
+  }
+
+  @Override
+  public HllSketch combine(final Object objectA, final Object objectB)
+  {
+    final Union union = new Union(lgK);
+    union.update((HllSketch) objectA);
+    union.update((HllSketch) objectA);
 
 Review comment:
   Hmm maybe there should be test scenario which checks that flow? It would be 
quite critical bug :)

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to