Java Code Examples for org.apache.pig.data.DataType#toDouble()

The following examples show how to use org.apache.pig.data.DataType#toDouble() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ROUND_TO.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a numeric value to round, a number of digits to keep, and an optional rounding mode.
 * @return output returns a single numeric value, the number with only those digits retained
 */
@Override
public Double exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2)
        return null;

    try {
        Double       num    = DataType.toDouble(input.get(0));
        Integer      digits = DataType.toInteger(input.get(1));
        RoundingMode mode   = (input.size() >= 3) ?
            RoundingMode.valueOf(DataType.toInteger(input.get(2))) : RoundingMode.HALF_EVEN;
        if (num == null) return null;

        BigDecimal bdnum  = BigDecimal.valueOf(num);
        bdnum = bdnum.setScale(digits, mode);
        return bdnum.doubleValue();
    } catch (NumberFormatException nfe){
        System.err.println("Failed to process input; error - " + nfe.getMessage());
        return null;
    } catch (Exception e){
        throw new IOException("Caught exception processing input row ", e);
    }
}
 
Example 2
Source File: nextAfter.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a tuple containing two numeric DataAtom value
 * @param output returns a single numeric DataAtom value, which is 
 * the floating-point number adjacent to the first argument in the 
 * direction of the second argument.
 */
@Override
public Double exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2) {
        return null;
    }
       if (input.get(0) == null || input.get(1) == null) {
           return null;
       }
	try{
		double first = DataType.toDouble(input.get(0));
		double second = DataType.toDouble(input.get(1));
		return Math.nextAfter(first, second);
       } catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
	} catch(Exception e){
           throw new IOException("Caught exception in nextAfter", e);
	}
}
 
Example 3
Source File: MAX.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a tuple containing two numeric DataAtom value
 * @param output returns a single numeric value, which is
 * the the larger value among the two.
 */
@Override
public Double exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2)
        return null;

    try{
        Double first = DataType.toDouble(input.get(0));
        Double second  = DataType.toDouble(input.get(1));
        if (first==null)
            return second;
        if (second==null)
            return first;
        return Math.max(first, second);
    } catch (NumberFormatException nfe){
        System.err.println("Failed to process input; error - " + nfe.getMessage());
        return null;
    } catch(Exception e){
        throw new IOException("Caught exception in MAX.Initial", e);
    }

}
 
Example 4
Source File: ROUND.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric value
 * @param output returns a single numeric value, 
 * the closest long to the argument
 */
@Override
public Long exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       try{
           Double d =  DataType.toDouble(input.get(0));
	    return Math.round(d);
       } catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch (Exception e){
           throw new IOException("Caught exception processing input row ", e);
       }
}
 
Example 5
Source File: MIN.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a tuple containing two numeric DataAtom value
 * @param output returns a single numeric value, which is
 * the the smaller value among the two.
 */
@Override
public Double exec(Tuple input) throws IOException {
    if (input == null || input.size() < 2)
        return null;

    try{
        Double first = DataType.toDouble(input.get(0));
        Double second  = DataType.toDouble(input.get(1));
        if (first==null)
            return first;
        if (second==null)
            return second;
        return Math.min(first, second);
    } catch (NumberFormatException nfe){
        System.err.println("Failed to process input; error - " + nfe.getMessage());
        return null;
    } catch(Exception e){
        throw new IOException("Caught exception in MIN.Initial", e);
    }

}
 
Example 6
Source File: NEXTUP.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
     * java level API
     * @param input expects a single numeric value
     * @param output returns a single numeric value, nextup value of the argument
     */
    public Double exec(Tuple input) throws IOException {
        if (input == null || input.size() == 0 || input.get(0) == null)
            return null;
        Double d;
        try{
           d = DataType.toDouble(input.get(0));
        } catch (NumberFormatException nfe){
            System.err.println("Failed to process input; error - " + nfe.getMessage());
            return null;
        } catch (Exception e){
            throw new IOException("Caught exception processing input row ", e);
        }

		return Math.nextUp(d);
}
 
Example 7
Source File: SIGNUM.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric DataAtom value
 * @param output returns a single numeric DataAtom value, 
 * signum function of the argument
 */
@Override
public Double exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       try{
	    Double d = DataType.toDouble(input.get(0));
	    return Math.signum(d);
	}catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       }catch (Exception e){
           throw new IOException("Caught exception processing input row ", e);
       }
	
}
 
Example 8
Source File: copySign.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a tuple containing two numeric DataAtom value
 * @param output returns a single numeric DataAtom value, which is 
 * first floating-point argument with the sign of the second 
 * floating-point argument.
 */
@Override
public Double exec(Tuple input) throws IOException {
       if (input == null || input.size() < 2)
           return null;
       if (input.get(0) == null || input.get(1) == null) {
           return null;
       }
	try{
		double first =  DataType.toDouble(input.get(0));
		double second = DataType.toDouble(input.get(1));
		return Math.copySign(first, second);
	} catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch(Exception e){
           throw new IOException("Caught exception processing input row ", e);
	}
}
 
