amansinha100 closed pull request #1496: DRILL-6783: CAST string literal as 
INTERVAL MONTH/YEAR works inconsis…
URL: https://github.com/apache/drill/pull/1496
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastFunctions.java
 
b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastFunctions.java
index cb0d0225b23..7768909c874 100644
--- 
a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastFunctions.java
+++ 
b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastFunctions.java
@@ -19,14 +19,21 @@
 
 import java.math.BigDecimal;
 import java.time.LocalDate;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.drill.categories.SqlFunctionTest;
 import org.apache.drill.categories.UnlikelyTest;
 import org.apache.drill.common.exceptions.UserRemoteException;
+import org.apache.drill.common.expression.SchemaPath;
 import org.apache.drill.exec.planner.physical.PlannerSettings;
+import org.apache.drill.exec.record.RecordBatchLoader;
+import org.apache.drill.exec.rpc.user.QueryDataBatch;
+import org.apache.drill.exec.vector.IntervalYearVector;
 import org.apache.drill.test.BaseTestQuery;
+import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -39,6 +46,7 @@
 import mockit.integration.junit4.JMockit;
 
 import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.hasItem;
 
 @RunWith(JMockit.class)
 @Category({UnlikelyTest.class, SqlFunctionTest.class})
@@ -664,4 +672,32 @@ public void testCastVarCharDecimalOverflow() throws 
Exception {
 
     test(query);
   }
-}
\ No newline at end of file
+
+  @Test // DRILL-6783
+  public void testCastVarCharIntervalYear() throws Exception {
+    String query = "select cast('P31M' as interval month) as i from 
cp.`employee.json` limit 10";
+    List<QueryDataBatch> result = testSqlWithResults(query);
+    RecordBatchLoader loader = new 
RecordBatchLoader(getDrillbitContext().getAllocator());
+
+    QueryDataBatch b = result.get(0);
+    loader.load(b.getHeader().getDef(), b.getData());
+
+    IntervalYearVector vector = (IntervalYearVector) 
loader.getValueAccessorById(
+          IntervalYearVector.class,
+          
loader.getValueVectorId(SchemaPath.getCompoundPath("i")).getFieldIds())
+        .getValueVector();
+
+    Set<String> resultSet = new HashSet<>();
+    for (int i = 0; i < loader.getRecordCount(); i++) {
+      String displayValue = 
vector.getAccessor().getAsStringBuilder(i).toString();
+      resultSet.add(displayValue);
+    }
+
+    Assert.assertEquals(
+        "Casting literal string as INTERVAL should yield the same result for 
each row", 1, resultSet.size());
+    Assert.assertThat(resultSet, hasItem("2 years 7 months"));
+
+    b.release();
+    loader.clear();
+  }
+}
diff --git a/exec/vector/src/main/codegen/templates/FixedValueVectors.java 
b/exec/vector/src/main/codegen/templates/FixedValueVectors.java
index a98aa66b6c7..508e4844d89 100644
--- a/exec/vector/src/main/codegen/templates/FixedValueVectors.java
+++ b/exec/vector/src/main/codegen/templates/FixedValueVectors.java
@@ -532,7 +532,8 @@ public long getTwoAsLong(int index) {
     }
 
     public StringBuilder getAsStringBuilder(int index) {
-      return DateUtilities.intervalYearStringBuilder(data.getInt(index));
+      int value = get(index);
+      return DateUtilities.intervalYearStringBuilder(value);
     }
     <#elseif minor.class == "Time">
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to