[
https://issues.apache.org/jira/browse/CALCITE-5370?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17630705#comment-17630705
]
Benchao Li commented on CALCITE-5370:
-------------------------------------
bq. Could you please point out which rules should I use to enforce the trait?
Thanks in advance!
The rules does not enforce the trait, it's the {{VolcanoPlanner}} that could
enforce the trait. {{ValcanoPlanner}} will use {{RelSubSet}} to represent an
{{RelSet}} with a specific {{RelTraitSet}}, if it could not find a plan which
satisfy the trait requirement, it will throw an exception. The rules you need
depends on the plan and the trait you provided, you can see in Calcite:
https://github.com/apache/calcite/blob/a0e119ea42def418957f214f539469f1aba76c18/core/src/main/java/org/apache/calcite/prepare/PlannerImpl.java#L181-L184,
we added many core rules in this phase.
bq. BTW, I don't think the behavior exposed by HepPlanner and the
AGGREGATE_REMOVE rule is desired, maybe it should be fixed (though lots of
codes seem to be involved)?
I'm not sure about this, {{AggregateRemoveRule}} has already convert the
input's traitset correctly:
https://github.com/apache/calcite/blob/a0e119ea42def418957f214f539469f1aba76c18/core/src/main/java/org/apache/calcite/rel/rules/AggregateRemoveRule.java#L121.
Many Calcite rules are designed to be "abstract", which means it only cares
about the abstract RelNode, it may produce some logical nodes without correct
traitset, e.g. {{LogicalProject}}, then it's other rule's responsibility to
transform it with proper traitset, e.g. {{EnumerableProjectRule}}.
IMO, {{HepPlanner}} is not ready yet to handle trait enforcement now. If you
are using it with some traitset, the rules you added should be carefully
designed, the example you can take a look:
https://github.com/apache/flink/blob/9c1a1a18c4cf968e7e627cc9b542bb60faf9dfb3/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/optimize/program/FlinkStreamProgram.scala#L299-L304.
> AGGREGATE_REMOVE will convert certain SQL statement into RelSubSet
> ------------------------------------------------------------------
>
> Key: CALCITE-5370
> URL: https://issues.apache.org/jira/browse/CALCITE-5370
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.32.0
> Reporter: alaneuler
> Priority: Major
>
> SQL:
> {code:java}
> SELECT name, sum(count_inner)
> FROM
> (SELECT name, COUNT(*) AS count_inner
> FROM pt_lsb
> GROUP BY name
> ORDER BY count_inner LIMIT 100)
> GROUP BY name {code}
> Table:
> {code:java}
> CREATE TABLE IF NOT EXISTS `pt_lsb` (name VARCHAR(20) NOT NULL) {code}
> After the `onMatch` method of rule AGGREGATE_REMOVE, it converts the RelNode
> into a RelSubset and this RelSubset cannot be converted into a SQL string:
> Error:
> {code:java}
> Exception in thread "main" java.lang.AssertionError: Need to implement
> org.apache.calcite.plan.volcano.RelSubset
> at
> org.apache.calcite.rel.rel2sql.RelToSqlConverter.visit(RelToSqlConverter.java:170)
> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
> at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
> at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.base/java.lang.reflect.Method.invoke(Method.java:568)
> at org.apache.calcite.util.ReflectUtil$2.invoke(ReflectUtil.java:531)
> at
> org.apache.calcite.rel.rel2sql.RelToSqlConverter.dispatch(RelToSqlConverter.java:139)
> at
> org.apache.calcite.rel.rel2sql.RelToSqlConverter.visitInput(RelToSqlConverter.java:147)
> at
> org.apache.calcite.rel.rel2sql.SqlImplementor.visitInput(SqlImplementor.java:216)
> at
> org.apache.calcite.rel.rel2sql.SqlImplementor.visitInput(SqlImplementor.java:204)
> at
> org.apache.calcite.rel.rel2sql.SqlImplementor.visitRoot(SqlImplementor.java:180)
> at
> me.alaneuler.calcite.ng.demo.util.SqlUtils.toSqlString(SqlUtils.java:13)
> at
> me.alaneuler.calcite.ng.demo.util.SqlUtils.toSqlString(SqlUtils.java:17)
> at me.alaneuler.calcite.ng.demo.AggRemoveMain.main(AggRemoveMain.java:51)
> Suppressed: java.lang.Throwable: Error while converting RelNode to
> SqlNode:
> HepRelVertex(subset=[rel#17:RelSubset#0.NONE.[]]) at
> org.apache.calcite.util.Util.throwAsRuntime(Util.java:967)
> at
> org.apache.calcite.rel.rel2sql.SqlImplementor.visitRoot(SqlImplementor.java:182)
> ... 3 more {code}
>
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)