extended by org.apache.pig.EvalFunc<Double>
      extended by org.apache.pig.builtin.ROUND_TO

public class ROUND_TO
extends EvalFunc<Double>

ROUND_TO safely rounds a number to a given precision by using an intermediate BigDecimal. The too-often seen trick of doing (1000.0 * ROUND(x/1000)) is not only hard to read but also fails to produce numerically accurate results. Given a single data atom and number of digits, it returns a double extending to the given number of decimal places. The result is a multiple of ten to the power given by the digits argument: a negative value zeros out correspondingly many places to the left of the decimal point: ROUND_TO(0.9876543, 3) is 0.988; ROUND_TO(0.9876543, 0) is 1.0; and ROUND_TO(1234.56, -2) is 1200.0. The optional mode argument specifies the rounding mode; by default, HALF_EVEN is used.

Nested Class Summary
Nested classes/interfaces inherited from class org.apache.pig.EvalFunc
Field Summary
Fields inherited from class org.apache.pig.EvalFunc
log, pigLogger, reporter, returnType
Constructor Summary
Method Summary
 Double exec(Tuple input)
          java level API
 List<FuncSpec> getArgToFuncMapping()
          Allow a UDF to specify type specific implementations of itself.
 Schema outputSchema(Schema input)
          Report the schema of the output of this UDF.
Methods inherited from class org.apache.pig.EvalFunc
finish, getCacheFiles, getInputSchema, getLogger, getPigLogger, getReporter, getReturnType, getSchemaName, getSchemaType, isAsynchronous, progress, setInputSchema, setPigLogger, setReporter, setUDFContextSignature, warn
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public ROUND_TO()
Method Detail


public Double exec(Tuple input)
            throws IOException
java level API

Specified by:
exec in class EvalFunc<Double>
input - expects a numeric value to round, a number of digits to keep, and an optional rounding mode.
output returns a single numeric value, the number with only those digits retained


public Schema outputSchema(Schema input)
Description copied from class: EvalFunc
Report the schema of the output of this UDF. Pig will make use of this in error checking, optimization, and planning. The schema of input data to this UDF is provided.

The default implementation interprets the OutputSchema annotation, if one is present. Otherwise, it returns null (no known output schema).

outputSchema in class EvalFunc<Double>
input - Schema of the input
Schema of the output


public List<FuncSpec> getArgToFuncMapping()
                                   throws FrontendException
Description copied from class: EvalFunc
Allow a UDF to specify type specific implementations of itself. For example, an implementation of arithmetic sum might have int and float implementations, since integer arithmetic performs much better than floating point arithmetic. Pig's typechecker will call this method and using the returned list plus the schema of the function's input data, decide which implementation of the UDF to use.

getArgToFuncMapping in class EvalFunc<Double>
A List containing FuncSpec objects representing the EvalFunc class which can handle the inputs corresponding to the schema in the objects. Each FuncSpec should be constructed with a schema that describes the input for that implementation. For example, the sum function above would return two elements in its list:
  1. FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.DOUBLE)))
  2. FuncSpec(IntSum.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.INTEGER)))
This would indicate that the main implementation is used for doubles, and the special implementation IntSum is used for ints.

Copyright © 2007-2012 The Apache Software Foundation