| /* |
| * 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.datasketches.hive.cpc; |
| |
| import org.apache.datasketches.cpc.CpcSketch; |
| import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; |
| import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils; |
| |
| class SketchState extends State { |
| |
| private CpcSketch sketch_; |
| |
| @Override |
| boolean isInitialized() { |
| return sketch_ != null; |
| } |
| |
| @Override |
| void init(final int logK, final long seed) { |
| super.init(logK, seed); |
| sketch_ = new CpcSketch(logK, seed); |
| } |
| |
| void update(final Object data, final PrimitiveObjectInspector objectInspector) { |
| switch (objectInspector.getPrimitiveCategory()) { |
| case BINARY: |
| sketch_.update(PrimitiveObjectInspectorUtils.getBinary(data, objectInspector) |
| .copyBytes()); |
| return; |
| case BYTE: |
| sketch_.update(PrimitiveObjectInspectorUtils.getByte(data, objectInspector)); |
| return; |
| case DOUBLE: |
| sketch_.update(PrimitiveObjectInspectorUtils.getDouble(data, objectInspector)); |
| return; |
| case FLOAT: |
| sketch_.update(PrimitiveObjectInspectorUtils.getFloat(data, objectInspector)); |
| return; |
| case INT: |
| sketch_.update(PrimitiveObjectInspectorUtils.getInt(data, objectInspector)); |
| return; |
| case LONG: |
| sketch_.update(PrimitiveObjectInspectorUtils.getLong(data, objectInspector)); |
| return; |
| case STRING: |
| // conversion to char[] avoids costly UTF-8 encoding |
| sketch_.update(PrimitiveObjectInspectorUtils.getString(data, objectInspector) |
| .toCharArray()); |
| return; |
| case CHAR: |
| sketch_.update(PrimitiveObjectInspectorUtils.getHiveChar(data, objectInspector) |
| .getValue().toCharArray()); |
| return; |
| case VARCHAR: |
| sketch_.update(PrimitiveObjectInspectorUtils.getHiveVarchar(data, objectInspector) |
| .getValue().toCharArray()); |
| return; |
| default: |
| throw new IllegalArgumentException( |
| "Unrecongnized input data type " + data.getClass().getSimpleName() + " category " |
| + objectInspector.getPrimitiveCategory() + ", please use data of the following types: " |
| + "byte, double, float, int, long, char, varchar or string."); |
| } |
| } |
| |
| @Override |
| CpcSketch getResult() { |
| if (sketch_ == null) { return null; } |
| return sketch_; |
| } |
| |
| @Override |
| void reset() { |
| sketch_ = null; |
| } |
| |
| } |