Author: gates
Date: Wed Apr  8 21:59:31 2009
New Revision: 763421

URL: http://svn.apache.org/viewvc?rev=763421&view=rev
Log:
PIG-745: Add DataType.toString() to force basic types to chararray, useful for 
UDFs that want to handle all simple types as strings.

Modified:
    hadoop/pig/trunk/CHANGES.txt
    hadoop/pig/trunk/src/org/apache/pig/data/DataType.java

Modified: hadoop/pig/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/CHANGES.txt?rev=763421&r1=763420&r2=763421&view=diff
==============================================================================
--- hadoop/pig/trunk/CHANGES.txt (original)
+++ hadoop/pig/trunk/CHANGES.txt Wed Apr  8 21:59:31 2009
@@ -34,6 +34,10 @@
 PIG-725: javadoc: warning - Multiple sources of package comments found for
 package "org.apache.commons.logging" (gkesavan via sms)
 
+PIG-745: Add DataType.toString() to force basic types to chararray, useful
+for UDFs that want to handle all simple types as strings (ciemo via gates).
+
+
 Release 0.2.0 - Unreleased
 
 INCOMPATIBLE CHANGES

Modified: hadoop/pig/trunk/src/org/apache/pig/data/DataType.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/data/DataType.java?rev=763421&r1=763420&r2=763421&view=diff
==============================================================================
--- hadoop/pig/trunk/src/org/apache/pig/data/DataType.java (original)
+++ hadoop/pig/trunk/src/org/apache/pig/data/DataType.java Wed Apr  8 21:59:31 
2009
@@ -619,6 +619,64 @@
     }
 
     /**
+     * Force a data object to a String, if possible.  Any simple (atomic) type
+     * can be forced to a String including ByteArray.  Complex types cannot be
+     * forced to a String.  This isn't particularly efficient, so if you
+     * already <b>know</b> that the object you have is a String you
+     * should just cast it.
+     * @return The object as a String.
+     * @throws ExecException if the type can't be forced to a String.
+     */
+    public static String toString(Object o) throws ExecException {
+        try {
+                       switch (findType(o)) {
+                       case INTEGER:
+                           return ((Integer)o).toString();
+
+                       case LONG:
+                           return ((Long)o).toString();
+
+                       case FLOAT:
+                           return ((Float)o).toString();
+
+                       case DOUBLE:
+                           return ((Double)o).toString();
+
+                       case BYTEARRAY:
+                           return ((DataByteArray)o).toString();
+
+                       case CHARARRAY:
+                           return ((String)o);
+
+                       case NULL:
+                           return null;
+
+                       case BOOLEAN:
+                           return ((Boolean)o).toString();
+
+                       case BYTE:
+                           return ((Byte)o).toString();
+
+                       case MAP:
+                       case TUPLE:
+                       case BAG:
+                       case UNKNOWN:
+                       default:
+                           int errCode = 1071;
+                           String msg = "Cannot convert a " + findTypeName(o) +
+                           " to a String";
+                           throw new ExecException(msg, errCode, 
PigException.INPUT);
+                       }
+               } catch (ExecException ee) {
+                       throw ee;
+               } catch (Exception e) {
+                       int errCode = 2054;
+                       String msg = "Internal error. Could not convert " + o + 
" to String.";
+                       throw new ExecException(msg, errCode, PigException.BUG);
+               }
+    }
+
+    /**
      * If this object is a map, return it as a map.
      * This isn't particularly efficient, so if you
      * already <b>know</b> that the object you have is a Map you


Reply via email to