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

Krisztian Kasa commented on CALCITE-6453:
-----------------------------------------

Hi [~nobigo],

Thanks for the review.
I bumped into this when I was searching for the cause of a StackOverflow in the 
Hive compiler running this query:
[https://github.com/apache/hive/blob/b15ce9126dcc596c953fe202e6834cebdfca6080/ql/src/test/queries/clientpositive/cardinality_preserving_join_opt2.q#L81-L92]
 using Calcite 1.33.0

The predicate
{code:java}
(cm.c5 is null or cm.c5 > date_format(cast('2020-10-30' as date),'yyyy-MM-01'))
{code}
is represented as
{code:java}
OR(IS NULL($4), >($4, 
CAST(date_format(CAST(_UTF-16LE'2020-10-30':VARCHAR(2147483647) CHARACTER SET 
"UTF-16LE"):DATE, _UTF-16LE'yyyy-MM-01':VARCHAR(2147483647) CHARACTER SET 
"UTF-16LE")):DATE))
{code}
The first RexSimplify.simplify() call transforms it to
{code:java}
OR(IS NULL($4), >($4, CAST(date_format(CAST(2020-10-30:DATE):DATE, 
_UTF-16LE'yyyy-MM-01':VARCHAR(2147483647) CHARACTER SET "UTF-16LE")):DATE))
{code}
The second call simplifies it further to
{code:java}
OR(IS NULL($4), >($4, 
CAST(date_format(2020-10-30:DATE,_UTF-16LE'yyyy-MM-01':VARCHAR(2147483647) 
CHARACTER SET "UTF-16LE")):DATE))
{code}
The goal of this jira is to do the simplification in one step and end up with 
an expression which can not be simplified further with subsequent simplify 
calls.

> Simplify casts which are result of constant reduction
> -----------------------------------------------------
>
>                 Key: CALCITE-6453
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6453
>             Project: Calcite
>          Issue Type: Improvement
>            Reporter: Krisztian Kasa
>            Assignee: Krisztian Kasa
>            Priority: Major
>              Labels: pull-request-available
>
> RexSimplify transforms nullable cast expressions with a non-nullable literal 
> operand which type is different from the cast type to another cast expression 
> with a literal operand with the same type as the cast.
> If simplify is called again using the result cast expression of the first 
> simplification we get a literal only as a result.
> Example:
> Initial expression
> {code:java}
> CAST(_UTF-16LE'2020-10-30':VARCHAR(2147483647) CHARACTER SET "UTF-16LE"):DATE
> {code}
> where the operand is non-nullable varchar and the result of the cast is 
> nullable date.
> After the first simplify call we get
> {code:java}
> CAST(2020-10-30:DATE):DATE
> {code}
> where the operand type is non-nullable date and the cast type is nullable 
> date.
> https://github.com/apache/calcite/blob/8ab0b03326730aa2cc6b476b2cbd8f99799bdacb/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L2269-L2276
> After the second simplify call we get
> {code:java}
> 2020-10-30:DATE
> {code}
> where the literal type is non-nullable date
> https://github.com/apache/calcite/blob/8ab0b03326730aa2cc6b476b2cbd8f99799bdacb/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L2195-L2196



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to