This is an automated email from the ASF dual-hosted git repository. hyuan pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push: new c7ab646 [CALCITE-3396] Materialized view matches unexpectedly for UNION with different 'all' property (Jin Xing) c7ab646 is described below commit c7ab64688f611f599d3b0d07340680e968c7ccf8 Author: jinxing <jinxing.co...@gmail.com> AuthorDate: Thu Oct 10 17:14:31 2019 +0800 [CALCITE-3396] Materialized view matches unexpectedly for UNION with different 'all' property (Jin Xing) Materialized-View: select * from emps where empid < 300 union select * from emps where empid > 200 Query: select * from emps where empid < 300 union all select * from emps where empid > 200 Above MV and Query have different 'all' property in UNION but they succeed matching now. This patch added a check in UnionToUnionUnifyRule. Close #1493 --- .../calcite/plan/MaterializedViewSubstitutionVisitor.java | 3 ++- .../java/org/apache/calcite/test/MaterializationTest.java | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java b/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java index c24da6f..3b29726 100644 --- a/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java +++ b/core/src/main/java/org/apache/calcite/plan/MaterializedViewSubstitutionVisitor.java @@ -199,7 +199,8 @@ public class MaterializedViewSubstitutionVisitor extends SubstitutionVisitor { final MutableUnion target = (MutableUnion) call.target; List<MutableRel> queryInputs = query.getInputs(); List<MutableRel> targetInputs = target.getInputs(); - if (queryInputs.size() == targetInputs.size()) { + if (query.isAll() == target.isAll() + && queryInputs.size() == targetInputs.size()) { for (MutableRel rel: queryInputs) { int index = targetInputs.indexOf(rel); if (index == -1) { diff --git a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java index 0175f74..a46fabe 100644 --- a/core/src/test/java/org/apache/calcite/test/MaterializationTest.java +++ b/core/src/test/java/org/apache/calcite/test/MaterializationTest.java @@ -2468,12 +2468,25 @@ public class MaterializationTest { checkMaterialize(sql, sql); } - @Test public void testUnionToUnion() { + @Test public void testUnionAllToUnionAll() { String sql0 = "select * from \"emps\" where \"empid\" < 300"; String sql1 = "select * from \"emps\" where \"empid\" > 200"; checkMaterialize(sql0 + " union all " + sql1, sql1 + " union all " + sql0); } + @Test public void testUnionDistinctToUnionDistinct() { + String sql0 = "select * from \"emps\" where \"empid\" < 300"; + String sql1 = "select * from \"emps\" where \"empid\" > 200"; + checkMaterialize(sql0 + " union " + sql1, sql1 + " union " + sql0); + } + + @Test public void testUnionDistinctToUnionAll() { + String sql0 = "select * from \"emps\" where \"empid\" < 300"; + String sql1 = "select * from \"emps\" where \"empid\" > 200"; + checkNoMaterialize(sql0 + " union " + sql1, sql0 + " union all " + sql1, + HR_FKUK_MODEL); + } + private static <E> List<List<List<E>>> list3(E[][][] as) { final ImmutableList.Builder<List<List<E>>> builder = ImmutableList.builder();