[ 
https://issues.apache.org/jira/browse/CALCITE-7766?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Ilya Korol updated CALCITE-7766:
--------------------------------
    Description: 
CALCITE-5387 fixed the same failure for INNER and LEFT joins only, but for 
RIGHT joins issues is still there. For example the following query fails under 
assertions:

{code:sql}
WITH
non_null_table AS (
  SELECT DATE '2023-08-07' AS date_col_non_null FROM dept
),
null_table AS (
  SELECT CAST(null as DATE) AS date_col_null FROM dept
)
SELECT *
FROM null_table
RIGHT JOIN non_null_table
ON null_table.date_col_null = non_null_table.date_col_non_null
{code}

{noformat}
java.lang.AssertionError: type mismatch:
ref:
DATE NOT NULL
input:
DATE
      at 
org.apache.calcite.plan.RelOptUtil.eqUpToNullability(RelOptUtil.java:2279)
      at org.apache.calcite.rex.RexChecker.visitInputRef(RexChecker.java:131)
      at org.apache.calcite.rel.core.Filter.isValid(Filter.java:165)
      at 
org.apache.calcite.rel.logical.LogicalFilter.create(LogicalFilter.java:141)
      at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1965)
      at 
org.apache.calcite.rel.rules.JoinPushTransitivePredicatesRule.onMatch(JoinPushTransitivePredicatesRule.java:88)
{noformat}

CALCITE-5387 made a targeted fix in `RexPermuteInputsShuttle` that adjusts 
input ref type:
{code:java}
@Override public RexNode visitInputRef(RexInputRef local) {
  final int index = local.getIndex();
  int target = mapping.getTarget(index);
  if (!matchTargetType) {
    return new RexInputRef(
        target,
        local.getType());
  } else {
    return new RexInputRef(
        target,
        fields.get(target).getType());
  }
}
{code}

but permutation outcome were not applied to RIGHT join at the first place. As 
far as I can guess results of permutation for RIGHT join are the same and 
EMPTY_LIST shortcut was used instead:
{code:java}
case LEFT:
case ANTI:
  return RelOptPredicateList.of(rexBuilder,
      RelOptUtil.conjunctions(leftChildPredicates),
      leftInferredPredicates, rightInferredPredicates);
case RIGHT:
  return RelOptPredicateList.of(rexBuilder,
      RelOptUtil.conjunctions(rightChildPredicates),
      inferredPredicates, EMPTY_LIST);         <------------------
{code}
so as a result RIGHT joins were out of C5387 scope 

  was:
CALCITE-5387 fixed the same failure for INNER and LEFT joins only, but for 
RIGHT joins issues is still there. For example the following query fails under 
assertions:

{code:sql}
WITH
non_null_table AS (
  SELECT DATE '2023-08-07' AS date_col_non_null FROM dept
),
null_table AS (
  SELECT CAST(null as DATE) AS date_col_null FROM dept
)
SELECT *
FROM null_table
RIGHT JOIN non_null_table
ON null_table.date_col_null = non_null_table.date_col_non_null
{code}

{noformat}
java.lang.AssertionError: type mismatch:
ref:
DATE NOT NULL
input:
DATE
      at 
org.apache.calcite.plan.RelOptUtil.eqUpToNullability(RelOptUtil.java:2279)
      at org.apache.calcite.rex.RexChecker.visitInputRef(RexChecker.java:131)
      at org.apache.calcite.rel.core.Filter.isValid(Filter.java:165)
      at 
org.apache.calcite.rel.logical.LogicalFilter.create(LogicalFilter.java:141)
      at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1965)
      at 
org.apache.calcite.rel.rules.JoinPushTransitivePredicatesRule.onMatch(JoinPushTransitivePredicatesRule.java:88)
{noformat}


> Type-mismatch on nullability in JoinPushTransitivePredicatesRule for RIGHT 
> join
> -------------------------------------------------------------------------------
>
>                 Key: CALCITE-7766
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7766
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.42.0
>            Reporter: Ilya Korol
>            Priority: Major
>
> CALCITE-5387 fixed the same failure for INNER and LEFT joins only, but for 
> RIGHT joins issues is still there. For example the following query fails 
> under assertions:
> {code:sql}
> WITH
> non_null_table AS (
>   SELECT DATE '2023-08-07' AS date_col_non_null FROM dept
> ),
> null_table AS (
>   SELECT CAST(null as DATE) AS date_col_null FROM dept
> )
> SELECT *
> FROM null_table
> RIGHT JOIN non_null_table
> ON null_table.date_col_null = non_null_table.date_col_non_null
> {code}
> {noformat}
> java.lang.AssertionError: type mismatch:
> ref:
> DATE NOT NULL
> input:
> DATE
>       at 
> org.apache.calcite.plan.RelOptUtil.eqUpToNullability(RelOptUtil.java:2279)
>       at org.apache.calcite.rex.RexChecker.visitInputRef(RexChecker.java:131)
>       at org.apache.calcite.rel.core.Filter.isValid(Filter.java:165)
>       at 
> org.apache.calcite.rel.logical.LogicalFilter.create(LogicalFilter.java:141)
>       at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1965)
>       at 
> org.apache.calcite.rel.rules.JoinPushTransitivePredicatesRule.onMatch(JoinPushTransitivePredicatesRule.java:88)
> {noformat}
> CALCITE-5387 made a targeted fix in `RexPermuteInputsShuttle` that adjusts 
> input ref type:
> {code:java}
> @Override public RexNode visitInputRef(RexInputRef local) {
>   final int index = local.getIndex();
>   int target = mapping.getTarget(index);
>   if (!matchTargetType) {
>     return new RexInputRef(
>         target,
>         local.getType());
>   } else {
>     return new RexInputRef(
>         target,
>         fields.get(target).getType());
>   }
> }
> {code}
> but permutation outcome were not applied to RIGHT join at the first place. As 
> far as I can guess results of permutation for RIGHT join are the same and 
> EMPTY_LIST shortcut was used instead:
> {code:java}
> case LEFT:
> case ANTI:
>   return RelOptPredicateList.of(rexBuilder,
>       RelOptUtil.conjunctions(leftChildPredicates),
>       leftInferredPredicates, rightInferredPredicates);
> case RIGHT:
>   return RelOptPredicateList.of(rexBuilder,
>       RelOptUtil.conjunctions(rightChildPredicates),
>       inferredPredicates, EMPTY_LIST);         <------------------
> {code}
> so as a result RIGHT joins were out of C5387 scope 



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

Reply via email to