[ 
https://issues.apache.org/jira/browse/CALCITE-6764?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17910890#comment-17910890
 ] 

Mihai Budiu edited comment on CALCITE-6764 at 1/8/25 2:24 AM:
--------------------------------------------------------------

Fixing this is relatively subtle.

The main question is: can a value with a ROW or user-defined struct type be 
nullable?

One would think that the answer is "yes", since NULLs are so pervasive in SQL. 
But the ROW type seems to have a very strange definition, especially with 
respect to NULL.

In case structs can be NULL, the second question is "what is the semantics of 
accessing a field of a struct which is NULL?"

There are two possible answers:
 * a runtime error
 * the result is NULL

In the former case we can assume that the field access has the same type as the 
struct field.

In the latter case, the field access must have a nullable type, even if the 
struct does not have nullable fields.

I will submit a fix which assumes that a field access in a nullable struct 
always has a nullable type.


was (Author: JIRAUSER295926):
Fixing this is relatively subtle.

The main question is: when a can a value with a ROW or user-defined struct type 
be nullable?

One would think that the answer is "yes", since NULLs are so pervasive in SQL. 
But the ROW type seems to have a very strange definition, especially with 
respect to NULL.

In case structs can be NULL, the second question is "what is the semantics of 
accessing a field of a struct which is NULL?

There are two possible answers:
 * a runtime error
 * the result is NULL

In the former case we can assume that the field access has the same type as the 
struct field.

In the latter case, the field access has a nullable type, even if the struct 
does not have a nullable type.

I will submit a fix which assumes that a field access in a nullable struct 
always has a nullable type.

> 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)

Reply via email to