Skip to content

Commit

Permalink
DRILL-6811: Fix type inference to return correct data mode for boolea…
Browse files Browse the repository at this point in the history
…n functions

closes #1510
  • Loading branch information
vvysotskyi authored and Ben-Zvi committed Oct 27, 2018
1 parent 7571d52 commit f39c772
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Expand Up @@ -284,16 +284,21 @@ public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
// 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),
Expand Down
Expand Up @@ -24,11 +24,16 @@
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;
Expand Down Expand Up @@ -977,4 +982,30 @@ public void testNegate() throws Exception {
.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();
}
}
}
Expand Up @@ -235,7 +235,7 @@ public void testIsNull() throws Exception {
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));

Expand Down

0 comments on commit f39c772

Please sign in to comment.