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

Benchao Li commented on CALCITE-5254:
-------------------------------------

[~swtalbot] Thanks for reporting and working on this.
After some investigation, I think the root cause lies in 
{{AggregateOnCalcToAggregateUnifyRule}}. In this [code 
block|https://github.com/apache/calcite/blob/250d13c566913ba541ddd7ac4c4443a11a1a976b/core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java#L1497-L1510],
 it changed the {{MutableScan}}'s parent, but fails to match finally, and makes 
the {{query}} changed after this rule matching.
WDYT?

> Infinite Loop in SubstitutionVisitor.go
> ---------------------------------------
>
>                 Key: CALCITE-5254
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5254
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Steven Talbot
>            Priority: Major
>
> Happens when you have a materialization "target" of an Aggregate/Scan 
> attempting to substitute for an a "query" with a matching Scan. 
> The following test should repro (in CustomMaterializedViewRecognitionRuleTest)
> {code:java}
> @Test void testNoInifiniteLoopOnAggregateScanMaterialization() {
>   final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
>   rootSchema.add("mv1", new AbstractTable() {
>     @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
>       return typeFactory.builder()
>           .add("count", SqlTypeName.INTEGER)
>           .build();
>     }
>   });
>   final Frameworks.ConfigBuilder config =  Frameworks.newConfigBuilder()
>       .parserConfig(SqlParser.Config.DEFAULT)
>       .defaultSchema(
>           CalciteAssert.addSchema(rootSchema, 
> CalciteAssert.SchemaSpec.SCOTT_WITH_TEMPORAL))
>       .traitDefs((List<RelTraitDef>) null);
>   final RelBuilder relBuilder = RelBuilder.create(config.build());
>   final RelNode query = relBuilder.scan("EMP")
>       .project(relBuilder.field("DEPTNO"))
>       .aggregate(
>           relBuilder.groupKey(0),
>           relBuilder.aggregateCall(SqlStdOperatorTable.COUNT)
>       )
>       .build();
>   final RelNode target = relBuilder.scan("EMP")
>       .aggregate(relBuilder.groupKey(), 
> relBuilder.aggregateCall(SqlStdOperatorTable.COUNT))
>       .build();
>   final RelNode replacement = relBuilder.scan("mv1").build();
>   final RelOptMaterialization relOptMaterialization =
>       new RelOptMaterialization(replacement,
>           target, null, Lists.newArrayList("mv1"));
>   final List<Pair<RelNode, List<RelOptMaterialization>>> relOptimized =
>       RelOptMaterializations.useMaterializedViews(query,
>           ImmutableList.of(relOptMaterialization), 
> SubstitutionVisitor.DEFAULT_RULES);
>   System.out.println("never get here!");
> } {code}



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

Reply via email to