[ 
https://issues.apache.org/jira/browse/PIG-3609?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13852224#comment-13852224
 ] 

Cheolsoo Park commented on PIG-3609:
------------------------------------

[~rding], I don't think the following is correct-
{code}
+      AvroBagWrapper bOther = (AvroBagWrapper) o;
+      if (this.size() != bOther.size()) {
+          if (this.size() > bOther.size()) return 1;
+          else return -1;
+      }
+      return GenericData.get().compare(theArray, bOther.theArray, 
theArray.getSchema());
{code}
When comparing arrays, we should compare entries one by one before comparing 
the size of arrays, shouldn't we? At least, that's how GenericData#compare() 
works for arrays-
{code}
    case ARRAY:
      Collection a1 = (Collection)o1;
      Collection a2 = (Collection)o2;
      Iterator e1 = a1.iterator();
      Iterator e2 = a2.iterator();
      Schema elementType = s.getElementType();
      while(e1.hasNext() && e2.hasNext()) {
        int compare = compare(e1.next(), e2.next(), elementType, equals);
        if (compare != 0) return compare;
      }
      return e1.hasNext() ? 1 : (e2.hasNext() ? -1 : 0);
{code}
I think we should just call GenericData#compare() w/o comparing the size.

In addition, I found your test case a bit confusing. Since getExpect() returns 
a set which doesn't guarantee any ordering, it's not clear how arrays are 
mapped to bw0, bw1, and bw2. That makes it hard to reason about the assertions.

> ClassCastException when calling compareTo method on AvroBagWrapper 
> -------------------------------------------------------------------
>
>                 Key: PIG-3609
>                 URL: https://issues.apache.org/jira/browse/PIG-3609
>             Project: Pig
>          Issue Type: Bug
>          Components: impl
>    Affects Versions: 0.12.0
>            Reporter: Richard Ding
>            Assignee: Richard Ding
>            Priority: Minor
>         Attachments: PIG-3609.patch, PIG-3609_2.patch
>
>
> One got the following exception when calling compareTo method on 
> AvroBagWrapper with an AvroBagWrapper object:
> {code}
> java.lang.ClassCastException: org.apache.pig.impl.util.avro.AvroBagWrapper 
> incompatible with java.util.Collection
>         at org.apache.avro.generic.GenericData.compare(GenericData.java:786)
>         at org.apache.avro.generic.GenericData.compare(GenericData.java:760)
>         at 
> org.apache.pig.impl.util.avro.AvroBagWrapper.compareTo(AvroBagWrapper.java:78)
> {code}
> Looking at the code, it compares objects with different types:
> {code}
> return GenericData.get().compare(theArray, o, theArray.getSchema());
> {code}



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)

Reply via email to