Skip to content

Commit

Permalink
DRILL-2342: Store the nullability property of column in view persiste…
Browse files Browse the repository at this point in the history
…nce store.
  • Loading branch information
vkorukanti committed Mar 20, 2015
1 parent 9c9ee8c commit d7dc0b9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Expand Up @@ -52,13 +52,23 @@ public static class FieldType {
public final SqlTypeName type;
public final Integer precision;
public final Integer scale;
public final Boolean isNullable;

@JsonCreator
public FieldType(@JsonProperty("name") String name, @JsonProperty("type") SqlTypeName type, @JsonProperty("precision") Integer precision, @JsonProperty("scale") Integer scale){
public FieldType(
@JsonProperty("name") String name,
@JsonProperty("type") SqlTypeName type,
@JsonProperty("precision") Integer precision,
@JsonProperty("scale") Integer scale,
@JsonProperty("isNullable") Boolean isNullable){
this.name = name;
this.type = type;
this.precision = precision;
this.scale = scale;

// Property "isNullable" is not part of the initial view definition and added in DRILL-2342. If the
// default value is null, consider it as "true". It is safe to default to "nullable" than "required" type.
this.isNullable = (isNullable == null) ? true : isNullable;
}

public FieldType(String name, RelDataType dataType){
Expand All @@ -82,6 +92,7 @@ public FieldType(String name, RelDataType dataType){

this.precision = p;
this.scale = s;
this.isNullable = dataType.isNullable();
}
}

Expand Down Expand Up @@ -120,12 +131,19 @@ public RelDataType getRowType(RelDataTypeFactory factory){

for(FieldType field : fields){
names.add(field.name);
RelDataType type;
if(field.precision == null && field.scale == null){
types.add(factory.createSqlType(field.type));
type = factory.createSqlType(field.type);
}else if(field.precision != null && field.scale == null){
types.add(factory.createSqlType(field.type, field.precision));
type = factory.createSqlType(field.type, field.precision);
}else{
types.add(factory.createSqlType(field.type, field.precision, field.scale));
type = factory.createSqlType(field.type, field.precision, field.scale);
}

if (field.isNullable) {
types.add(factory.createTypeWithNullability(type, true));
} else {
types.add(type);
}
}
return factory.createStructType(types, names);
Expand Down
Expand Up @@ -55,8 +55,8 @@ public void testDRILL_FilterMerge() throws Exception {
"group by dat.store_id\n" +
"order by dat.store_id";

String expectedPattern1 = "Filter(condition=[AND(OR(=($0, 1), =($0, 2), =($0, 3)), =(CAST($4):ANY NOT NULL, 'GRADUATE DEGREE'))])";
String expectedPattern2 = "Filter(condition=[AND(OR(=($0, 1), =($0, 2), =($0, 3)), LIKE(CAST($1):ANY NOT NULL, '%VP%'))])";
String expectedPattern1 = "Filter(condition=[AND(OR(=($0, 1), =($0, 2), =($0, 3)), =($4, 'GRADUATE DEGREE'))])";
String expectedPattern2 = "Filter(condition=[AND(OR(=($0, 1), =($0, 2), =($0, 3)), LIKE($1, '%VP%'))])";
String excludedPattern = "Filter(condition=[OR(=($0, 1), =($0, 2), =($0, 3))])";

test("use dfs.tmp");
Expand Down
Expand Up @@ -40,5 +40,41 @@ public void referToSchemaInsideAndOutsideView() throws Exception {
test(selectOutside);
}

/**
* DRILL-2342 This test is for case where output columns are nullable. Existing tests already cover the case
* where columns are required.
*/
@Test
public void testNullabilityPropertyInViewPersistence() throws Exception {
final String viewName = "testNullabilityPropertyInViewPersistence";
try {

test("USE dfs_test.tmp");
test(String.format("CREATE OR REPLACE VIEW %s AS SELECT " +
"CAST(customer_id AS BIGINT) as cust_id, " +
"CAST(fname AS VARCHAR(25)) as fname, " +
"CAST(country AS VARCHAR(20)) as country " +
"FROM cp.`customer.json` " +
"ORDER BY customer_id " +
"LIMIT 1;", viewName));

testBuilder()
.sqlQuery(String.format("DESCRIBE %s", viewName))
.unOrdered()
.baselineColumns("COLUMN_NAME", "DATA_TYPE", "IS_NULLABLE")
.baselineValues("cust_id", "BIGINT", "YES")
.baselineValues("fname", "VARCHAR", "YES")
.baselineValues("country", "VARCHAR", "YES")
.go();

testBuilder()
.sqlQuery(String.format("SELECT * FROM %s", viewName))
.ordered()
.baselineColumns("cust_id", "fname", "country")
.baselineValues(1L, "Sheri", "Mexico")
.go();
} finally {
test("drop view " + viewName + ";");
}
}
}
Expand Up @@ -228,7 +228,7 @@ public Void apply(Connection connection) {
resultSet = statement.executeQuery("DESCRIBE `TABLES`");
Set<String> result = JdbcAssert.toStringSet(resultSet);
resultSet.close();
ImmutableSet<String> expected = ImmutableSet.of("COLUMN_NAME=key; DATA_TYPE=INTEGER; IS_NULLABLE=NO");
ImmutableSet<String> expected = ImmutableSet.of("COLUMN_NAME=key; DATA_TYPE=INTEGER; IS_NULLABLE=YES");
assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result, expected), expected.equals(result));

// Test describe of `TABLES` with a schema qualifier which is not in default schema
Expand Down
Expand Up @@ -350,8 +350,8 @@ public Void apply(Connection connection) {
result = JdbcAssert.toString(resultSet).trim();
resultSet.close();
expected =
"COLUMN_NAME=key; DATA_TYPE=INTEGER; IS_NULLABLE=NO\n" +
"COLUMN_NAME=value; DATA_TYPE=VARCHAR; IS_NULLABLE=NO";
"COLUMN_NAME=key; DATA_TYPE=INTEGER; IS_NULLABLE=YES\n" +
"COLUMN_NAME=value; DATA_TYPE=VARCHAR; IS_NULLABLE=YES";
assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result, expected),
expected.equals(result));

Expand Down

0 comments on commit d7dc0b9

Please sign in to comment.