Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.hadoop.hive.ql.udf.generic;
 
 
 import  org.apache.hadoop.hive.ql.exec.Description;
 import  org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import  org.apache.hadoop.hive.ql.metadata.HiveException;
 import  org.apache.hadoop.hive.ql.parse.SemanticException;
 import  org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import  org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
 import  org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 import  org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
 import  org.apache.hadoop.io.LongWritable;
 
This class implements the COUNT aggregation function as in SQL.
 
 @Description(name = "count", value = "_FUNC_(*) - Returns the total number of retrieved rows, including "
         + "rows containing NULL values.\n"
 
         + "_FUNC_(expr) - Returns the number of rows for which the supplied " + "expression is non-NULL.\n"
 
         + "_FUNC_(DISTINCT expr[, expr...]) - Returns the number of rows for "
         + "which the supplied expression(s) are unique and non-NULL.")
 public class GenericUDAFCount implements GenericUDAFResolver2 {
 
     @Override
     public GenericUDAFEvaluator getEvaluator(TypeInfo[] parametersthrows SemanticException {
         // This method implementation is preserved for backward compatibility.
         return new GenericUDAFCountEvaluator();
     }
 
     @Override
     public GenericUDAFEvaluator getEvaluator(GenericUDAFParameterInfo paramInfothrows SemanticException {
 
         TypeInfo[] parameters = paramInfo.getParameters();
 
         if (parameters.length == 0) {
             if (!paramInfo.isAllColumns()) {
                 throw new UDFArgumentException("Argument expected");
             }
             assert !paramInfo.isDistinct() : "DISTINCT not supported with *";
         } else {
             if (parameters.length > 1 && !paramInfo.isDistinct()) {
                 throw new UDFArgumentException("DISTINCT keyword must be specified");
             }
             assert !paramInfo.isAllColumns() : "* not supported in expression list";
         }
 
         return new GenericUDAFCountEvaluator().setCountAllColumns(paramInfo.isAllColumns());
     }

    
GenericUDAFCountEvaluator.
 
     public static class GenericUDAFCountEvaluator extends GenericUDAFEvaluator {
         private boolean countAllColumns = false;
         private LongObjectInspector partialCountAggOI;
         private LongWritable result;
 
         @Override
         public ObjectInspector init(Mode m, ObjectInspector[] parametersthrows HiveException {
             super.init(mparameters);
              = PrimitiveObjectInspectorFactory.writableLongObjectInspector;
              = new LongWritable(0);
             return PrimitiveObjectInspectorFactory.writableLongObjectInspector;
         }
 
         private GenericUDAFCountEvaluator setCountAllColumns(boolean countAllCols) {
              = countAllCols;
             return this;
         }

        
class for storing count value.
 
         static class CountAgg implements SerializableBuffer {
             long value;
 
             @Override
            public void deSerializeAggBuffer(byte[] dataint startint len) {
                 = BufferSerDeUtil.getLong(datastart);
            }
            @Override
            public void serializeAggBuffer(byte[] dataint startint len) {
                BufferSerDeUtil.writeLong(datastart);
            }
            @Override
            public void serializeAggBuffer(DataOutput outputthrows IOException {
                output.writeLong();
            }
        }
        @Override
        public AggregationBuffer getNewAggregationBuffer() throws HiveException {
            CountAgg buffer = new CountAgg();
            reset(buffer);
            return buffer;
        }
        @Override
        public void reset(AggregationBuffer aggthrows HiveException {
            ((CountAggagg). = 0;
        }
        @Override
        public void iterate(AggregationBuffer aggObject[] parametersthrows HiveException {
            // parameters == null means the input table/split is empty
            if (parameters == null) {
                return;
            }
            if () {
                assert parameters.length == 0;
                ((CountAggagg).++;
            } else {
                assert parameters.length > 0;
                boolean countThisRow = true;
                for (Object nextParam : parameters) {
                    if (nextParam == null) {
                        countThisRow = false;
                        break;
                    }
                }
                if (countThisRow) {
                    ((CountAggagg).++;
                }
            }
        }
        @Override
        public void merge(AggregationBuffer aggObject partialthrows HiveException {
            if (partial != null) {
                long p = .get(partial);
                ((CountAggagg). += p;
            }
        }
        @Override
        public Object terminate(AggregationBuffer aggthrows HiveException {
            .set(((CountAggagg).);
            return ;
        }
        @Override
        public Object terminatePartial(AggregationBuffer aggthrows HiveException {
            return terminate(agg);
        }
    }
New to GrepCode? Check out our FAQ X