kasakrisz commented on code in PR #5096: URL: https://github.com/apache/hive/pull/5096#discussion_r1512178918
########## ql/src/test/org/apache/hadoop/hive/ql/optimizer/calcite/rules/views/TestMaterializedViewIncrementalRewritingRelVisitor.java: ########## @@ -0,0 +1,242 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hive.ql.optimizer.calcite.rules.views; + +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.doReturn; + +@RunWith(MockitoJUnitRunner.class) +public class TestMaterializedViewIncrementalRewritingRelVisitor extends TestRuleBase { + + @Test + public void testIncrementalRebuildIsNotAvailableWhenPlanHasUnsupportedOperator() { + RelNode ts1 = createTS(t1NativeMock, "t1"); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .sort(1) // Order by is not supported + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + assertThat(visitor.go(mvQueryPlan).getIncrementalRebuildMode(), is(IncrementalRebuildMode.NOT_AVAILABLE)); + } + + @Test + public void testIncrementalRebuildIsInsertOnlyWhenPlanHasTSOnNonNativeTable() { + RelNode ts1 = createT2IcebergTS(); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + assertThat(visitor.go(mvQueryPlan).getIncrementalRebuildMode(), is(IncrementalRebuildMode.NOT_AVAILABLE)); + } + + @Test + public void testIncrementalRebuildIsInsertOnlyWhenPlanHasTSOnNonNativeTableSupportsSnapshots() { + doReturn(true).when(table2storageHandler).areSnapshotsSupported(); + RelNode ts1 = createT2IcebergTS(); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + assertThat(visitor.go(mvQueryPlan).getIncrementalRebuildMode(), is(IncrementalRebuildMode.INSERT_ONLY)); + } + + @Test + public void testIncrementalRebuildIsInsertOnlyWhenPlanHasFilter() { + RelNode ts1 = createTS(t1NativeMock, "t1"); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .filter(REX_BUILDER.makeCall(SqlStdOperatorTable.IS_NOT_NULL, REX_BUILDER.makeInputRef(ts1, 0))) + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + MaterializedViewIncrementalRewritingRelVisitor.Result result = visitor.go(mvQueryPlan); + + assertThat(result.getIncrementalRebuildMode(), is(IncrementalRebuildMode.INSERT_ONLY)); + assertThat(result.containsAggregate(), is(false)); + } + + @Test + public void testIncrementalRebuildIsInsertOnlyWhenPlanHasInnerJoin() { + RelNode ts1 = createTS(t1NativeMock, "t1"); + RelNode ts2 = createTS(t2NativeMock, "t2"); + + RexNode joinCondition = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, + REX_BUILDER.makeInputRef(ts1.getRowType().getFieldList().get(0).getType(), 0), + REX_BUILDER.makeInputRef(ts2.getRowType().getFieldList().get(0).getType(), 5)); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .push(ts2) + .join(JoinRelType.INNER, joinCondition) + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + assertThat(visitor.go(mvQueryPlan).getIncrementalRebuildMode(), is(IncrementalRebuildMode.INSERT_ONLY)); + } + + @Test + public void testIncrementalRebuildIsNotAvailableWhenPlanHasJoinOtherThanInner() { + RelNode ts1 = createTS(t1NativeMock, "t1"); + RelNode ts2 = createTS(t2NativeMock, "t2"); + + RexNode joinCondition = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS, + REX_BUILDER.makeInputRef(ts1.getRowType().getFieldList().get(0).getType(), 0), + REX_BUILDER.makeInputRef(ts2.getRowType().getFieldList().get(0).getType(), 5)); + + RelNode mvQueryPlan = REL_BUILDER + .push(ts1) + .push(ts2) + .join(JoinRelType.LEFT, joinCondition) + .build(); + + MaterializedViewIncrementalRewritingRelVisitor visitor = new MaterializedViewIncrementalRewritingRelVisitor(); + assertThat(visitor.go(mvQueryPlan).getIncrementalRebuildMode(), is(IncrementalRebuildMode.NOT_AVAILABLE)); + } + + @Test + public void testIncrementalRebuildIsInsertOnlyWhenPlanHasAggregate() { Review Comment: IMHO that wouldn't increase the test coverage because there are no separate codepaths for aggregates with and without grouping keys. Anyway I added one of the newly created tests about aggregates without grouping keys. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org