Hi
Recently, I've been using calcite to build an MPP system and I'd like to add a 
distribution trait to statistics of a table. And then the metadata of 
statistics for my table would contain HASH_DISTRIBUTED.
However, when I perform RBO, calcite will raise an UnsupportedException which 
exception stack is
```
java.lang.UnsupportedOperationException
        at 
org.apache.calcite.util.mapping.Mappings$AbstractMapping.getSourceOpt(Mappings.java:949)
        at 
org.apache.calcite.util.mapping.Mappings$InverseMapping.getTargetOpt(Mappings.java:1839)
        at 
org.apache.calcite.rel.RelDistributions$RelDistributionImpl.apply(RelDistributions.java:146)
        at 
org.apache.calcite.rel.metadata.RelMdDistribution.project(RelMdDistribution.java:167)
        at 
org.apache.calcite.rel.metadata.RelMdDistribution.distribution(RelMdDistribution.java:103)
        at 
org.apache.calcite.rel.metadata.janino.GeneratedMetadata_DistributionHandler.distribution_$(Unknown
 Source)
        at 
org.apache.calcite.rel.metadata.janino.GeneratedMetadata_DistributionHandler.distribution(Unknown
 Source)
        at 
org.apache.calcite.rel.metadata.RelMetadataQuery.distribution(RelMetadataQuery.java:621)
        at 
org.apache.calcite.rel.metadata.RelMdDistribution.filter(RelMdDistribution.java:137)
        at 
org.apache.calcite.rel.logical.LogicalFilter.lambda$create$1(LogicalFilter.java:138)
        at org.apache.calcite.plan.RelTraitSet.replaceIf(RelTraitSet.java:258)
        at 
org.apache.calcite.rel.logical.LogicalFilter.create(LogicalFilter.java:137)
        at 
org.apache.calcite.rel.core.RelFactories$FilterFactoryImpl.createFilter(RelFactories.java:345)
        at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1771)
        at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1747)
        at org.apache.calcite.tools.RelBuilder.filter(RelBuilder.java:1714)
        at 
org.apache.calcite.rel.rules.PushProjector.convertProject(PushProjector.java:394)
        at 
org.apache.calcite.rel.rules.ProjectFilterTransposeRule.onMatch(ProjectFilterTransposeRule.java:180)
        at 
org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:337)
        at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:556)
        at 
org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:420)
        at 
org.apache.calcite.plan.hep.HepPlanner.executeEndGroup(HepPlanner.java:360)
        at 
org.apache.calcite.plan.hep.HepInstruction$EndGroup$State.execute(HepInstruction.java:349)
        at 
org.apache.calcite.plan.hep.HepPlanner.lambda$executeProgram$0(HepPlanner.java:211)
        at 
com.google.common.collect.ImmutableList.forEach(ImmutableList.java:405)
        at 
org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:210)
        at 
org.apache.calcite.plan.hep.HepProgram$State.execute(HepProgram.java:118)
        at 
org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:205)
        at 
org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:191)
```
According to the stack trace, this exception is caused by the rule of 
ProjectFilterTransposeRule. This rule will push the project down the filter 
node, and deep into this process, I find calcite wants to build a new project 
with distribution which infers from the input of original filter using a 
mapping by code below:
```java
  public static RelDistribution project(RelMetadataQuery mq, RelNode input,
      List<? extends RexNode> projects) {
    final RelDistribution inputDistribution = mq.distribution(input);
    final Mappings.TargetMapping mapping =
        Project.getPartialMapping(input.getRowType().getFieldCount(),
            projects);
    return inputDistribution.apply(mapping);
  }
But this is an inverse mapping with a parent `PartialFunctionImpl` which 
doesn't implement functions of getTargetOpt() and getSourceOpt(). And it raises 
the exception above.
So, I can't figure out if it is a bug or if I don't use the correct way of 
doing with distribution trait.

Reply via email to