I think the "The names of the fields in RelNodes are semantically
meaningless in Calcite's eyes" may not always be applicable.
If I change the testUnionMergeRule test case to the following(just change
the final output *** to *name as ename, deptno as edeptno*):
@Test void testUnionMergeRule() {
    final String sql = "select name as ename, deptno as edeptno from (\n"
        + "select * from (\n"
        + "  select name, deptno from dept\n"
        + "  union all\n"
        + "  select name, deptno from\n"
        + "  (\n"
        + "    select name, deptno, count(1) from dept group by name,
deptno\n"
        + "    union all\n"
        + "    select name, deptno, count(1) from dept group by name,
deptno\n"
        + "  ) subq\n"
        + ") a\n"
        + "union all\n"
        + "select name, deptno from dept\n"
        + ") aa\n";
    sql(sql)
        .withRule(CoreRules.PROJECT_SET_OP_TRANSPOSE,
            CoreRules.PROJECT_REMOVE,
            CoreRules.UNION_MERGE)
        .check();
  }

The result plan is equal to the unchanged one, But they shouldn't be equal
because I modified the final output column alias.
However, the plan before optimization is correct.



Jacques Nadeau <[email protected]> 于2021年11月25日周四 上午11:50写道:

> Are you looking at the RelRoot's field names or the field names of the
> RelNodes in the plan? The names of the fields in RelNodes are semantically
> meaningless in Calcite's eyes (rule semantics are based on ordinal
> position, not field name). The validated row type (and correct field names)
> may only exist in the RelRoot.validatedRowType. Sometimes the RelNode of
> the output of the plan matches this RowType but often it does not.
>
> On Tue, Nov 23, 2021 at 10:47 PM Yanjing Wang <[email protected]>
> wrote:
>
> > Hi, team
> >   I'm using ProjectRemoveRule to simplify my logical plan, But the plan
> > sometimes miss its project alias, I find the rule hasn't checked the
> alias
> > for project row type identity equation, so I think it may be a bug, I
> have
> > tried to reproduce the problem, but I failed to build two projects.
> >
> >   I have tried the following but the plan always ignores the alias:
> >
> > final Function<RelBuilder, RelNode> relFn = b -> {
> >       RelNode plan = b.scan("EMP")
> >           .project(
> >               b.field("DEPTNO"),
> >               b.field("ENAME")
> >           )
> >           .project(
> >               b.alias(b.field(0), "DEPTNO_ALIAS"),
> >               b.alias(b.field(1), "ENAME_ALIAS")
> >           )
> >           .build();
> >       return plan;
> >     };
> >
> > relFn(relFn)
> >         .withRule(
> >             CoreRules.PROJECT_REMOVE)
> >         .withRelBuilderConfig(b -> b.withBloat(-1))
> >         .check();
> >
> >
> >   So I resort to modifying existing test case to reproduce, I change
> > the testUnionMergeRule test case in RelOptRulesTest.java. I change its
> > final projection *** to *name as ename, deptno as edeptno*,
> > and the plan miss the alias again. I think the hep rules shouldn't remove
> > the alias which are output column label.
> >
>

Reply via email to