You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@drill.apache.org by bo...@apache.org on 2018/10/28 19:46:28 UTC

[drill] 01/02: DRILL-6811: Fix type inference to return correct data mode for boolean functions

This is an automated email from the ASF dual-hosted git repository.

boaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/drill.git

commit f39c7724cbbbe04a53ce98b74dfada46b37ecb0d
Author: Volodymyr Vysotskyi <vv...@gmail.com>
AuthorDate: Wed Oct 24 12:57:24 2018 +0300

    DRILL-6811: Fix type inference to return correct data mode for boolean functions
    
    closes #1510
---
 .../drill/exec/planner/sql/TypeInferenceUtils.java |  9 +++++--
 .../java/org/apache/drill/TestFunctionsQuery.java  | 31 ++++++++++++++++++++++
 .../drill/TestFunctionsWithTypeExpoQueries.java    |  2 +-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
index 031da3e..016473c 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
@@ -284,16 +284,21 @@ public class TypeInferenceUtils {
           // In summary, if we have a boolean output function in the WHERE-CLAUSE,
           // this logic can validate and execute user queries seamlessly
           boolean allBooleanOutput = true;
+          boolean isNullable = false;
           for (DrillFuncHolder function : functions) {
             if (function.getReturnType().getMinorType() != TypeProtos.MinorType.BIT) {
               allBooleanOutput = false;
               break;
             }
+            if (function.getReturnType().getMode() == TypeProtos.DataMode.OPTIONAL
+                || function.getNullHandling() == FunctionTemplate.NullHandling.NULL_IF_NULL) {
+              isNullable = true;
+            }
           }
 
-          if(allBooleanOutput) {
+          if (allBooleanOutput) {
             return factory.createTypeWithNullability(
-                factory.createSqlType(SqlTypeName.BOOLEAN), true);
+                factory.createSqlType(SqlTypeName.BOOLEAN), isNullable);
           } else {
             return factory.createTypeWithNullability(
                 factory.createSqlType(SqlTypeName.ANY),
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
index 719df6f..bf1f4aa 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
@@ -24,11 +24,16 @@ import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.drill.categories.SqlFunctionTest;
 import org.apache.drill.common.exceptions.UserRemoteException;
+import org.apache.drill.common.types.TypeProtos;
 import org.apache.drill.exec.planner.physical.PlannerSettings;
+import org.apache.drill.exec.record.BatchSchema;
 import org.apache.drill.test.BaseTestQuery;
+import org.apache.drill.test.rowSet.schema.SchemaBuilder;
 import org.hamcrest.CoreMatchers;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -977,4 +982,30 @@ public class TestFunctionsQuery extends BaseTestQuery {
             .baselineValues(new BigDecimal("-1.1"))
             .go();
   }
+
+  @Test
+  public void testBooleanConditionsMode() throws Exception {
+    List<String> conditions = Arrays.asList(
+        "employee_id IS NULL",
+        "employee_id IS NOT NULL",
+        "employee_id > 0 IS TRUE",
+        "employee_id > 0 IS NOT TRUE",
+        "employee_id > 0 IS FALSE",
+        "employee_id > 0 IS NOT FALSE",
+        "employee_id IS NULL OR position_id IS NULL",
+        "employee_id IS NULL AND position_id IS NULL",
+        "isdate(employee_id)",
+        "NOT (employee_id IS NULL)");
+
+    BatchSchema expectedSchema = new SchemaBuilder()
+        .add("col1", TypeProtos.MinorType.BIT)
+        .build();
+
+    for (String condition : conditions) {
+      testBuilder()
+          .sqlQuery("SELECT %s AS col1 FROM cp.`employee.json` LIMIT 0", condition)
+          .schemaBaseLine(expectedSchema)
+          .go();
+    }
+  }
 }
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsWithTypeExpoQueries.java b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsWithTypeExpoQueries.java
index 08b15fc..207638d 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsWithTypeExpoQueries.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsWithTypeExpoQueries.java
@@ -235,7 +235,7 @@ public class TestFunctionsWithTypeExpoQueries extends BaseTestQuery {
     List<Pair<SchemaPath, TypeProtos.MajorType>> expectedSchema = Lists.newArrayList();
     TypeProtos.MajorType majorType = TypeProtos.MajorType.newBuilder()
         .setMinorType(TypeProtos.MinorType.BIT)
-        .setMode(TypeProtos.DataMode.OPTIONAL)
+        .setMode(TypeProtos.DataMode.REQUIRED)
         .build();
     expectedSchema.add(Pair.of(SchemaPath.getSimplePath("col"), majorType));