Example 9
Source File: SCALB.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a tuple containing two numeric DataAtom value
 * @param output returns a single numeric DataAtom value, which is 
 * fistArgument pow(2,secondArgument) rounded as if performed by a 
 * single correctly rounded floating-point multiply to a member of 
 * the double value set.
 */
public Double exec(Tuple input) throws IOException {
       if (input == null || input.size() < 2)
           return null;
       if (input.get(0) == null || input.get(1) == null) {
           return null;
       }
	try{
		Double first = DataType.toDouble(input.get(0));
		Integer second = DataType.toInteger(input.get(1));

           return Math.scalb(first, second);
	} catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch(Exception e){
           throw new IOException("Caught exception in MAX.Initial", e);
       }
}
 
Example 10
Source File: ROUND.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric value
 * @return output returns a single numeric value, 
 * the closest long to the argument
 */
@Override
public Long exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       try{
           Double d =  DataType.toDouble(input.get(0));
	    return Math.round(d);
       } catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch (Exception e){
           throw new IOException("Caught exception processing input row ", e);
       }
}
 
Example 11
Source File: ABS.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric value
 * @return output returns a single numeric value, absolute value of the argument
 */
public Double exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       Double d;
       try{
           d = DataType.toDouble(input.get(0));
       } catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch (Exception e){
           throw new IOException("Caught exception processing input row ", e);
       }

	return Math.abs(d);
}
 
Example 12
Source File: TestLocal.java    From spork with Apache License 2.0 5 votes vote down vote up
public Double bigGroupAll( File tmpFile ) throws Throwable {

        String query = "foreach (group (load '"
                + Util.generateURI(tmpFile.toString(), pig.getPigContext())
                + "') all) generate " + COUNT.class.getName() + "($1) ;";
        System.out.println(query);
        pig.registerQuery("asdf_id = " + query);
        Iterator<Tuple> it = pig.openIterator("asdf_id");
        Tuple t = it.next();

        return  DataType.toDouble(t.get(0));
    }
 
Example 13
Source File: TestInfixArithmetic.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testDivide() throws Throwable {
    File tmpFile = File.createTempFile("test", "txt");
    for (int i = 0; i < nullFlags.length; i++) {
        System.err.println("Testing with nulls: " + nullFlags[i]);
        PrintStream ps = new PrintStream(new FileOutputStream(tmpFile));
        generateInput(ps, nullFlags[i]);
        String query = "A = foreach (load '"
                + Util.encodeEscape(Util.generateURI(tmpFile.toString(), pig.getPigContext()))
                + "' using " + PigStorage.class.getName()
                + "(':')) generate $0, $0 / $1, $1 ;";
        log.info(query);
        pig.registerQuery(query);
        Iterator<Tuple> it = pig.openIterator("A");
        tmpFile.delete();
        while(it.hasNext()) {
            Tuple t = it.next();
            Double first = (t.get(0) == null ? null :DataType.toDouble(t.get(0)));
            Double second = (t.get(1) == null ? null :DataType.toDouble(t.get(1)));
            Double third = (t.get(2) == null ? null :DataType.toDouble(t.get(2)));
            if(first != null && third != null) {
                assertEquals(Double.valueOf(1.0), second);
            } else {
                assertNull(second);
            }
        }
    }
}
 
Example 14
Source File: TestInfixArithmetic.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiply() throws Throwable {
    File tmpFile = File.createTempFile("test", "txt");
    for (int i = 0; i < nullFlags.length; i++) {
        System.err.println("Testing with nulls: " + nullFlags[i]);
        PrintStream ps = new PrintStream(new FileOutputStream(tmpFile));
        generateInput(ps, nullFlags[i]);
        String query = "A = foreach (load '"
                + Util.encodeEscape(Util.generateURI(tmpFile.toString(), pig.getPigContext()))
                + "' using " + PigStorage.class.getName()
                + "(':')) generate $0, $0 * $1, $1 ;";
        log.info(query);
        pig.registerQuery(query);
        Iterator<Tuple> it = pig.openIterator("A");
        tmpFile.delete();
        while(it.hasNext()) {
            Tuple t = it.next();
            Double first = (t.get(0) == null ? null :DataType.toDouble(t.get(0)));
            Double second = (t.get(1) == null ? null :DataType.toDouble(t.get(1)));
            Double third = (t.get(2) == null ? null :DataType.toDouble(t.get(2)));
            if(first != null && third != null) {
                assertEquals(Double.valueOf(first * first), second);
            } else {
                assertNull(second);
            }
        }
    }
}
 
