When a materialized view definition includes a GROUP BY clause, but the base query does not contain a GROUP BY clause, the current MaterializedViewQueryOptimizer logic may perform an incorrect MV rewrite. For example, the following test case currently passes in TestMaterializedViewQueryOptimizer:
@Test
public void testBaseQueryWithoutGroupByRewrittenToMVWithGroupByIncorrectly()
{
String originalViewSql = format("SELECT a as mv_a, count(b) as cb, c as mv_c FROM %s group by mv_a, mv_c", BASE_TABLE_1);
String baseQuerySql = format("SELECT a, c FROM %s", BASE_TABLE_1);
String expectedRewrittenSql = format("SELECT mv_a as a, mv_c as c FROM %s", VIEW_1);
assertOptimizedQuery(baseQuerySql, expectedRewrittenSql, originalViewSql, BASE_TABLE_1, VIEW_1);
}
This will lead to incorrect query results.
When a materialized view definition includes a
GROUP BYclause, but the base query does not contain aGROUP BYclause, the currentMaterializedViewQueryOptimizerlogic may perform an incorrect MV rewrite. For example, the following test case currently passes inTestMaterializedViewQueryOptimizer:This will lead to incorrect query results.