/*
 * Copyright 2016, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.sketches.hive.theta;

import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator.AbstractAggregationBuffer;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;

import com.yahoo.memory.Memory;
import com.yahoo.sketches.theta.SetOperation;
import com.yahoo.sketches.theta.Sketch;
import com.yahoo.sketches.theta.Union;

class UnionState extends AbstractAggregationBuffer {

  static final float DEFAULT_SAMPLING_PROBABILITY = 1;

  private int nominalEntries_;
  private long seed_;
  private Union union_;

  public boolean isInitialized() {
    return union_ != null;
  }

  // sampling probability is not relevant for merging
  public void init(final int nominalEntries, final long seed) {
    init(nominalEntries, UnionState.DEFAULT_SAMPLING_PROBABILITY, seed);
  }

  public void init(final int nominalEntries, final float samplingProbability, final long seed) {
    this.nominalEntries_ = nominalEntries;
    this.seed_ = seed;
    union_ = SetOperation.builder().setNominalEntries(nominalEntries).setP(samplingProbability)
        .setSeed(seed).buildUnion();
  }

  public int getNominalEntries() {
    return nominalEntries_;
  }

  public long getSeed() {
    return seed_;
  }

  public void update(final Memory mem) {
    union_.update(mem);
  }

  public void update(final Object value, final PrimitiveObjectInspector objectInspector) {
    switch (objectInspector.getPrimitiveCategory()) {
    case BINARY:
      union_.update(PrimitiveObjectInspectorUtils.getBinary(value, objectInspector).getBytes());
      return;
    case BYTE:
      union_.update(PrimitiveObjectInspectorUtils.getByte(value, objectInspector));
      return;
    case DOUBLE:
      union_.update(PrimitiveObjectInspectorUtils.getDouble(value, objectInspector));
      return;
    case FLOAT:
      union_.update(PrimitiveObjectInspectorUtils.getFloat(value, objectInspector));
      return;
    case INT:
      union_.update(PrimitiveObjectInspectorUtils.getInt(value, objectInspector));
      return;
    case LONG:
      union_.update(PrimitiveObjectInspectorUtils.getLong(value, objectInspector));
      return;
    case STRING:
      union_.update(PrimitiveObjectInspectorUtils.getString(value, objectInspector));
      return;
    default:
      throw new IllegalArgumentException(
          "Unrecongnized input data type. Please use data of type: "
      + "byte, double, float, int, long, or string only.");
    }
  }

  public Sketch getResult() {
    if (union_ == null) { return null; }
    return union_.getResult();
  }

  public void reset() {
    union_ = null;
  }

}

@@ -2494,24 +2494,314 @@
     return;%0A
+    case CHAR:%0A      union_.update(PrimitiveObjectInspectorUtils.getHiveChar(value, objectInspector).getValue().toCharArray());%0A      return;%0A    case VARCHAR:%0A      union_.update(PrimitiveObjectInspectorUtils.getHiveVarchar(value, objectInspector).getValue().toCharArray());%0A      return;%0A
     default:
@@ -2851,18 +2851,16 @@
         
-  
 %22Unrecon
@@ -2885,11 +2885,116 @@
 type
-. P
+ %22 + value.getClass().getSimpleName() + %22 category %22%0A        + objectInspector.getPrimitiveCategory() + %22, p
 leas
@@ -3011,16 +3011,33 @@
  of 
+the following 
 type
+s
 : %22%0A
+  
     
@@ -3072,16 +3072,30 @@
 t, long,
+ char, varchar
  or stri
@@ -3100,13 +3100,8 @@
 ring
- only
 .%22);

support char and varchar types