elephant-bird library generates default values of fields instead of null when an optional field is not set

177 Views Asked by At

I am currently using version 4.4 of elephant-bird-pig library. If I try to make a tuple from a thrift object, I expect that fields that were not set in the object, are marked null in tuple. However instead default values are put into the tuple. E.g.

struct PropValueUnion {
    1: optional i32 intValue,
    2: optional i64 longValue,
    3: optional string stringValue,
    4: optional double doubleValue,
    5: optional bool flag
} 

the output of following should be (null,null,abc,null,null)

PropValueUnion value = new PropValueUnion();
a.setStringValue("abc");
System.out.println(ThriftToPig.newInstance(PropvalueUnion.class).getPigTuple(value));

Actual: (0,0,abc,0.0,0)

The problem is that isset information for fields is being lost during the conversion to tuple. Was it done deliberately and is there any workaround for this problem?

1

There are 1 best solutions below

2
On

I expect that fields that were not set in the object, are marked null in tuple. However instead default values are put into the tuple. E

That's right. Whether or not an optional field is set can be detected by checking the isset flags.

public class PropValueUnion implements org.apache.thrift.TBase<PropValueUnion, PropValueUnion._Fields>, java.io.Serializable, Cloneable, Comparable<PropValueUnion> {

  // ... lots of other code omitted ...

  // isset id assignments
  private static final int __INTVALUE_ISSET_ID = 0;
  private static final int __LONGVALUE_ISSET_ID = 1;
  private static final int __DOUBLEVALUE_ISSET_ID = 2;
  private static final int __FLAG_ISSET_ID = 3;
  private byte __isset_bitfield = 0;

  // ... lots of other code omitted ...

  /** Returns true if field doubleValue is set (has been assigned a value) and false otherwise */
  public boolean isSetDoubleValue() {
    return EncodingUtils.testBit(__isset_bitfield, __DOUBLEVALUE_ISSET_ID);
  }

  public void setDoubleValueIsSet(boolean value) {
    __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __DOUBLEVALUE_ISSET_ID, value);
  }

  // ... even more code omitted ...

}