Search code examples
hadoopapache-pig

Pig sum fails with +ve and -ve values


I have below data

primary,first,second
1,393440.09,354096.08
1,4410533.33,3969479.99
1,-4803973.41,-4323576.07

I have to aggregate and sum first and second column. Below is the script I am executing

   data_load= load <filelocation> using org.apache.pig.piggybank.storage.CSVExcelStorage(',', 'NO_MULTILINE', 'NOCHANGE', 'SKIP_INPUT_HEADER') As  (primary:double, first:double,second:double)

   dataAgrr = group data_load by primary;

   sumData = FOREACH dataAgrr GENERATE 
   group as data,
   SUM(data_load.first) as first,
   SUM(data_load.second) as second,
   SUM(data_load.primary) as primary;

After executing, below Output is produced:

(1.0,0.009999999951105565,-5.820766091346741E-11,3.0)

But when we manually adding second column (354096.08, 3969479.99, -4323576.07) gives 0.


Solution

  • Pig uses Java "double" internally. Testing with a sample code below

    import java.math.BigDecimal;
    public class TestSum  {
      public static void main(String[] args) {
        double d1 = 354096.08;
        double d2 = 3969479.99;
        double d3 = -4323576.07;
        System.err.println("Total in double is " + ((d3 + d2 ) + d1));
    
        BigDecimal bd1 = new BigDecimal("354096.08");
        BigDecimal bd2 = new BigDecimal("3969479.99");
        BigDecimal bd3 = new BigDecimal("-4323576.07");
        System.err.println("Total in BigDecimal is " + bd3.add(bd2).add(bd1));
    
      }
    }
    

    This produces

    Total in double is -5.820766091346741E-11
    Total in BigDecimal is 0.00
    

    If you need a better precision, you may want to try using "bigdecimal" instead of "double" in your script.