[CALCITE-4376] Materialized view recognition fails when target project different columns with GROUP BY#2277
[CALCITE-4376] Materialized view recognition fails when target project different columns with GROUP BY#2277yanlin-Lynn wants to merge 1 commit intoapache:masterfrom
Conversation
…t different columns with GROUP BY (Wang Yanlin)
8499b75 to
ff0fc72
Compare
| this.target = MutableRels.toMutable(target_); | ||
| if (mutableQuery instanceof MutableCalc) { | ||
| this.query = Holder.of(mutableQuery); | ||
| } else { |
There was a problem hiding this comment.
Here, we always try to compensate Calc in the original RelNode. Why can't we do it through custom normalization, or there will be more patterns in the future materialized view recognition. There should be a unified way to implement it.
How do you think?
| final String optimized = "" | ||
| + "LogicalProject(deptno=[CAST($0):TINYINT], count_sal=[$1])\n" | ||
| + " LogicalTableScan(table=[[mv0]])\n"; | ||
| + "LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n" |
There was a problem hiding this comment.
LogicalCalc seems like unnecessary.
| + " EnumerableTableScan(table=[[hr, MV0]])")) | ||
| + "LogicalCalc(expr#0..1=[{inputs}], expr#2=[1], expr#3=[+($t1, $t2)], C=[$t3], " | ||
| + "deptno=[$t0])\n" | ||
| + " LogicalAggregate(group=[{0}], agg#0=[$SUM0($1)])\n" |
There was a problem hiding this comment.
Do we have to add LogicalCalc? Is it possible to be compatible with the original? If LogicalCalc is added, it may not be the optimal RelNode`, which will increase the cost of calculation in physical execution.
| } else { | ||
| List<? extends RexNode> rexNodes = | ||
| cluster.getRexBuilder().identityProjects(mutableQuery.rowType); | ||
| RexProgram program = RexProgram.create( |
There was a problem hiding this comment.
There is a PR[1] to enhance the normalization ability before the recognition of materialized views by defining normalization rules. In the unit test, the ability of materialized recognition is enhanced by compensating the Calc operator. Through this way to achieve better versatility.
[1] Add an interface in RelOptMaterializations to allow registering normalization rules
When target project a different columns with group by, materialized view recognition will fail, see the case below
After apply
AggregateOnCalcToAggregateUnifyRule, query becomesThe target is
There is no match for
Aggregate(groupSet: {0, 1}, groupSets: [{0, 1}], calls: [])in queryand
Calc(program: (expr#0..2=[{inputs}], deptno=[$t1], name=[$t0])).in mv.Always add a Calc between target aggregate and the rolled up aggregate,
but the Calc just projects columns used by the rolled up aggregate.
So we want the query to be like this: