PIG-5288: Improve performance of PigTextRawBytesComparator (rohini)

git-svn-id: https://svn.apache.org/repos/asf/pig/trunk@1804812 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index eb61e8c..10527d2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -24,6 +24,8 @@
  
 IMPROVEMENTS
 
+PIG-5288: Improve performance of PigTextRawBytesComparator (rohini)
+
 PIG-5287: bump jython to 2.7.1 (dbist13 via rohini)
 
 PIG-5264: Remove deprecated keys from PigConfiguration (nkollar via rohini)
diff --git a/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/PigWritableComparators.java b/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/PigWritableComparators.java
index 3dfe199..c7680df 100644
--- a/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/PigWritableComparators.java
+++ b/src/org/apache/pig/backend/hadoop/executionengine/mapReduceLayer/PigWritableComparators.java
@@ -19,6 +19,7 @@
 
 import org.apache.hadoop.io.WritableComparator;
 import org.apache.pig.impl.io.NullablePartitionWritable;
+import org.apache.pig.impl.io.NullableText;
 
 public class PigWritableComparators {
 
@@ -134,6 +135,26 @@
         public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
             return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
         }
+
+        @Override
+        public int compare(Object o1, Object o2) {
+            NullableText nt1 = (NullableText)o1;
+            NullableText nt2 = (NullableText)o2;
+            int rc = 0;
+
+            // If either are null, handle differently.
+            if (!nt1.isNull() && !nt2.isNull()) {
+                rc = nt1.getText().compareTo(nt2.getText());
+            } else {
+                // Two nulls are equal if indices are same
+                if (nt1.isNull() && nt2.isNull()) {
+                    rc = nt1.getIndex() - nt2.getIndex();
+                }
+                else if (nt1.isNull()) rc = -1;
+                else rc = 1;
+            }
+            return rc;
+        }
     }
 
     public static class PigBytesRawBytesComparator extends PigBytesRawComparator {
@@ -146,6 +167,7 @@
         public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
             return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
         }
+
     }
 
     public static class PigTupleSortBytesComparator extends PigTupleSortComparator {
diff --git a/src/org/apache/pig/impl/io/NullableText.java b/src/org/apache/pig/impl/io/NullableText.java
index 3708c94..279a14d 100644
--- a/src/org/apache/pig/impl/io/NullableText.java
+++ b/src/org/apache/pig/impl/io/NullableText.java
@@ -42,7 +42,12 @@
         mValue = new Text(string);
     }
 
+    @Override
     public Object getValueAsPigType() {
         return isNull() ? null : ((Text)mValue).toString();
     }
+
+    public Text getText() {
+        return isNull() ? null : (Text)mValue;
+    }
 }