Author: daijy
Date: Fri Jun 12 23:03:20 2015
New Revision: 1685190
URL: http://svn.apache.org/r1685190
Log:
PIG-4578: ToDateISO should support optional ' ' space variant used by JDBC
Modified:
pig/trunk/CHANGES.txt
pig/trunk/src/org/apache/pig/builtin/ToDate.java
pig/trunk/test/org/apache/pig/test/TestBuiltin.java
Modified: pig/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1685190&r1=1685189&r2=1685190&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Fri Jun 12 23:03:20 2015
@@ -24,9 +24,13 @@ INCOMPATIBLE CHANGES
IMPROVEMENTS
+PIG-4570: Allow AvroStorage to use a class for the schema (pmazak via daijy)
+
BUG FIXES
-Release 0.15.0 - Unreleased
+PIG-4578: ToDateISO should support optional ' ' space variant used by JDBC
(michaelthoward via daijy)
+
+Release 0.15.0
INCOMPATIBLE CHANGES
Modified: pig/trunk/src/org/apache/pig/builtin/ToDate.java
URL:
http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/builtin/ToDate.java?rev=1685190&r1=1685189&r2=1685190&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/builtin/ToDate.java (original)
+++ pig/trunk/src/org/apache/pig/builtin/ToDate.java Fri Jun 12 23:03:20 2015
@@ -117,15 +117,45 @@ public class ToDate extends EvalFunc<Dat
}
public static DateTimeZone extractDateTimeZone(String dtStr) {
- return isoDateTimeFormatter.parseDateTime(dtStr).getZone();
+ return
isoDateTimeFormatter.parseDateTime(allowIso8601Space(dtStr)).getZone();
}
public static DateTime extractDateTime(String dtStr) {
- return isoDateTimeFormatter.parseDateTime(dtStr);
+ return isoDateTimeFormatter.parseDateTime(allowIso8601Space(dtStr));
+ }
+
+ /*
+ * ISO-8601 format and JDBC timestamp format are similar but not the
same.
+ *
+ * Strict ISO-8601 specifies a 'T' between the date portion and
+ * the time portion:
+ * 2015-05-29T10:41:30.123
+ *
+ * ISO-8601 allows a space instead of a 'T' as a looser variant.
+ * This variant is often adopted because it increases human
readability.
+ * The JDBC timestamp format uses the ' ' space variant.
+ * 2015-05-29 10:41:30.123
+ *
+ * Hive & Impala are database-oriented and generate JDBC timestamps
+ * with a ' ' space.
+ *
+ * We would like to accept both 'T' and ' ' space formats.
+ *
+ * org.joda.time.format.ISODateTimeFormatter requires the 'T'.
+ * The cleanest way to get joda-time to accept both is to convert
+ * the ' ' space to a a 'T' before feeding the string to the
+ * ISODateTimeFormatter.
+ */
+ private static String allowIso8601Space(String dtStr) {
+ if (dtStr == null || dtStr.length() <= 10 || dtStr.charAt(10) != ' ') {
+ return dtStr;
+ }
+ return dtStr.substring(0, 10) + 'T' + dtStr.substring(11);
}
@Override
public boolean allowCompileTimeCalculation() {
return true;
}
+
}
Modified: pig/trunk/test/org/apache/pig/test/TestBuiltin.java
URL:
http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestBuiltin.java?rev=1685190&r1=1685189&r2=1685190&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestBuiltin.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestBuiltin.java Fri Jun 12 23:03:20 2015
@@ -421,11 +421,41 @@ public class TestBuiltin {
DateTime dt2 = func2.exec(t2);
assertEquals(dt2.compareTo(new DateTime("2009-01-07T01:07:01.000Z")),
0);
+ Tuple t2space = TupleFactory.getInstance().newTuple(1);
+ t2space.set(0, "2009-01-07 01:07:01.000Z");
+ DateTime dt2space = func2.exec(t2space);
+ assertEquals(dt2space.compareTo(new
DateTime("2009-01-07T01:07:01.000Z")), 0);
+
+ Tuple t2dateOnly = TupleFactory.getInstance().newTuple(1);
+ t2dateOnly.set(0, "2015-05-29");
+ DateTime dt2dateOnly = func2.exec(t2dateOnly);
+ assertEquals(dt2dateOnly.compareTo(new DateTime("2015-05-29")), 0);
+
+ Tuple t2dateSpaceHour = TupleFactory.getInstance().newTuple(1);
+ t2dateSpaceHour.set(0, "2015-05-29 11");
+ DateTime dt2dateSpaceHour = func2.exec(t2dateSpaceHour);
+ assertEquals(dt2dateSpaceHour.compareTo(new
DateTime("2015-05-29T11")), 0);
+
+ Tuple t2dateSpaceHourMin = TupleFactory.getInstance().newTuple(1);
+ t2dateSpaceHourMin.set(0, "2015-05-29 11:38");
+ DateTime dt2dateSpaceHourMin = func2.exec(t2dateSpaceHourMin);
+ assertEquals(dt2dateSpaceHourMin.compareTo(new
DateTime("2015-05-29T11:38")), 0);
+
+ Tuple t2dateSpaceHourMinSec = TupleFactory.getInstance().newTuple(1);
+ t2dateSpaceHourMinSec.set(0, "2015-05-29 11:38:39");
+ DateTime dt2dateSpaceHourMinSec = func2.exec(t2dateSpaceHourMinSec);
+ assertEquals(dt2dateSpaceHourMinSec.compareTo(new
DateTime("2015-05-29T11:38:39")), 0);
+
Tuple t3 = TupleFactory.getInstance().newTuple(1);
t3.set(0, "2009-01-07T01:07:01.000+08:00");
DateTime dt3 = func2.exec(t3);
assertEquals(dt3.compareTo(new
DateTime("2009-01-07T01:07:01.000+08:00", DateTimeZone.forID("+08:00"))), 0);
+ Tuple t3space = TupleFactory.getInstance().newTuple(1);
+ t3space.set(0, "2009-01-07 01:07:01.000+08:00");
+ DateTime dt3space = func2.exec(t3space);
+ assertEquals(dt3space.compareTo(new
DateTime("2009-01-07T01:07:01.000+08:00", DateTimeZone.forID("+08:00"))), 0);
+
ToDate2ARGS func3 = new ToDate2ARGS();
Tuple t4 = TupleFactory.getInstance().newTuple(2);
t4.set(0, "2009.01.07 AD at 01:07:01");