/*
 * 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 com.fasterxml.jackson.annotation.JsonCreator;
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.query.aggregation.Aggregator;
import org.apache.druid.query.aggregation.AggregatorUtil;
import org.apache.druid.query.aggregation.BufferAggregator;
import org.apache.druid.segment.ColumnSelectorFactory;
import org.apache.druid.segment.ColumnValueSelector;

import javax.annotation.Nullable;

/**
 * This aggregator factory is for merging existing sketches.
 * The input column must contain {@link HllSketch}
 * @author Alexander Saydakov
 */
public class HllSketchMergeAggregatorFactory extends HllSketchAggregatorFactory
{

  @JsonCreator
  public HllSketchMergeAggregatorFactory(
      @JsonProperty("name") final String name,
      @JsonProperty("fieldName") final String fieldName,
      @JsonProperty("lgK") @Nullable final Integer lgK,
      @JsonProperty("tgtHllType") @Nullable final String tgtHllType
  )
  {
    super(name, fieldName, lgK, tgtHllType);
  }

  @Override
  public String getTypeName()
  {
    return HllSketchModule.MERGE_TYPE_NAME;
  }

  @Override
  protected byte getCacheTypeId()
  {
    return AggregatorUtil.HLL_SKETCH_MERGE_CACHE_TYPE_ID;
  }

  @Override
  public Aggregator factorize(final ColumnSelectorFactory columnSelectorFactory)
  {
    final ColumnValueSelector<HllSketch> selector = columnSelectorFactory.makeColumnValueSelector(getFieldName());
    return new HllSketchMergeAggregator(selector, getLgK(), TgtHllType.valueOf(getTgtHllType()));
  }

  @Override
  public BufferAggregator factorizeBuffered(final ColumnSelectorFactory columnSelectorFactory)
  {
    final ColumnValueSelector<HllSketch> selector = columnSelectorFactory.makeColumnValueSelector(getFieldName());
    return new HllSketchMergeBufferAggregator(
        selector,
        getLgK(),
        TgtHllType.valueOf(getTgtHllType()),
        getMaxIntermediateSize()
    );
  }

  @Override
  public int getMaxIntermediateSize()
  {
    return Union.getMaxSerializationBytes(getLgK());
  }

}

@@ -1137,32 +1137,175 @@
 ion.Aggregator;%0A
+import org.apache.druid.query.aggregation.AggregatorFactory;%0Aimport org.apache.druid.query.aggregation.AggregatorFactoryNotMergeableException;%0A
 import org.apach
@@ -2118,32 +2118,739 @@
 tHllType);%0A  %7D%0A%0A
+  @Override%0A  public AggregatorFactory getMergingFactory(AggregatorFactory other) throws AggregatorFactoryNotMergeableException%0A  %7B%0A    if (other.getName().equals(this.getName()) && other instanceof HllSketchMergeAggregatorFactory) %7B%0A      HllSketchMergeAggregatorFactory castedOther = (HllSketchMergeAggregatorFactory) other;%0A%0A      return new HllSketchMergeAggregatorFactory(%0A              getName(),%0A              getName(),%0A              Math.max(getLgK(), castedOther.getLgK()),%0A              getTgtHllType().compareTo(castedOther.getTgtHllType()) %3C 0 ? castedOther.getTgtHllType() : getTgtHllType()%0A      );%0A    %7D else %7B%0A      throw new AggregatorFactoryNotMergeableException(this, other);%0A    %7D%0A  %7D%0A%0A
   @Override%0A  pu

Fix aggregatorFactory meta merge exception (#7504)