hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jiten...@apache.org
Subject svn commit: r1573687 [2/3] - in /hive/trunk/ql/src: java/org/apache/hadoop/hive/ql/exec/vector/ java/org/apache/hadoop/hive/ql/exec/vector/expressions/ java/org/apache/hadoop/hive/ql/exec/vector/udf/ java/org/apache/hadoop/hive/ql/io/orc/ java/org/apac...
Date Mon, 03 Mar 2014 19:28:18 GMT
Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubColScalar.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubColScalar.java?rev=1573687&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubColScalar.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubColScalar.java Mon Mar  3 19:28:17 2014
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.exec.vector.expressions;
+
+/**
+ * Returns the date that is num_days before start_date.
+ */
+public class VectorUDFDateSubColScalar extends VectorUDFDateAddColScalar {
+  public VectorUDFDateSubColScalar(int colNum, long numDays, int outputColumn) {
+    super(colNum, numDays, outputColumn);
+    isPositive = false;
+  }
+
+  public VectorUDFDateSubColScalar() {
+    super();
+    isPositive = false;
+  }
+}

Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubScalarCol.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubScalarCol.java?rev=1573687&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubScalarCol.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateSubScalarCol.java Mon Mar  3 19:28:17 2014
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.exec.vector.expressions;
+
+public class VectorUDFDateSubScalarCol extends VectorUDFDateAddScalarCol {
+  public VectorUDFDateSubScalarCol(Object object, int colNum, int outputColumn) {
+    super(object, colNum, outputColumn);
+    isPositive = false;
+  }
+
+  public VectorUDFDateSubScalarCol() {
+    super();
+    isPositive = false;
+  }
+}

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFMonthLong.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFMonthLong.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFMonthLong.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFMonthLong.java Mon Mar  3 19:28:17 2014
@@ -37,9 +37,14 @@ public final class VectorUDFMonthLong ex
   }
 
   @Override
-  protected long getField(long time) {
+  protected long getTimestampField(long time) {
     /* january is 0 */
-    return 1 + super.getField(time);
+    return 1 + super.getTimestampField(time);
   }
 
+  @Override
+  protected long getDateField(long days) {
+    /* january is 0 */
+    return 1 + super.getDateField(days);
+  }
 }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFTimestampFieldLong.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFTimestampFieldLong.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFTimestampFieldLong.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFTimestampFieldLong.java Mon Mar  3 19:28:17 2014
@@ -24,9 +24,10 @@ import java.util.Calendar;
 import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
 import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor;
 import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.serde2.io.DateWritable;
 
 /**
- * Abstract class to return various fields from a Timestamp.
+ * Abstract class to return various fields from a Timestamp or Date.
  */
 public abstract class VectorUDFTimestampFieldLong extends VectorExpression {
 
@@ -75,11 +76,16 @@ public abstract class VectorUDFTimestamp
     return ts;
   }
 
