Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Jan 14, 2025
1 parent 8cddd87 commit a69d5fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7085,6 +7085,9 @@ private class DeriveTypeVisitor implements SqlVisitor<RelDataType> {
type = field.getType();
if (recordIsNullable) {
// If parent record is nullable, field must also be nullable.
// Consider CREATE TABLE T(p ROW(k VARCHAR) NULL);
// Since T.p is nullable, T.p.k also has to be nullable, even if
// the type of k itself is not nullable.
type = getTypeFactory().createTypeWithNullability(type, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class TableInRootSchemaTest {
connection.close();
}

/** Represents a table with no data. An abstract base class,
/** Represents a table with no data. An abstract base class,
* derived classes need to define the schema. */
private abstract static class EmptyTable extends AbstractQueryableTable {
protected EmptyTable() {
Expand Down Expand Up @@ -119,8 +119,8 @@ protected EmptyTable() {
}

/** Helper class for the test for [CALCITE-6764] below. */
private static class RowTable extends EmptyTable {
protected RowTable() {
private static class TableWithNullableRowInMap extends EmptyTable {
protected TableWithNullableRowInMap() {
super();
}

Expand All @@ -143,8 +143,8 @@ protected RowTable() {
}

/** Helper class for the test for [CALCITE-6764] below. */
private static class RowTable2 extends EmptyTable {
protected RowTable2() {
private static class TableWithNullableRowToplevel extends EmptyTable {
protected TableWithNullableRowToplevel() {
super();
}

Expand All @@ -163,26 +163,36 @@ protected RowTable2() {
}
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6764">[CALCITE-6764]
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6764">[CALCITE-6764]
* Field access from a nullable ROW should be nullable</a>. */
@Test void testNullableValue() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
calciteConnection.getRootSchema().add("T", new RowTable());
calciteConnection.getRootSchema().add("T", new TableWithNullableRowInMap());
Statement statement = calciteConnection.createStatement();
// Without the fix to this issue the Validator crashes with an AssertionFailure:
// java.lang.RuntimeException: java.lang.AssertionError:
// Conversion to relational algebra failed to preserve datatypes:
// validated type:
ResultSet resultSet = statement.executeQuery("SELECT P['a'].K FROM T");
resultSet.close();
statement.close();
connection.close();
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6764">[CALCITE-6764]
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6764">[CALCITE-6764]
* Field access from a nullable ROW should be nullable</a>. */
@Test void testNullableValue2() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
calciteConnection.getRootSchema().add("T", new RowTable2());
calciteConnection.getRootSchema().add("T", new TableWithNullableRowToplevel());
Statement statement = calciteConnection.createStatement();
// Without the fix to this issue the Validator crashes with an AssertionFailure:
// java.lang.RuntimeException: java.lang.AssertionError:
// Conversion to relational algebra failed to preserve datatypes:
// validated type:
ResultSet resultSet = statement.executeQuery("SELECT t.p.k FROM T");
resultSet.close();
statement.close();
Expand Down

0 comments on commit a69d5fe

Please sign in to comment.