[ 
https://issues.apache.org/jira/browse/DRILL-6768?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16656651#comment-16656651
 ] 

ASF GitHub Bot commented on DRILL-6768:
---------------------------------------

vdiravka commented on a change in pull request #1494: DRILL-6768: Improve 
to_date, to_time and to_timestamp and correspondi…
URL: https://github.com/apache/drill/pull/1494#discussion_r226346120
 
 

 ##########
 File path: 
exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestCastEmptyStrings.java
 ##########
 @@ -17,111 +17,116 @@
  */
 package org.apache.drill.exec.fn.impl;
 
-import org.apache.drill.test.BaseTestQuery;
+import org.apache.drill.exec.ExecConstants;
 import org.apache.drill.categories.SqlFunctionTest;
 import org.apache.drill.categories.UnlikelyTest;
 import org.apache.drill.exec.planner.physical.PlannerSettings;
-import org.junit.AfterClass;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterFixtureBuilder;
+import org.apache.drill.test.ClusterTest;
+import org.apache.drill.test.TestBuilder;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+
 @Category({UnlikelyTest.class, SqlFunctionTest.class})
-public class TestCastEmptyStrings extends BaseTestQuery {
-  // enable decimal data type
-  @BeforeClass
-  public static void enableDecimalDataType() throws Exception {
-    test("alter session set `%s` = true", 
PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY);
-  }
+public class TestCastEmptyStrings extends ClusterTest {
 
-  @AfterClass
-  public static void disableDecimalDataType() throws Exception {
-    test("alter session set `%s` = false", 
PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY);
+  @BeforeClass
+  public static void setup() throws Exception {
+    ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher)
+        // enable decimal data type
+        .sessionOption(PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY, true)
+        // Enable the new cast functions (cast empty string "" to null)
+        .systemOption(ExecConstants.CAST_EMPTY_STRING_TO_NULL, true);
+    startCluster(builder);
   }
 
   @Test // see DRILL-1874
-  public void testCastInputTypeNullableVarCharToNumeric() throws Exception {
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-
-    // Test Optional VarChar
-    test("select cast(columns[0] as int) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as bigint) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as float) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as double) from cp.`emptyStrings.csv`");
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastOptionalVarCharToNumeric() throws Exception {
+    testCastOptionalString("columns[0]", "int", "cp.`emptyStrings.csv`", null, 
1, 2);
+    testCastOptionalString("columns[0]", "bigint", "cp.`emptyStrings.csv`", 
null, 1L, 2L);
+    testCastOptionalString("columns[0]", "float", "cp.`emptyStrings.csv`", 
null, 1.0f, 2.0f);
+    testCastOptionalString("columns[0]", "double", "cp.`emptyStrings.csv`", 
null, 1.0, 2.0);
   }
 
   @Test // see DRILL-1874
-  public void testCastInputTypeNonNullableVarCharToNumeric() throws Exception {
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-    // Test Required VarChar
-    test("select cast('' as int) from cp.`emptyStrings.csv`");
-    test("select cast('' as bigint) from cp.`emptyStrings.csv`");
-    test("select cast('' as float) from cp.`emptyStrings.csv`");
-    test("select cast('' as double) from cp.`emptyStrings.csv`");
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastRequiredVarCharToNumeric() throws Exception {
+    testCastEmptyString("int");
+    testCastEmptyString("bigint");
+    testCastEmptyString("float");
+    testCastEmptyString("double");
   }
 
   @Test // see DRILL-1874
-  public void testCastInputTypeNullableVarCharToDecimal() throws Exception {
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-
-    // Test Optional VarChar
-    test("select cast(columns[0] as decimal) from cp.`emptyStrings.csv` where 
cast(columns[0] as decimal) is null");
-    test("select cast(columns[0] as decimal(9)) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as decimal(18)) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as decimal(28)) from cp.`emptyStrings.csv`");
-    test("select cast(columns[0] as decimal(38)) from cp.`emptyStrings.csv`");
-
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastOptionalVarCharToDecimal() throws Exception {
+    BigDecimal one = BigDecimal.valueOf(1L);
+    BigDecimal two = BigDecimal.valueOf(2L);
+    testCastOptionalString("columns[0]", "decimal", "cp.`emptyStrings.csv`", 
null, one, two);
+    testCastOptionalString("columns[0]", "decimal(9)", 
"cp.`emptyStrings.csv`", null, one, two);
+    testCastOptionalString("columns[0]", "decimal(18)", 
"cp.`emptyStrings.csv`", null, one, two);
+    testCastOptionalString("columns[0]", "decimal(28)", 
"cp.`emptyStrings.csv`", null, one, two);
+    testCastOptionalString("columns[0]", "decimal(38)", 
"cp.`emptyStrings.csv`", null, one, two);
   }
 
   @Test // see DRILL-1874
-  public void testCastInputTypeNonNullableVarCharToDecimal() throws Exception {
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-
-    // Test Required VarChar
-    test("select cast('' as decimal) from cp.`emptyStrings.csv` where cast('' 
as decimal) is null");
-    test("select cast('' as decimal(18)) from cp.`emptyStrings.csv`");
-    test("select cast('' as decimal(28)) from cp.`emptyStrings.csv`");
-    test("select cast('' as decimal(38)) from cp.`emptyStrings.csv`");
-
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastRequiredVarCharToDecimal() throws Exception {
+    testCastEmptyString("decimal");
+    testCastEmptyString("decimal(18)");
+    testCastEmptyString("decimal(28)");
+    testCastEmptyString("decimal(38)");
   }
 
   @Test
-  public void testCastInputTypeNonNullableVarCharToDateTime() throws Exception 
{
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-    // Test Required VarChar
-    test("select cast('' as date) from cp.`emptyStrings.csv`");
-    test("select cast('' as time) from cp.`emptyStrings.csv`");
-    test("select cast('' as timestamp) from cp.`emptyStrings.csv`");
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastRequiredVarCharToDateTime() throws Exception {
+    testCastEmptyString("date");
+    testCastEmptyString("time");
+    testCastEmptyString("timestamp");
   }
 
   @Test
-  public void testCastInputTypeNullableVarCharToDateTime() throws Exception {
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-    // Test Optional VarChar
-    test("select cast(dateCol as date) from cp.`dateWithEmptyStrings.json`");
-    test("select cast(timeCol as time) from cp.`dateWithEmptyStrings.json`");
-    test("select cast(timestampCol as timestamp) from 
cp.`dateWithEmptyStrings.json`");
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastOptionalVarCharToDateTime() throws Exception {
+    testCastOptionalString("dateCol", "date", "cp.`dateWithEmptyStrings.json`",
+        null, null, LocalDate.of(1997, 12, 10));
+    testCastOptionalString("timeCol", "time", "cp.`dateWithEmptyStrings.json`",
+        null, null, LocalTime.of(7, 21, 39));
+    testCastOptionalString("timestampCol", "timestamp", 
"cp.`dateWithEmptyStrings.json`",
+        null, null, LocalDateTime.of(2003, 9, 11, 10, 1, 37));
   }
 
   @Test
-  public void testCastInputTypeNonNullableVarCharToInterval() throws Exception 
{
-    // Enable the new cast functions (cast empty string "" to null)
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
true;");
-    // Test Required VarChar
-    test("select cast('' as interval year) from cp.`emptyStrings.csv`");
-    test("select cast('' as interval day) from cp.`emptyStrings.csv`");
-    test("alter system set `drill.exec.functions.cast_empty_string_to_null` = 
false;");
+  public void testCastRequiredVarCharToInterval() throws Exception {
+    testCastEmptyString("interval year");
+    testCastEmptyString("interval day");
+    testCastEmptyString("interval month");
+  }
+
+  private void testCastOptionalString(String column, String asType, String 
table,
+                                      Object... baselineValues) throws 
Exception {
+    String query = String.format("select cast(%s as %s) c from %s", column, 
asType, table);
+    TestBuilder testBuilder = testBuilder()
+        .sqlQuery(query)
+        .unOrdered()
+        .baselineColumns("c");
+    for (Object value : baselineValues) {
 
 Review comment:
   consider introducing the new method in the `TestBuilder` class with:
   ```
   Arrays.stream(baselineValues)
           .forEach(testBuilder::baselineValues);
   ```

----------------------------------------------------------------
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]


> Improve to_date, to_time and to_timestamp and corresponding cast functions to 
> handle empty string when `drill.exec.functions.cast_empty_string_to_null` 
> option is enabled
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: DRILL-6768
>                 URL: https://issues.apache.org/jira/browse/DRILL-6768
>             Project: Apache Drill
>          Issue Type: Improvement
>    Affects Versions: 1.14.0
>            Reporter: Bohdan Kazydub
>            Assignee: Bohdan Kazydub
>            Priority: Major
>              Labels: doc-impacting
>             Fix For: 1.15.0
>
>
> When `drill.exec.functions.cast_empty_string_to_null` option is enabled
> `to_date`, `to_time` and `to_timestamp` functions while converting string to 
> according type in case if null or empty string values are passed will return 
> NULL (to avoid CASE clauses which are littering a query and will work in 
> accordance with their respective CAST counterparts) for both cases.
>  
>   
>   
> CASTs will  be handled in a similar way (uniformly with numeric types):
>  
> ||Value to cast||Now||Will be||
> |NULL|NULL|NULL|
> |'' (empty string)|Error in many cases (except numerical types)|NULL|
>  CAST empty string to null (in case of enabled option) will be supported by 
> DATE, TIME, TIMESTAMP, INTERVAL YEAR, INTERVAL MONTH and INTERVAL DAY 
> functions in addition to numeric types.
>  
> *For documentation*
> TBA



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to