[
https://issues.apache.org/jira/browse/CALCITE-6764?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17912713#comment-17912713
]
Mihai Budiu commented on CALCITE-6764:
--------------------------------------
I am not sure whether this problem can be fixed without a breaking change.
The problem is that the RelDataTypeFactory refuses to create nullable struct
types.
There's this comment in the factory in the createTypeWithNullability method,
which I think really indicates a design bug:
```
// REVIEW: angel 18-Aug-2005 dtbug 336 workaround
// Changed to ignore nullable parameter if nullable is false since
// copyRecordType implementation is doubtful
// - If nullable -> Do a deep copy, setting all fields of the record type
// to be nullable regardless of initial nullability.
// - If not nullable -> Do a deep copy, setting not nullable at top
RelRecordType
// level only, keeping its fields' nullability as before.
// According to the SQL standard, nullability for struct types can be
defined only for
// columns, which translates to top level structs. Nested struct
attributes are always
// nullable, so in principle we could always set the nested attributes to
be nullable.
// However, this might create regressions so we will not do it and we
will keep previous
// behavior.
```
The factory should create whatever types it is required to create. It
implements a mechanism.
The policy whether ROW types can be nullable or not is a policy, which should
be left to upper software layers.
Moreover, Calcite definitely supports specifying nullability for each ROW
field, even if the field is not a column.
So a ROW type can have arbitrary field nullability, including for nested ROW
types.
The grammar allows specifying nullability for row fields.
> Field access from a nullable ROW should be nullable
> ---------------------------------------------------
>
> Key: CALCITE-6764
> URL: https://issues.apache.org/jira/browse/CALCITE-6764
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.38.0
> Reporter: Mihai Budiu
> Assignee: Mihai Budiu
> Priority: Minor
> Labels: pull-request-available
>
> Consider the following SQL:
> {code:sql}
> CREATE TABLE T(p MAP<VARCHAR, ROW(k VARCHAR, v VARCHAR)>);
> SELECT p['a'].k FROM T;
> {code}
> The MAP values are ROW values, which can be null.
> The type of p['a'] is nullable, so p['a'].k should also be nullable.
> The validator will crash while validating this query with the following error:
> {code}
> java.lang.RuntimeException: java.lang.AssertionError: Conversion to
> relational algebra failed to preserve datatypes:
> validated type:
> RecordType(VARCHAR EXPR$0) NOT NULL
> converted type:
> RecordType(VARCHAR NOT NULL EXPR$0) NOT NULL
> rel:
> LogicalProject(EXPR$0=[ITEM($0, 'a').k])
> LogicalTableScan(table=[[schema, t]])
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)