Example 15
Source File: TestInfixArithmetic.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubtract() throws Throwable {
    File tmpFile = File.createTempFile("test", "txt");
    for (int i = 0; i < nullFlags.length; i++) {
        System.err.println("Testing with nulls: " + nullFlags[i]);
        PrintStream ps = new PrintStream(new FileOutputStream(tmpFile));
        generateInput(ps, nullFlags[i]);
        String query = "A = foreach (load '"
                + Util.encodeEscape(Util.generateURI(tmpFile.toString(), pig.getPigContext()))
                + "' using " + PigStorage.class.getName()
                + "(':')) generate $0, $0 - $1, $1 ;";
        log.info(query);
        pig.registerQuery(query);
        Iterator<Tuple> it = pig.openIterator("A");
        tmpFile.delete();
        while(it.hasNext()) {
            Tuple t = it.next();
            Double first = (t.get(0) == null ? null :DataType.toDouble(t.get(0)));
            Double second = (t.get(1) == null ? null :DataType.toDouble(t.get(1)));
            Double third = (t.get(2) == null ? null :DataType.toDouble(t.get(2)));
            if(first != null && third != null) {
                assertEquals(Double.valueOf(0.0), second);
            } else {
                assertNull(second);
            }
        }
    }
}
 
Example 16
Source File: ULP.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric value
 * @param output returns a single numeric value, 
 * the size of an ulp of the argument.
 */
public Double exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       try{
           Double d = DataType.toDouble(input.get(0));
	    return Math.ulp(d);
       }catch(NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch(Exception e){
           throw new IOException("Caught exception in ULP.", e);
       }
   }
 
Example 17
Source File: TestInfixArithmetic.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() throws Throwable {
    File tmpFile = File.createTempFile("test", "txt");

    for (int i = 0; i < nullFlags.length; i++) {
        System.err.println("Testing with nulls: " + nullFlags[i]);
        PrintStream ps = new PrintStream(new FileOutputStream(tmpFile));
        generateInput(ps, nullFlags[i]);
        String query = "A = foreach (load '"
                + Util.encodeEscape(Util.generateURI(tmpFile.toString(), pig.getPigContext()))
                + "' using " + PigStorage.class.getName()
                + "(':')) generate $0, $0 + $1, $1;";
        log.info(query);
        pig.registerQuery(query);
        Iterator<Tuple> it = pig.openIterator("A");
        tmpFile.delete();
        while(it.hasNext()) {
            Tuple t = it.next();
            Double first = (t.get(0) == null ? null :DataType.toDouble(t.get(0)));
            Double second = (t.get(1) == null ? null :DataType.toDouble(t.get(1)));
            Double third = (t.get(2) == null ? null :DataType.toDouble(t.get(2)));
            if(first != null && third != null) {
                assertEquals(Double.valueOf(first + first), second);
            } else {
                assertNull(second);
            }
        }
    }
}
 
Example 18
Source File: getExponent.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * java level API
 * @param input expects a single numeric value
 * @param output returns a single numeric value, unbiased 
 * exponent used in the representation of a double
 */
public Integer exec(Tuple input) throws IOException {
       if (input == null || input.size() == 0 || input.get(0) == null)
           return null;

       try {
           Double d =  DataType.toDouble(input.get(0));
	    return Math.getExponent(d);
       } catch (NumberFormatException nfe){
           System.err.println("Failed to process input; error - " + nfe.getMessage());
           return null;
       } catch (Exception e){
           throw new IOException("Caught exception processing input row ", e);
       }
}
 
Example 19
Source File: TestMapReduce.java    From spork with Apache License 2.0 5 votes vote down vote up
public Double bigGroupAll( File tmpFile ) throws Throwable {

        String query = "foreach (group (load '"
                + Util.generateURI(tmpFile.toString(), pig.getPigContext())
                + "') all) generate " + COUNT.class.getName() + "($1) ;";
        System.out.println(query);
        pig.registerQuery("asdf_id = " + query);
        Iterator<Tuple> it = pig.openIterator("asdf_id");
        Tuple t = it.next();

        return  DataType.toDouble(t.get(0));
    }
 
Example 20
Source File: Coalesce.java    From datafu with Apache License 2.0 4 votes vote down vote up
@Override
public Object exec(Tuple input) throws IOException
{    
  if (input == null || input.size() == 0)
  {
    return null;
  }
  
  Byte type = (Byte)getInstanceProperties().get("type");
              
  for (Object o : input)
  {
    if (o != null)
    {
      if (strict)
      {
        return o;
      }
      else
      {
        try
        {
          switch (type)
          {
          case DataType.INTEGER:
            return DataType.toInteger(o);
          case DataType.LONG:
            return DataType.toLong(o);
          case DataType.DOUBLE:
            return DataType.toDouble(o); 
          case DataType.FLOAT:
            return DataType.toFloat(o);      
          default:
            return o;
          }
        }
        catch (Exception e)
        {
          DataFuException dfe = new DataFuException(e.getMessage(),e);
          dfe.setData(o);
          dfe.setFieldAliases(getFieldAliases());
          throw dfe;
        }
      }
    }
  }
  
  return null;
}