Skip to content

Base query without GROUP BY wrongly rewritten to MV with GROUP BY in MaterializedViewQueryOptimizer #27742

@hantangwangd

Description

@hantangwangd

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

🆕 Unprioritized

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions