Alessandro Solimando created CALCITE-7958:
---------------------------------------------
Summary: MaterializedViewAggregateRule should match a NOT NULL
aggregate call of the view with a nullable aggregate call of the query
Key: CALCITE-7958
URL: https://issues.apache.org/jira/browse/CALCITE-7958
Project: Calcite
Issue Type: Improvement
Components: core
Affects Versions: 1.42.0
Reporter: Alessandro Solimando
Assignee: Alessandro Solimando
Fix For: 1.43.0
_MaterializedViewAggregateRule#rewriteView_ pairs each aggregate call of the
query with an aggregate call of the view, and rejects a pair unless the types
of the two calls are the same object
([here|https://github.com/apache/calcite/blob/63567f2e7478e2658a051ffab532a9b1d439a8f8/core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewAggregateRule.java#L491-L498]):
{code:java}
if (queryAggCall.getAggregation().getKind() !=
viewAggCall.getAggregation().getKind()
|| queryAggCall.isDistinct() != viewAggCall.isDistinct()
|| queryAggCall.getArgList().size() != viewAggCall.getArgList().size()
|| queryAggCall.getType() != viewAggCall.getType()
|| viewAggCall.filterArg >= 0) {
// Continue
continue;
}
{code}
MIN, MAX and SUM of a NOT NULL column are NOT NULL with GROUP BY, because every
group has at least one row, and nullable without GROUP BY, because an empty
input gives one row with NULL. A query without GROUP BY is therefore never
rewritten using a view with GROUP BY for these aggregates, although the view
can answer it.
Reproducer (for {_}MaterializedViewRelOptRulesTest{_}):
{code:java}
@Test void testAggregateMaterializationGlobalAggregateOnGroupedView() {
sql("select \"deptno\", min(\"salary\") as m\n"
+ "from \"emps\" group by \"deptno\"",
"select min(\"salary\") from \"emps\"")
.ok();
}
{code}
The test currently fails as follows:
{noformat}
java.lang.AssertionError: Materialized view failed to be matched by optimized
results:
EnumerableAggregate(group=[{}], EXPR$0=[MIN($3)])
EnumerableTableScan(table=[[hr, emps]])
{noformat}
SUM fails in the same way. COUNT succeeds, because its type is NOT NULL with
and without GROUP BY.
Proposed fix: compare the two types ignoring nullability, accepting a NOT NULL
view call for a nullable query call (but not the reverse)
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)