-  protected long getField(long time) {
+  protected long getTimestampField(long time) {
     calendar.setTime(getTimestamp(time));
     return calendar.get(field);
   }
 
+  protected long getDateField(long days) {
+    calendar.setTimeInMillis(DateWritable.daysToMillis((int) days));
+    return calendar.get(field);
+  }
+
   @Override
   public void evaluate(VectorizedRowBatch batch) {
     LongColumnVector outV = (LongColumnVector) batch.cols[outputColumn];
@@ -96,38 +102,78 @@ public abstract class VectorUDFTimestamp
     /* true for all algebraic UDFs with no state */
     outV.isRepeating = inputCol.isRepeating;
 
-    if (inputCol.noNulls) {
-      outV.noNulls = true;
-      if (batch.selectedInUse) {
-        for(int j=0; j < n; j++) {
-          int i = sel[j];
-          outV.vector[i] = getField(inputCol.vector[i]);
-        }
-      } else {
-        for(int i = 0; i < n; i++) {
-          outV.vector[i] = getField(inputCol.vector[i]);
-        }
-      }
-    } else {
-      // Handle case with nulls. Don't do function if the value is null, to save time,
-      // because calling the function can be expensive.
-      outV.noNulls = false;
-      if (batch.selectedInUse) {
-        for(int j=0; j < n; j++) {
-          int i = sel[j];
-          outV.isNull[i] = inputCol.isNull[i];
-          if (!inputCol.isNull[i]) {
-            outV.vector[i] = getField(inputCol.vector[i]);
+    switch (inputTypes[0]) {
+      case TIMESTAMP:
+        if (inputCol.noNulls) {
+          outV.noNulls = true;
+          if (batch.selectedInUse) {
+            for(int j=0; j < n; j++) {
+              int i = sel[j];
+              outV.vector[i] = getTimestampField(inputCol.vector[i]);
+            }
+          } else {
+            for(int i = 0; i < n; i++) {
+              outV.vector[i] = getTimestampField(inputCol.vector[i]);
+            }
+          }
+        } else {
+          // Handle case with nulls. Don't do function if the value is null, to save time,
+          // because calling the function can be expensive.
+          outV.noNulls = false;
+          if (batch.selectedInUse) {
+            for(int j=0; j < n; j++) {
+              int i = sel[j];
+              outV.isNull[i] = inputCol.isNull[i];
+              if (!inputCol.isNull[i]) {
+                outV.vector[i] = getTimestampField(inputCol.vector[i]);
+              }
+            }
+          } else {
+            for(int i = 0; i < n; i++) {
+              outV.isNull[i] = inputCol.isNull[i];
+              if (!inputCol.isNull[i]) {
+                outV.vector[i] = getTimestampField(inputCol.vector[i]);
+              }
+            }
           }
         }
-      } else {
-        for(int i = 0; i < n; i++) {
-          outV.isNull[i] = inputCol.isNull[i];
-          if (!inputCol.isNull[i]) {
-            outV.vector[i] = getField(inputCol.vector[i]);
+        break;
+
+      case DATE:
+        if (inputCol.noNulls) {
+          outV.noNulls = true;
+          if (batch.selectedInUse) {
+            for(int j=0; j < n; j++) {
+              int i = sel[j];
+              outV.vector[i] = getDateField(inputCol.vector[i]);
+            }
+          } else {
+            for(int i = 0; i < n; i++) {
+              outV.vector[i] = getDateField(inputCol.vector[i]);
+            }
+          }
+        } else {
+          // Handle case with nulls. Don't do function if the value is null, to save time,
+          // because calling the function can be expensive.
+          outV.noNulls = false;
+          if (batch.selectedInUse) {
+            for(int j=0; j < n; j++) {
+              int i = sel[j];
+              outV.isNull[i] = inputCol.isNull[i];
+              if (!inputCol.isNull[i]) {
+                outV.vector[i] = getDateField(inputCol.vector[i]);
+              }
+            }
+          } else {
+            for(int i = 0; i < n; i++) {
+              outV.isNull[i] = inputCol.isNull[i];
+              if (!inputCol.isNull[i]) {
+                outV.vector[i] = getDateField(inputCol.vector[i]);
+              }
+            }
           }
         }
-      }
+        break;
     }
   }
 

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFUnixTimeStampLong.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFUnixTimeStampLong.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFUnixTimeStampLong.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFUnixTimeStampLong.java Mon Mar  3 19:28:17 2014
@@ -18,6 +18,8 @@
 
 package org.apache.hadoop.hive.ql.exec.vector.expressions;
 
+import org.apache.hadoop.hive.serde2.io.DateWritable;
+
 /**
  * Return Unix Timestamp.
  * Extends {@link VectorUDFTimestampFieldLong}
@@ -27,14 +29,20 @@ public final class VectorUDFUnixTimeStam
   private static final long serialVersionUID = 1L;
 
   @Override
-  protected long getField(long time) {
+  protected long getTimestampField(long time) {
     long ms = (time / (1000*1000*1000)) * 1000;
     long remainder = time % (1000*1000*1000);
     /* negative timestamps need to be adjusted */
     if(remainder < 0) {
       ms -= 1000;
     }
-    return ms/1000;
+    return ms / 1000;
+  }
+
+  @Override
+  protected long getDateField(long days) {
+    long ms = DateWritable.daysToMillis((int) days);
+    return ms / 1000;
   }
 
   public VectorUDFUnixTimeStampLong(int colNum, int outputColumn) {

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFYearLong.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFYearLong.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFYearLong.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFYearLong.java Mon Mar  3 19:28:17 2014
@@ -45,7 +45,7 @@ public final class VectorUDFYearLong ext
   }
 
   @Override
-  protected long getField(long time) {
+  protected long getTimestampField(long time) {
     /* binarySearch is faster than a loop doing a[i] (no array out of bounds checks) */
     int year = Arrays.binarySearch(YEAR_BOUNDARIES, time);
     if(year >= 0) {

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/udf/VectorUDFAdaptor.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/udf/VectorUDFAdaptor.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/udf/VectorUDFAdaptor.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/udf/VectorUDFAdaptor.java Mon Mar  3 19:28:17 2014
@@ -17,6 +17,7 @@
  */
 package org.apache.hadoop.hive.ql.exec.vector.udf;
 
+import java.sql.Date;
 import java.sql.Timestamp;
 
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
@@ -27,9 +28,11 @@ import org.apache.hadoop.hive.ql.exec.ve
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.serde2.io.DateWritable;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableBooleanObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableByteObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableDateObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableDoubleObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableFloatObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableIntObjectInspector;
@@ -280,6 +283,16 @@ public class VectorUDFAdaptor extends Ve
                                        // The % 1000000 operation removes the ms values
                                        // so that the milliseconds are not counted twice.
       lv.vector[i] = l;
+    } else if (outputOI instanceof WritableDateObjectInspector) {
+      LongColumnVector lv = (LongColumnVector) colVec;
+      Date ts;
+      if (value instanceof Date) {
+        ts = (Date) value;
+      } else {
+        ts = ((WritableDateObjectInspector) outputOI).getPrimitiveJavaObject(value);
+      }
+      long l = DateWritable.dateToDays(ts);
+      lv.vector[i] = l;
     } else if (outputOI instanceof WritableBooleanObjectInspector) {
       LongColumnVector lv = (LongColumnVector) colVec;
       if (value instanceof Boolean) {

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/io/orc/RecordReaderImpl.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/io/orc/RecordReaderImpl.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/io/orc/RecordReaderImpl.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/io/orc/RecordReaderImpl.java Mon Mar  3 19:28:17 2014
@@ -2543,6 +2543,7 @@ class RecordReaderImpl implements Record
       case LONG:
       case FLOAT:
       case DOUBLE:
+      case DATE:
       case STRUCT:
       case MAP:
       case LIST:

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java Mon Mar  3 19:28:17 2014
@@ -190,6 +190,11 @@ public class Vectorizer implements Physi
     supportedGenericUDFs.add(UDFWeekOfYear.class);
     supportedGenericUDFs.add(GenericUDFToUnixTimeStamp.class);
 
+    supportedGenericUDFs.add(GenericUDFDateAdd.class);
+    supportedGenericUDFs.add(GenericUDFDateSub.class);
+    supportedGenericUDFs.add(GenericUDFDate.class);
+    supportedGenericUDFs.add(GenericUDFDateDiff.class);
+
     supportedGenericUDFs.add(UDFLike.class);
     supportedGenericUDFs.add(UDFRegExp.class);
     supportedGenericUDFs.add(UDFSubstr.class);
@@ -243,6 +248,7 @@ public class Vectorizer implements Physi
     supportedGenericUDFs.add(UDFToString.class);
     supportedGenericUDFs.add(GenericUDFTimestamp.class);
     supportedGenericUDFs.add(GenericUDFToDecimal.class);
+    supportedGenericUDFs.add(GenericUDFToDate.class);
 
     // For conditional expressions
     supportedGenericUDFs.add(GenericUDFIf.class);

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java Mon Mar  3 19:28:17 2014
@@ -25,6 +25,9 @@ import java.util.Date;
 import org.apache.hadoop.hive.ql.exec.Description;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateLong;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateString;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.io.DateWritable;
 import org.apache.hadoop.hive.serde2.io.TimestampWritable;
@@ -46,6 +49,7 @@ import org.apache.hadoop.io.Text;
     extended = "Example:\n "
         + "  > SELECT _FUNC_('2009-07-30 04:17:52') FROM src LIMIT 1;\n"
         + "  '2009-07-30'")
+@VectorizedExpressions({VectorUDFDateString.class, VectorUDFDateLong.class})
 public class GenericUDFDate extends GenericUDF {
   private transient SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   private transient TimestampConverter timestampConverter;

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java Mon Mar  3 19:28:17 2014
@@ -27,6 +27,10 @@ import org.apache.hadoop.hive.ql.exec.De
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateAddColCol;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateAddColScalar;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateAddScalarCol;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.io.DateWritable;
 import org.apache.hadoop.hive.serde2.io.TimestampWritable;
@@ -59,6 +63,7 @@ import org.apache.hadoop.io.IntWritable;
         + "Example:\n "
         + "  > SELECT _FUNC_('2009-30-07', 1) FROM src LIMIT 1;\n"
         + "  '2009-31-07'")
+@VectorizedExpressions({VectorUDFDateAddColScalar.class, VectorUDFDateAddScalarCol.class, VectorUDFDateAddColCol.class})
 public class GenericUDFDateAdd extends GenericUDF {
   private transient SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   private transient TimestampConverter timestampConverter;

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java Mon Mar  3 19:28:17 2014
@@ -20,7 +20,6 @@ package org.apache.hadoop.hive.ql.udf.ge
 import java.sql.Timestamp;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.Calendar;
 import java.util.Date;
 import java.util.TimeZone;
 
@@ -28,6 +27,10 @@ import org.apache.hadoop.hive.ql.exec.De
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateDiffColCol;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateDiffColScalar;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateDiffScalarCol;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.io.DateWritable;
 import org.apache.hadoop.hive.serde2.io.TimestampWritable;
@@ -38,7 +41,6 @@ import org.apache.hadoop.hive.serde2.obj
 import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter;
-import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.IntWritable;
 
 /**
@@ -57,6 +59,7 @@ import org.apache.hadoop.io.IntWritable;
         + "Example:\n "
         + "  > SELECT _FUNC_('2009-30-07', '2009-31-07') FROM src LIMIT 1;\n"
         + "  1")
+@VectorizedExpressions({VectorUDFDateDiffColScalar.class, VectorUDFDateDiffColCol.class, VectorUDFDateDiffScalarCol.class})
 public class GenericUDFDateDiff extends GenericUDF {
   private transient SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   private transient Converter inputConverter1;

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateSub.java Mon Mar  3 19:28:17 2014
@@ -27,6 +27,10 @@ import org.apache.hadoop.hive.ql.exec.De
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateSubColCol;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateSubColScalar;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFDateSubScalarCol;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.io.DateWritable;
 import org.apache.hadoop.hive.serde2.io.TimestampWritable;
@@ -59,6 +63,7 @@ import org.apache.hadoop.io.IntWritable;
         + "Example:\n "
         + "  > SELECT _FUNC_('2009-30-07', 1) FROM src LIMIT 1;\n"
         + "  '2009-29-07'")
+@VectorizedExpressions({VectorUDFDateSubColScalar.class, VectorUDFDateSubScalarCol.class, VectorUDFDateSubColCol.class})
 public class GenericUDFDateSub extends GenericUDF {
   private transient SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   private transient TimestampConverter timestampConverter;

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToDate.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToDate.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToDate.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToDate.java Mon Mar  3 19:28:17 2014
@@ -20,6 +20,9 @@ package org.apache.hadoop.hive.ql.udf.ge
 import org.apache.hadoop.hive.ql.exec.Description;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.CastLongToDate;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.CastStringToDate;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
@@ -38,6 +41,7 @@ import org.apache.hadoop.hive.serde2.obj
     + "Example:\n "
     + "  > SELECT CAST('2009-01-01' AS DATE) FROM src LIMIT 1;\n"
     + "  '2009-01-01'")
+@VectorizedExpressions({CastStringToDate.class, CastLongToDate.class})
 public class GenericUDFToDate extends GenericUDF {
 
   private transient PrimitiveObjectInspector argumentOI;

Added: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorDateExpressions.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorDateExpressions.java?rev=1573687&view=auto
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorDateExpressions.java (added)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorDateExpressions.java Mon Mar  3 19:28:17 2014
@@ -0,0 +1,423 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.exec.vector.expressions;
+
+import junit.framework.Assert;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.ql.udf.UDFDayOfMonth;
+import org.apache.hadoop.hive.ql.udf.UDFMonth;
+import org.apache.hadoop.hive.ql.udf.UDFWeekOfYear;
+import org.apache.hadoop.hive.ql.udf.UDFYear;
+import org.apache.hadoop.hive.serde2.io.DateWritable;
+import org.apache.hadoop.hive.serde2.io.TimestampWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.LongWritable;
+import org.junit.Test;
+
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+import java.util.Random;
+
+public class TestVectorDateExpressions {
+  /* copied over from VectorUDFTimestampFieldLong */
+  private TimestampWritable toTimestampWritable(long daysSinceEpoch) {
+    Timestamp ts = new Timestamp(DateWritable.daysToMillis((int) daysSinceEpoch));
+    return new TimestampWritable(ts);
+  }
+
+  private int[] getAllBoundaries() {
+    List<Integer> boundaries = new ArrayList<Integer>(1);
+    Calendar c = Calendar.getInstance();
+    c.setTimeInMillis(0); // c.set doesn't reset millis
+    for (int year = 1902; year <= 2038; year++) {
+      c.set(year, Calendar.JANUARY, 1, 0, 0, 0);
+      int exactly = (int) (c.getTimeInMillis() / (24 * 60 * 60 * 1000));
+      int before = exactly - 1;
+      int after = exactly + 1;
+      boundaries.add(Integer.valueOf(before));
+      boundaries.add(Integer.valueOf(exactly));
+      boundaries.add(Integer.valueOf(after));
+    }
+    Integer[] indices = boundaries.toArray(new Integer[1]);
+    return ArrayUtils.toPrimitive(indices);
+  }
+
+  private VectorizedRowBatch getVectorizedRandomRowBatch(int seed, int size) {
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+    LongColumnVector lcv = new LongColumnVector(size);
+    Random rand = new Random(seed);
+    for (int i = 0; i < size; i++) {
+      lcv.vector[i] = (rand.nextInt());
+    }
+    batch.cols[0] = lcv;
+    batch.cols[1] = new LongColumnVector(size);
+    batch.size = size;
+    return batch;
+  }
+
+  /*
+   * Input array is used to fill the entire size of the vector row batch
+   */
+  private VectorizedRowBatch getVectorizedRowBatch(int[] inputs, int size) {
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+    LongColumnVector lcv = new LongColumnVector(size);
+    for (int i = 0; i < size; i++) {
+      lcv.vector[i] = inputs[i % inputs.length];
+    }
+    batch.cols[0] = lcv;
+    batch.cols[1] = new LongColumnVector(size);
+    batch.size = size;
+    return batch;
+  }
+
+  private void compareToUDFYearDate(long t, int y) {
+    UDFYear udf = new UDFYear();
+    TimestampWritable tsw = toTimestampWritable(t);
+    IntWritable res = udf.evaluate(tsw);
+    Assert.assertEquals(res.get(), y);
+  }
+
+  private void verifyUDFYear(VectorizedRowBatch batch) {
+    VectorExpression udf = null;
+    udf = new VectorUDFYearLong(0, 1);
+    udf.setInputTypes(VectorExpression.Type.DATE);
+    udf.evaluate(batch);
+    final int in = 0;
+    final int out = 1;
+
+    for (int i = 0; i < batch.size; i++) {
+      if (batch.cols[in].noNulls || !batch.cols[in].isNull[i]) {
+        if (!batch.cols[in].noNulls) {
+          Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+        }
+        long t = ((LongColumnVector) batch.cols[in]).vector[i];
+        long y = ((LongColumnVector) batch.cols[out]).vector[i];
+        compareToUDFYearDate(t, (int) y);
+      } else {
+        Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testVectorUDFYear() {
+    VectorizedRowBatch batch = getVectorizedRowBatch(new int[] {0},
+            VectorizedRowBatch.DEFAULT_SIZE);
+    Assert.assertTrue(((LongColumnVector) batch.cols[1]).noNulls);
+    Assert.assertFalse(((LongColumnVector) batch.cols[1]).isRepeating);
+    verifyUDFYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFYear(batch);
+
+    int[] boundaries = getAllBoundaries();
+    batch = getVectorizedRowBatch(boundaries, boundaries.length);
+    verifyUDFYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFYear(batch);
+
+    batch = getVectorizedRowBatch(new int[] {0}, 1);
+    batch.cols[0].isRepeating = true;
+    verifyUDFYear(batch);
+    batch.cols[0].noNulls = false;
+    batch.cols[0].isNull[0] = true;
+    verifyUDFYear(batch);
+
+    batch = getVectorizedRandomRowBatch(200, VectorizedRowBatch.DEFAULT_SIZE);
+    verifyUDFYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFYear(batch);
+  }
+
+  private void compareToUDFDayOfMonthDate(long t, int y) {
+    UDFDayOfMonth udf = new UDFDayOfMonth();
+    TimestampWritable tsw = toTimestampWritable(t);
+    IntWritable res = udf.evaluate(tsw);
+    Assert.assertEquals(res.get(), y);
+  }
+
+  private void verifyUDFDayOfMonth(VectorizedRowBatch batch) {
+    VectorExpression udf = null;
+    udf = new VectorUDFDayOfMonthLong(0, 1);
+    udf.setInputTypes(VectorExpression.Type.DATE);
+    udf.evaluate(batch);
+    final int in = 0;
+    final int out = 1;
+
+    for (int i = 0; i < batch.size; i++) {
+      if (batch.cols[in].noNulls || !batch.cols[in].isNull[i]) {
+        if (!batch.cols[in].noNulls) {
+          Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+        }
+        long t = ((LongColumnVector) batch.cols[in]).vector[i];
+        long y = ((LongColumnVector) batch.cols[out]).vector[i];
+        compareToUDFDayOfMonthDate(t, (int) y);
+      } else {
+        Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testVectorUDFDayOfMonth() {
+    VectorizedRowBatch batch = getVectorizedRowBatch(new int[] {0},
+            VectorizedRowBatch.DEFAULT_SIZE);
+    Assert.assertTrue(((LongColumnVector) batch.cols[1]).noNulls);
+    Assert.assertFalse(((LongColumnVector) batch.cols[1]).isRepeating);
+    verifyUDFDayOfMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFDayOfMonth(batch);
+
+    int[] boundaries = getAllBoundaries();
+    batch = getVectorizedRowBatch(boundaries, boundaries.length);
+    verifyUDFDayOfMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFDayOfMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFDayOfMonth(batch);
+
+    batch = getVectorizedRowBatch(new int[] {0}, 1);
+    batch.cols[0].isRepeating = true;
+    verifyUDFDayOfMonth(batch);
+    batch.cols[0].noNulls = false;
+    batch.cols[0].isNull[0] = true;
+    verifyUDFDayOfMonth(batch);
+
+    batch = getVectorizedRandomRowBatch(200, VectorizedRowBatch.DEFAULT_SIZE);
+    verifyUDFDayOfMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFDayOfMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFDayOfMonth(batch);
+  }
+
+  private void compareToUDFMonthDate(long t, int y) {
+    UDFMonth udf = new UDFMonth();
+    TimestampWritable tsw = toTimestampWritable(t);
+    IntWritable res = udf.evaluate(tsw);
+    Assert.assertEquals(res.get(), y);
+  }
+
+  private void verifyUDFMonth(VectorizedRowBatch batch) {
+    VectorExpression udf;
+      udf = new VectorUDFMonthLong(0, 1);
+    udf.setInputTypes(VectorExpression.Type.DATE);
+    udf.evaluate(batch);
+    final int in = 0;
+    final int out = 1;
+
+    for (int i = 0; i < batch.size; i++) {
+      if (batch.cols[in].noNulls || !batch.cols[in].isNull[i]) {
+        if (!batch.cols[in].noNulls) {
+          Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+        }
+        long t = ((LongColumnVector) batch.cols[in]).vector[i];
+        long y = ((LongColumnVector) batch.cols[out]).vector[i];
+        compareToUDFMonthDate(t, (int) y);
+      } else {
+        Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testVectorUDFMonth() {
+    VectorizedRowBatch batch = getVectorizedRowBatch(new int[] {0},
+            VectorizedRowBatch.DEFAULT_SIZE);
+    Assert.assertTrue(((LongColumnVector) batch.cols[1]).noNulls);
+    Assert.assertFalse(((LongColumnVector) batch.cols[1]).isRepeating);
+    verifyUDFMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFMonth(batch);
+
+    int[] boundaries = getAllBoundaries();
+    batch = getVectorizedRowBatch(boundaries, boundaries.length);
+    verifyUDFMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFMonth(batch);
+
+    batch = getVectorizedRowBatch(new int[] {0}, 1);
+    batch.cols[0].isRepeating = true;
+    verifyUDFMonth(batch);
+    batch.cols[0].noNulls = false;
+    batch.cols[0].isNull[0] = true;
+    verifyUDFMonth(batch);
+
+    batch = getVectorizedRandomRowBatch(200, VectorizedRowBatch.DEFAULT_SIZE);
+    verifyUDFMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFMonth(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFMonth(batch);
+  }
+
+  private LongWritable getLongWritable(TimestampWritable i) {
+    LongWritable result = new LongWritable();
+    if (i == null) {
+      return null;
+    } else {
+      result.set(i.getSeconds());
+      return result;
+    }
+  }
+
+  private void compareToUDFUnixTimeStampDate(long t, long y) {
+    TimestampWritable tsw = toTimestampWritable(t);
+    LongWritable res = getLongWritable(tsw);
+    if(res.get() != y) {
+      System.out.printf("%d vs %d for %d, %d\n", res.get(), y, t,
+              tsw.getTimestamp().getTime()/1000);
+    }
+
+    Assert.assertEquals(res.get(), y);
+  }
+
+  private void verifyUDFUnixTimeStamp(VectorizedRowBatch batch) {
+    VectorExpression udf;
+    udf = new VectorUDFUnixTimeStampLong(0, 1);
+    udf.setInputTypes(VectorExpression.Type.DATE);
+    udf.evaluate(batch);
+    final int in = 0;
+    final int out = 1;
+
+    for (int i = 0; i < batch.size; i++) {
+      if (batch.cols[in].noNulls || !batch.cols[in].isNull[i]) {
+        if (!batch.cols[out].noNulls) {
+          Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+        }
+        long t = ((LongColumnVector) batch.cols[in]).vector[i];
+        long y = ((LongColumnVector) batch.cols[out]).vector[i];
+        compareToUDFUnixTimeStampDate(t, y);
+      } else {
+        Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testVectorUDFUnixTimeStamp() {
+    VectorizedRowBatch batch = getVectorizedRowBatch(new int[] {0},
+            VectorizedRowBatch.DEFAULT_SIZE);
+    Assert.assertTrue(((LongColumnVector) batch.cols[1]).noNulls);
+    Assert.assertFalse(((LongColumnVector) batch.cols[1]).isRepeating);
+    verifyUDFUnixTimeStamp(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFUnixTimeStamp(batch);
+
+    int[] boundaries = getAllBoundaries();
+    batch = getVectorizedRowBatch(boundaries, boundaries.length);
+    verifyUDFUnixTimeStamp(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFUnixTimeStamp(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFUnixTimeStamp(batch);
+
+    batch = getVectorizedRowBatch(new int[] {0}, 1);
+    batch.cols[0].isRepeating = true;
+    verifyUDFUnixTimeStamp(batch);
+    batch.cols[0].noNulls = false;
+    batch.cols[0].isNull[0] = true;
+    verifyUDFUnixTimeStamp(batch);
+
+    batch = getVectorizedRandomRowBatch(200, VectorizedRowBatch.DEFAULT_SIZE);
+    verifyUDFUnixTimeStamp(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFUnixTimeStamp(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFUnixTimeStamp(batch);
+  }
+
+  private void compareToUDFWeekOfYearDate(long t, int y) {
+    UDFWeekOfYear udf = new UDFWeekOfYear();
+    TimestampWritable tsw = toTimestampWritable(t);
+    IntWritable res = udf.evaluate(tsw);
+    Assert.assertEquals(res.get(), y);
+  }
+
+  private void verifyUDFWeekOfYear(VectorizedRowBatch batch) {
+    VectorExpression udf;
+    udf = new VectorUDFWeekOfYearLong(0, 1);
+    udf.setInputTypes(VectorExpression.Type.DATE);
+    udf.evaluate(batch);
+    final int in = 0;
+    final int out = 1;
+
+    for (int i = 0; i < batch.size; i++) {
+      if (batch.cols[in].noNulls || !batch.cols[in].isNull[i]) {
+        long t = ((LongColumnVector) batch.cols[in]).vector[i];
+        long y = ((LongColumnVector) batch.cols[out]).vector[i];
+        compareToUDFWeekOfYearDate(t, (int) y);
+      } else {
+        Assert.assertEquals(batch.cols[out].isNull[i], batch.cols[in].isNull[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testVectorUDFWeekOfYear() {
+    VectorizedRowBatch batch = getVectorizedRowBatch(new int[] {0},
+            VectorizedRowBatch.DEFAULT_SIZE);
+    Assert.assertTrue(((LongColumnVector) batch.cols[1]).noNulls);
+    Assert.assertFalse(((LongColumnVector) batch.cols[1]).isRepeating);
+    verifyUDFWeekOfYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFWeekOfYear(batch);
+
+    int[] boundaries = getAllBoundaries();
+    batch = getVectorizedRowBatch(boundaries, boundaries.length);
+    verifyUDFWeekOfYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFWeekOfYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFWeekOfYear(batch);
+
+    batch = getVectorizedRowBatch(new int[] {0}, 1);
+    batch.cols[0].isRepeating = true;
+    verifyUDFWeekOfYear(batch);
+    batch.cols[0].noNulls = false;
+    batch.cols[0].isNull[0] = true;
+    verifyUDFWeekOfYear(batch);
+
+    batch = getVectorizedRandomRowBatch(200, VectorizedRowBatch.DEFAULT_SIZE);
+    verifyUDFWeekOfYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    verifyUDFWeekOfYear(batch);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[1]);
+    verifyUDFWeekOfYear(batch);
+  }
+
+  public static void main(String[] args) {
+    TestVectorDateExpressions self = new TestVectorDateExpressions();
+    self.testVectorUDFYear();
+    self.testVectorUDFMonth();
+    self.testVectorUDFDayOfMonth();
+    self.testVectorUDFWeekOfYear();
+    self.testVectorUDFUnixTimeStamp();
+  }
+}

Added: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorGenericDateExpressions.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorGenericDateExpressions.java?rev=1573687&view=auto
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorGenericDateExpressions.java (added)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorGenericDateExpressions.java Mon Mar  3 19:28:17 2014
@@ -0,0 +1,751 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hive.ql.exec.vector.expressions;
+
+import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.serde2.io.DateWritable;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+import java.io.UnsupportedEncodingException;
+import java.sql.Date;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Random;
+
+public class TestVectorGenericDateExpressions {
+  private int size = 200;
+  private Random random = new Random();
+  private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
+  private List<VectorExpression.Type> dateTimestampStringTypes =
+      Arrays.<VectorExpression.Type>asList(VectorExpression.Type.DATE, VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING);
+
+  private long newRandom(int i) {
+    return random.nextInt(i);
+  }
+
+  private LongColumnVector newRandomLongColumnVector(int range, int size) {
+    LongColumnVector vector = new LongColumnVector(size);
+    for (int i = 0; i < size; i++) {
+      vector.vector[i] = random.nextInt(range);
+    }
+    return vector;
+  }
+
+  private LongColumnVector toTimestamp(LongColumnVector date) {
+    LongColumnVector vector = new LongColumnVector(size);
+    for (int i = 0; i < size; i++) {
+      if (date.isNull[i]) {
+        vector.isNull[i] = true;
+        vector.noNulls = false;
+      } else {
+        vector.vector[i] = toTimestamp(date.vector[i]);
+      }
+    }
+    return vector;
+  }
+
+  private long toTimestamp(long date) {
+    return DateWritable.daysToMillis((int) date) * 1000000;
+  }
+
+  private BytesColumnVector toString(LongColumnVector date) {
+    BytesColumnVector bcv = new BytesColumnVector(size);
+    for (int i = 0; i < size; i++) {
+      if (date.isNull[i]) {
+        bcv.isNull[i] = true;
+        bcv.noNulls = false;
+      } else {
+        bcv.vector[i] = toString(date.vector[i]);
+        bcv.start[i] = 0;
+        bcv.length[i] = bcv.vector[i].length;
+      }
+    }
+    return bcv;
+  }
+
+  private byte[] toString(long date) {
+    try {
+      String formatted = formatter.format(new Date(DateWritable.daysToMillis((int) date)));
+      return formatted.getBytes("UTF-8");
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private void validateDateAdd(VectorizedRowBatch batch, VectorExpression.Type colType1, long scalar2,
+                               boolean isPositive, LongColumnVector date1) {
+    VectorUDFDateAddColScalar udf;
+    if (isPositive) {
+      udf = new VectorUDFDateAddColScalar(0, scalar2, 1);
+    } else {
+      udf = new VectorUDFDateSubColScalar(0, scalar2, 1);
+    }
+    udf.setInputTypes(colType1, VectorExpression.Type.OTHER);
+    udf.evaluate(batch);
+    BytesColumnVector output = (BytesColumnVector) batch.cols[1];
+
+    try {
+      for (int i = 0; i < size; i++) {
+        String expected;
+        if (isPositive) {
+          expected = new String(toString(date1.vector[i] + scalar2), "UTF-8");
+        } else {
+          expected = new String(toString(date1.vector[i] - scalar2), "UTF-8");
+        }
+        if (date1.isNull[i]) {
+          Assert.assertTrue(output.isNull[i]);
+        } else {
+          String actual = new String(output.vector[i], output.start[i], output.start[i] + output.length[i], "UTF-8");
+          Assert.assertEquals("expectedLen:" + expected.length() + " actualLen:" + actual.length(), expected, actual);
+        }
+      }
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private ColumnVector castTo(LongColumnVector date, VectorExpression.Type type) {
+    switch (type) {
+      case DATE:
+        return date;
+
+      case TIMESTAMP:
+        return toTimestamp(date);
+
+      case STRING:
+        return toString(date);
+    }
+    return null;
+  }
+
+  private void testDateAddColScalar(VectorExpression.Type colType1, boolean isPositive) {
+    LongColumnVector date1 = newRandomLongColumnVector(10000, size);
+    ColumnVector col1 = castTo(date1, colType1);
+    long scalar2 = newRandom(1000);
+    BytesColumnVector output = new BytesColumnVector(size);
+
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+    batch.cols[0] = col1;
+    batch.cols[1] = output;
+
+    validateDateAdd(batch, colType1, scalar2, isPositive, date1);
+    TestVectorizedRowBatch.addRandomNulls(batch.cols[0]);
+    validateDateAdd(batch, colType1, scalar2, isPositive, date1);
+  }
+
+  @Test
+  public void testDateAddColScalar() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes)
+      testDateAddColScalar(colType1, true);
+
+    VectorExpression udf = new VectorUDFDateAddColScalar(0, 0, 1);
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  @Test
+  public void testDateSubColScalar() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes)
+      testDateAddColScalar(colType1, false);
+
+    VectorExpression udf = new VectorUDFDateSubColScalar(0, 0, 1);
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  private void validateDateAdd(VectorizedRowBatch batch, long scalar1, LongColumnVector date2,
+                               VectorExpression.Type colType1, boolean isPositive) {
+    VectorExpression udf = null;
+    if (isPositive) {
+      switch (colType1) {
+        case DATE:
+          udf = new VectorUDFDateAddScalarCol(scalar1, 0, 1);
+          break;
+        case TIMESTAMP:
+          udf = new VectorUDFDateAddScalarCol(toTimestamp(scalar1), 0, 1);
+          break;
+        case STRING:
+          udf = new VectorUDFDateAddScalarCol(toString(scalar1), 0, 1);
+          break;
+      }
+    } else {
+      switch (colType1) {
+        case DATE:
+          udf = new VectorUDFDateSubScalarCol(scalar1, 0, 1);
+          break;
+        case TIMESTAMP:
+          udf = new VectorUDFDateSubScalarCol(toTimestamp(scalar1), 0, 1);
+          break;
+        case STRING:
+          udf = new VectorUDFDateSubScalarCol(toString(scalar1), 0, 1);
+          break;
+      }
+    }
+    udf.setInputTypes(colType1, VectorExpression.Type.OTHER);
+    udf.evaluate(batch);
+
+    BytesColumnVector output = (BytesColumnVector) batch.cols[1];
+    try {
+      for (int i = 0; i < date2.vector.length; i++) {
+        String expected;
+        if (isPositive) {
+          expected = new String(toString(scalar1 + date2.vector[i]), "UTF-8");
+        } else {
+          expected = new String(toString(scalar1 - date2.vector[i]), "UTF-8");
+        }
+        if (date2.isNull[i]) {
+          Assert.assertTrue(output.isNull[i]);
+        } else {
+          Assert.assertEquals(expected,
+              new String(output.vector[i], output.start[i], output.start[i] + output.length[i], "UTF-8"));
+        }
+      }
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private void testDateAddScalarCol(VectorExpression.Type colType1, boolean isPositive) {
+    LongColumnVector date2 = newRandomLongColumnVector(10000, size);
+    long scalar1 = newRandom(1000);
+
+    BytesColumnVector output = new BytesColumnVector(size);
+
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+    batch.cols[0] = date2;
+    batch.cols[1] = output;
+    validateDateAdd(batch, scalar1, date2, colType1, isPositive);
+    TestVectorizedRowBatch.addRandomNulls(date2);
+    batch.cols[0] = date2;
+    validateDateAdd(batch, scalar1, date2, colType1, isPositive);
+  }
+
+  @Test
+  public void testDateAddScalarCol() {
+    for (VectorExpression.Type scalarType1 : dateTimestampStringTypes)
+      testDateAddScalarCol(scalarType1, true);
+
+    VectorExpression udf = null;
+    try {
+      udf = new VectorUDFDateAddScalarCol("error".getBytes("UTF-8"), 0, 1);
+    } catch (UnsupportedEncodingException e) {
+    }
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new LongColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  @Test
+  public void testDateSubScalarCol() {
+    for (VectorExpression.Type scalarType1 : dateTimestampStringTypes)
+      testDateAddScalarCol(scalarType1, false);
+
+    VectorExpression udf = null;
+    try {
+      udf = new VectorUDFDateSubScalarCol("error".getBytes("UTF-8"), 0, 1);
+    } catch (UnsupportedEncodingException e) {
+    }
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new LongColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  private void validateDateAdd(VectorizedRowBatch batch,
+                               LongColumnVector date1, LongColumnVector date2,
+                               VectorExpression.Type colType1, boolean isPositive) {
+    VectorExpression udf;
+    if (isPositive) {
+      udf = new VectorUDFDateAddColCol(0, 1, 2);
+    } else {
+      udf = new VectorUDFDateSubColCol(0, 1, 2);
+    }
+    udf.setInputTypes(colType1, VectorExpression.Type.OTHER);
+    udf.evaluate(batch);
+    BytesColumnVector output = (BytesColumnVector) batch.cols[2];
+    try {
+      for (int i = 0; i < date2.vector.length; i++) {
+        String expected;
+        if (isPositive) {
+          expected = new String(toString(date1.vector[i] + date2.vector[i]), "UTF-8");
+        } else {
+          expected = new String(toString(date1.vector[i] - date2.vector[i]), "UTF-8");
+        }
+        if (date1.isNull[i] || date2.isNull[i]) {
+          Assert.assertTrue(output.isNull[i]);
+        } else {
+          Assert.assertEquals(expected,
+              new String(output.vector[i], output.start[i], output.start[i] + output.length[i], "UTF-8"));
+        }
+      }
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private void testDateAddColCol(VectorExpression.Type colType1, boolean isPositive) {
+    LongColumnVector date1 = newRandomLongColumnVector(10000, size);
+    LongColumnVector days2 = newRandomLongColumnVector(1000, size);
+    ColumnVector col1 = castTo(date1, colType1);
+
+    BytesColumnVector output = new BytesColumnVector(size);
+
+    VectorizedRowBatch batch = new VectorizedRowBatch(3, size);
+    batch.cols[0] = col1;
+    batch.cols[1] = days2;
+    batch.cols[2] = output;
+
+    validateDateAdd(batch, date1, days2, colType1, isPositive);
+    TestVectorizedRowBatch.addRandomNulls(date1);
+    batch.cols[0] = castTo(date1, colType1);
+    validateDateAdd(batch, date1, days2, colType1, isPositive);
+    TestVectorizedRowBatch.addRandomNulls(days2);
+    batch.cols[1] = days2;
+    validateDateAdd(batch, date1, days2, colType1, isPositive);
+  }
+
+  @Test
+  public void testDateAddColCol() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes)
+      testDateAddColCol(colType1, true);
+
+    VectorExpression udf = new VectorUDFDateAddColCol(0, 1, 2);
+    VectorizedRowBatch batch = new VectorizedRowBatch(3, 1);
+    BytesColumnVector bcv;
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    batch.cols[2] = new BytesColumnVector(1);
+    bcv = (BytesColumnVector) batch.cols[0];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[2].isNull[0], true);
+  }
+
+  @Test
+  public void testDateSubColCol() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes)
+      testDateAddColCol(colType1, false);
+
+    VectorExpression udf = new VectorUDFDateSubColCol(0, 1, 2);
+    VectorizedRowBatch batch = new VectorizedRowBatch(3, 1);
+    BytesColumnVector bcv;
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    batch.cols[2] = new BytesColumnVector(1);
+    bcv = (BytesColumnVector) batch.cols[0];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[2].isNull[0], true);
+  }
+
+  private void validateDateDiff(VectorizedRowBatch batch, long scalar1,
+                                VectorExpression.Type scalarType1, VectorExpression.Type colType2,
+                                LongColumnVector date2) {
+    VectorExpression udf = null;
+    switch (scalarType1) {
+      case DATE:
+        udf = new VectorUDFDateDiffScalarCol(scalar1, 0, 1);
+        break;
+
+      case TIMESTAMP:
+        udf = new VectorUDFDateDiffScalarCol(toTimestamp(scalar1), 0, 1);
+        break;
+
+      case STRING:
+        udf = new VectorUDFDateDiffScalarCol(toString(scalar1), 0, 1);
+        break;
+    }
+
+    udf.setInputTypes(scalarType1, colType2);
+    udf.evaluate(batch);
+
+    LongColumnVector output = (LongColumnVector) batch.cols[1];
+    for (int i = 0; i < date2.vector.length; i++) {
+      Assert.assertEquals(scalar1 - date2.vector[i], output.vector[i]);
+    }
+  }
+
+  @Test
+  public void testDateDiffScalarCol() {
+    for (VectorExpression.Type scalarType1 : dateTimestampStringTypes) {
+      for (VectorExpression.Type colType2 : dateTimestampStringTypes) {
+        LongColumnVector date2 = newRandomLongColumnVector(10000, size);
+        LongColumnVector output = new LongColumnVector(size);
+        ColumnVector col2 = castTo(date2, colType2);
+        VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+        batch.cols[0] = col2;
+        batch.cols[1] = output;
+        long scalar1 = newRandom(1000);
+
+        validateDateDiff(batch, scalar1, scalarType1, colType2, date2);
+        TestVectorizedRowBatch.addRandomNulls(date2);
+        batch.cols[0] = castTo(date2, colType2);
+        validateDateDiff(batch, scalar1, scalarType1, colType2, date2);
+      }
+    }
+
+    VectorExpression udf;
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+
+    udf = new VectorUDFDateDiffScalarCol(0, 0, 1);
+    udf.setInputTypes(VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+
+    udf = new VectorUDFDateDiffScalarCol(bytes, 0, 1);
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    batch.cols[0] = new LongColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  private void validateDateDiff(VectorizedRowBatch batch, LongColumnVector date1, long scalar2,
+                                VectorExpression.Type colType1, VectorExpression.Type scalarType2) {
+    VectorExpression udf = null;
+    switch (scalarType2) {
+      case DATE:
+        udf = new VectorUDFDateDiffColScalar(0, scalar2, 1);
+        break;
+
+      case TIMESTAMP:
+        udf = new VectorUDFDateDiffColScalar(0, toTimestamp(scalar2), 1);
+        break;
+
+      case STRING:
+        udf = new VectorUDFDateDiffColScalar(0, toString(scalar2), 1);
+        break;
+    }
+
+    udf.setInputTypes(colType1, scalarType2);
+    udf.evaluate(batch);
+
+    LongColumnVector output = (LongColumnVector) batch.cols[1];
+    for (int i = 0; i < date1.vector.length; i++) {
+      Assert.assertEquals(date1.vector[i] - scalar2, output.vector[i]);
+    }
+  }
+
+  @Test
+  public void testDateDiffColScalar() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes) {
+      for (VectorExpression.Type scalarType2 : dateTimestampStringTypes) {
+        LongColumnVector date1 = newRandomLongColumnVector(10000, size);
+        LongColumnVector output = new LongColumnVector(size);
+        VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+        batch.cols[0] = castTo(date1, colType1);
+        batch.cols[1] = output;
+        long scalar2 = newRandom(1000);
+
+        validateDateDiff(batch, date1, scalar2, colType1, scalarType2);
+        TestVectorizedRowBatch.addRandomNulls(date1);
+        batch.cols[0] = castTo(date1, colType1);
+        validateDateDiff(batch, date1, scalar2, colType1, scalarType2);
+      }
+    }
+    VectorExpression udf;
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+
+    udf = new VectorUDFDateDiffColScalar(0, 0, 1);
+    udf.setInputTypes(VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+
+    udf = new VectorUDFDateDiffColScalar(0, bytes, 1);
+    udf.setInputTypes(VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING);
+    batch.cols[0] = new LongColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  private void validateDateDiff(VectorizedRowBatch batch,
+                                LongColumnVector date1, LongColumnVector date2,
+                                VectorExpression.Type colType1, VectorExpression.Type colType2) {
+    VectorExpression udf = new VectorUDFDateDiffColCol(0, 1, 2);
+    udf.setInputTypes(colType1, colType2);
+    udf.evaluate(batch);
+    LongColumnVector output = (LongColumnVector) batch.cols[2];
+    for (int i = 0; i < date1.vector.length; i++) {
+      if (date1.isNull[i] || date2.isNull[i]) {
+        Assert.assertTrue(output.isNull[i]);
+      } else {
+        Assert.assertEquals(date1.vector[i] - date2.vector[i], output.vector[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testDateDiffColCol() {
+    for (VectorExpression.Type colType1 : dateTimestampStringTypes) {
+      for (VectorExpression.Type colType2 : dateTimestampStringTypes) {
+        LongColumnVector date1 = newRandomLongColumnVector(10000, size);
+        LongColumnVector date2 = newRandomLongColumnVector(10000, size);
+        LongColumnVector output = new LongColumnVector(size);
+        VectorizedRowBatch batch = new VectorizedRowBatch(3, size);
+
+        batch.cols[0] = castTo(date1, colType1);
+        batch.cols[1] = castTo(date2, colType2);
+        batch.cols[2] = output;
+
+        validateDateDiff(batch, date1, date2, colType1, colType2);
+        TestVectorizedRowBatch.addRandomNulls(date1);
+        batch.cols[0] = castTo(date1, colType1);
+        validateDateDiff(batch, date1, date2, colType1, colType2);
+        TestVectorizedRowBatch.addRandomNulls(date2);
+        batch.cols[1] = castTo(date2, colType2);
+        validateDateDiff(batch, date1, date2, colType1, colType2);
+      }
+    }
+
+    VectorExpression udf = new VectorUDFDateDiffColCol(0, 1, 2);
+    VectorizedRowBatch batch = new VectorizedRowBatch(3, 1);
+    BytesColumnVector bcv;
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+
+    udf.setInputTypes(VectorExpression.Type.STRING, VectorExpression.Type.TIMESTAMP);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    batch.cols[2] = new LongColumnVector(1);
+    bcv = (BytesColumnVector) batch.cols[0];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[2].isNull[0], true);
+
+    udf.setInputTypes(VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING);
+    batch.cols[0] = new LongColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    batch.cols[2] = new LongColumnVector(1);
+    bcv = (BytesColumnVector) batch.cols[1];
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[2].isNull[0], true);
+  }
+
+  private void validateDate(VectorizedRowBatch batch, VectorExpression.Type colType, LongColumnVector date) {
+    VectorExpression udf;
+    if (colType == VectorExpression.Type.STRING) {
+      udf = new VectorUDFDateString(0, 1);
+    } else {
+      udf = new VectorUDFDateLong(0, 1);
+    }
+
+    udf.setInputTypes(colType);
+    udf.evaluate(batch);
+    BytesColumnVector output = (BytesColumnVector) batch.cols[1];
+
+    for (int i = 0; i < size; i++) {
+      String actual;
+      if (output.isNull[i]) {
+        actual = null;
+      } else {
+        try {
+          actual = new String(output.vector[i], output.start[i], output.length[i], "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+          throw new RuntimeException(e);
+        }
+      }
+      if (date.isNull[i]) {
+        Assert.assertTrue(output.isNull[i]);
+      } else {
+        String expected = formatter.format(new Date(DateWritable.daysToMillis((int) date.vector[i])));
+        Assert.assertEquals(expected, actual);
+      }
+    }
+  }
+
+  @Test
+  public void testDate() {
+    for (VectorExpression.Type colType : dateTimestampStringTypes) {
+      LongColumnVector date = newRandomLongColumnVector(10000, size);
+      BytesColumnVector output = new BytesColumnVector(size);
+
+      VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+      batch.cols[0] = castTo(date, colType);
+      batch.cols[1] = output;
+
+      validateDate(batch, colType, date);
+      TestVectorizedRowBatch.addRandomNulls(date);
+      batch.cols[0] = castTo(date, colType);
+      validateDate(batch, colType, date);
+    }
+
+    VectorExpression udf = new VectorUDFDateString(0, 1);
+    udf.setInputTypes(VectorExpression.Type.STRING);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new BytesColumnVector(1);
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+
+  private void validateToDate(VectorizedRowBatch batch, VectorExpression.Type colType, LongColumnVector date) {
+    VectorExpression udf;
+    if (colType == VectorExpression.Type.STRING) {
+      udf = new CastStringToDate(0, 1);
+    } else {
+      udf = new CastLongToDate(0, 1);
+    }
+    udf.setInputTypes(colType);
+    udf.evaluate(batch);
+    LongColumnVector output = (LongColumnVector) batch.cols[1];
+
+    for (int i = 0; i < size; i++) {
+      long actual = output.vector[i];
+      if (date.isNull[i]) {
+        Assert.assertTrue(output.isNull[i]);
+      } else {
+        long expected = date.vector[i];
+        Assert.assertEquals(expected, actual);
+      }
+    }
+  }
+
+  @Test
+  public void testToDate() {
+    for (VectorExpression.Type type :
+        Arrays.asList(VectorExpression.Type.TIMESTAMP, VectorExpression.Type.STRING)) {
+      LongColumnVector date = newRandomLongColumnVector(10000, size);
+      LongColumnVector output = new LongColumnVector(size);
+
+      VectorizedRowBatch batch = new VectorizedRowBatch(2, size);
+      batch.cols[0] = castTo(date, type);
+      batch.cols[1] = output;
+
+      validateToDate(batch, type, date);
+      TestVectorizedRowBatch.addRandomNulls(date);
+      batch.cols[0] = castTo(date, type);
+      validateToDate(batch, type, date);
+    }
+
+    VectorExpression udf = new CastStringToDate(0, 1);
+    udf.setInputTypes(VectorExpression.Type.STRING);
+    VectorizedRowBatch batch = new VectorizedRowBatch(2, 1);
+    batch.cols[0] = new BytesColumnVector(1);
+    batch.cols[1] = new LongColumnVector(1);
+    BytesColumnVector bcv = (BytesColumnVector) batch.cols[0];
+    byte[] bytes = new byte[0];
+    try {
+      bytes = "error".getBytes("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+    }
+    bcv.vector[0] = bytes;
+    bcv.start[0] = 0;
+    bcv.length[0] = bytes.length;
+    udf.evaluate(batch);
+    Assert.assertEquals(batch.cols[1].isNull[0], true);
+  }
+}

Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorTimestampExpressions.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorTimestampExpressions.java?rev=1573687&r1=1573686&r2=1573687&view=diff
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorTimestampExpressions.java (original)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorTimestampExpressions.java Mon Mar  3 19:28:17 2014
@@ -237,8 +237,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf = null;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFYearLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFYearString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -318,8 +320,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf = null;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFDayOfMonthLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFDayOfMonthString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -392,8 +396,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf = null;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFHourLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFHourString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -466,8 +472,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf = null;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFMinuteLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFMinuteString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -540,8 +548,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFMonthLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFMonthString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -614,8 +624,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFSecondLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFSecondString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -702,8 +714,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFUnixTimeStampLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFUnixTimeStampString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;
@@ -776,8 +790,10 @@ public class TestVectorTimestampExpressi
     VectorExpression udf;
     if (testType == TestType.LONG2) {
       udf = new VectorUDFWeekOfYearLong(0, 1);
+      udf.setInputTypes(VectorExpression.Type.TIMESTAMP);
     } else {
       udf = new VectorUDFWeekOfYearString(0, 1);
+      udf.setInputTypes(VectorExpression.Type.STRING);
     }
     udf.evaluate(batch);
     final int in = 0;

Added: hive/trunk/ql/src/test/queries/clientpositive/vectorized_date_funcs.q
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/queries/clientpositive/vectorized_date_funcs.q?rev=1573687&view=auto
==============================================================================
--- hive/trunk/ql/src/test/queries/clientpositive/vectorized_date_funcs.q (added)
+++ hive/trunk/ql/src/test/queries/clientpositive/vectorized_date_funcs.q Mon Mar  3 19:28:17 2014
@@ -0,0 +1,104 @@
+SET hive.vectorized.execution.enabled = true;
+
+-- Test timestamp functions in vectorized mode to verify they run correctly end-to-end.
+
+CREATE TABLE date_udf_flight (
+  origin_city_name STRING,
+  dest_city_name STRING,
+  fl_date DATE,
+  arr_delay FLOAT,
+  fl_num INT
+);
+LOAD DATA LOCAL INPATH '../../data/files/flights_tiny.txt.1' OVERWRITE INTO TABLE date_udf_flight;
+
+CREATE TABLE date_udf_flight_orc (
+  fl_date DATE,
+  fl_time TIMESTAMP
+) STORED AS ORC;
+
+INSERT INTO TABLE date_udf_flight_orc SELECT fl_date, to_utc_timestamp(fl_date, 'America/Los_Angeles') FROM date_udf_flight;
+
+SELECT * FROM date_udf_flight_orc;
+
+EXPLAIN SELECT
+  to_unix_timestamp(fl_time),
+  year(fl_time),
+  month(fl_time),
+  day(fl_time),
+  dayofmonth(fl_time),
+  weekofyear(fl_time),
+  date(fl_time),
+  to_date(fl_time),
+  date_add(fl_time, 2),
+  date_sub(fl_time, 2),
+  datediff(fl_time, "2000-01-01")
+FROM date_udf_flight_orc;
+
+SELECT
+  to_unix_timestamp(fl_time),
+  year(fl_time),
+  month(fl_time),
+  day(fl_time),
+  dayofmonth(fl_time),
+  weekofyear(fl_time),
+  date(fl_time),
+  to_date(fl_time),
+  date_add(fl_time, 2),
+  date_sub(fl_time, 2),
+  datediff(fl_time, "2000-01-01")
+FROM date_udf_flight_orc;
+
+EXPLAIN SELECT
+  to_unix_timestamp(fl_date),
+  year(fl_date),
+  month(fl_date),
+  day(fl_date),
+  dayofmonth(fl_date),
+  weekofyear(fl_date),
+  date(fl_date),
+  to_date(fl_date),
+  date_add(fl_date, 2),
+  date_sub(fl_date, 2),
+  datediff(fl_date, "2000-01-01")
+FROM date_udf_flight_orc;
+
+SELECT
+  to_unix_timestamp(fl_date),
+  year(fl_date),
+  month(fl_date),
+  day(fl_date),
+  dayofmonth(fl_date),
+  weekofyear(fl_date),
+  date(fl_date),
+  to_date(fl_date),
+  date_add(fl_date, 2),
+  date_sub(fl_date, 2),
+  datediff(fl_date, "2000-01-01")
+FROM date_udf_flight_orc;
+
+EXPLAIN SELECT
+  year(fl_time) = year(fl_date),
+  month(fl_time) = month(fl_date),
+  day(fl_time) = day(fl_date),
+  dayofmonth(fl_time) = dayofmonth(fl_date),
+  weekofyear(fl_time) = weekofyear(fl_date),
+  date(fl_time) = date(fl_date),
+  to_date(fl_time) = to_date(fl_date),
+  date_add(fl_time, 2) = date_add(fl_date, 2),
+  date_sub(fl_time, 2) = date_sub(fl_date, 2),
+  datediff(fl_time, "2000-01-01") = datediff(fl_date, "2000-01-01")
+FROM date_udf_flight_orc;
+
+-- Should all be true or NULL
+SELECT
+  year(fl_time) = year(fl_date),
+  month(fl_time) = month(fl_date),
+  day(fl_time) = day(fl_date),
+  dayofmonth(fl_time) = dayofmonth(fl_date),
+  weekofyear(fl_time) = weekofyear(fl_date),
+  date(fl_time) = date(fl_date),
+  to_date(fl_time) = to_date(fl_date),
+  date_add(fl_time, 2) = date_add(fl_date, 2),
+  date_sub(fl_time, 2) = date_sub(fl_date, 2),
+  datediff(fl_time, "2000-01-01") = datediff(fl_date, "2000-01-01")
+FROM date_udf_flight_orc;



Mime
View raw message