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

Vova Vysotskyi commented on CALCITE-3457:
-----------------------------------------

Thanks for your suggestions.
ITEM expression for the array is already derived as nullable, but the issue 
here is connected with the code where strong policy is used 
({{RexSimplify.simplifyIsNotNull()}}):
{code:java}
    switch (Strong.policy(a.getKind())) {
    case NOT_NULL:
      return rexBuilder.makeLiteral(true);
    case ANY:
      // "f" is a strong operator, so "f(operand0, operand1) IS NOT NULL"
      // simplifies to "operand0 IS NOT NULL AND operand1 IS NOT NULL"
      final List<RexNode> operands = new ArrayList<>();
      for (RexNode operand : ((RexCall) a).getOperands()) {
        final RexNode simplified = simplifyIsNotNull(operand);
        if (simplified == null) {
          operands.add(
              rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, operand));
        } else if (simplified.isAlwaysFalse()) {
          return rexBuilder.makeLiteral(false);
        } else {
          operands.add(simplified);
        }
      }
{code}

But this code is also applied for ITEM call since it has {{ANY}} policy.

> RexSimplify incorrectly simplifies IS NOT NULL operator with ITEM call
> ----------------------------------------------------------------------
>
>                 Key: CALCITE-3457
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3457
>             Project: Calcite
>          Issue Type: Bug
>    Affects Versions: 1.22.0
>            Reporter: Vova Vysotskyi
>            Priority: Major
>             Fix For: 1.22.0
>
>
> In CALCITE-3390 ITEM was marked with {{Policy.ANY}} strong policy, but 
> according to its JavaDoc, the result may be null if and only if at least one 
> of its arguments is null. This statement was used in 
> {{RexSimplify.simplifyIsNotNull()}} method, so {{t1.c_nationkey[0] is not 
> null}} will be simplified to {{IS NOT NULL($0)}} which is wrong, since array 
> may be empty, or index may be less than the size of the array.
> Unit test which helps to reproduce this issue:
> {noformat}
>   @Test public void testSimplifyItemIsNotNull() {
>     String query = "select * from sales.customer as t1 where 
> t1.c_nationkey[0] is not null";
>     sql(query)
>         .withTester(t -> createDynamicTester())
>         .withRule(ReduceExpressionsRule.FILTER_INSTANCE)
>         .check();
>   }
> {noformat}
> Returns plan with incorrectly simplified ITEM expression:
> {noformat}
> LogicalProject(**=[$1])
>   LogicalFilter(condition=[IS NOT NULL($0)])
>     LogicalTableScan(table=[[CATALOG, SALES, CUSTOMER]])
> {noformat}
> But the initial intention of CALCITE-3390 was to allow pushing ITEM 
> expression to the right input of left-outer-join.
> I propose to add a new element to the {{Policy}} which will have a relaxed 
> condition - expression is null if at least one of its arguments is null.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to