You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/04/20 12:39:06 UTC

[GitHub] [hive] kasakrisz opened a new pull request #2196: [draft] HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

kasakrisz opened a new pull request #2196:
URL: https://github.com/apache/hive/pull/2196


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://cwiki.apache.org/confluence/display/Hive/HowToContribute
     2. Ensure that you have created an issue on the Hive project JIRA: https://issues.apache.org/jira/projects/HIVE/summary
     3. Ensure you have added or run the appropriate tests for your PR: 
     4. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]HIVE-XXXXX:  Your PR title ...'.
     5. Be sure to keep the PR description updated to reflect all changes.
     6. Please write your PR title to summarize what this PR proposes.
     7. If possible, provide a concise example to reproduce the issue for a faster review.
   
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description, screenshot and/or a reproducable example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Hive versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   


-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
jcamachor commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r621840197



##########
File path: ql/src/test/queries/clientpositive/materialized_view_create_rewrite_6.q
##########
@@ -0,0 +1,75 @@
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.materializedview.rewriting.sql=false;
+
+create table t1(a char(15), b int, c int) stored as orc TBLPROPERTIES ('transactional'='true');
+create table t2(a char(15), b int) stored as orc TBLPROPERTIES ('transactional'='true');
+
+insert into t1(a, b, c) values
+('update', 1, 1), ('update', 2, 1),
+('null_update', null, 1), ('null_update', null, 2);
+insert into t1(a, b, c) values ('remove', 3, 1), ('null_remove', null, 1);
+insert into t1(a, b, c) values ('sum0', 0, 1), ('sum0', 0, 2);
+
+insert into t2(a, b) values
+('update', 10),
+('null_update', null);
+insert into t2(a, b) values ('remove', 30), ('null_remove', null);
+insert into t2(a, b) values ('sum0', 0);
+
+-- Aggregate with count(*): incremental rebuild should be triggered even if there were deletes from source table
+create materialized view mat1 stored as orc TBLPROPERTIES ('transactional'='true') as
+select t1.a, sum(t1.b), count(*) from t1

Review comment:
       Can we add the corresponding test without `count(*)`, making sure that we fallback to full rebuild? Or are there tests that are already covering this case?




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r622058477



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/views/HiveAggregateInsertDeleteIncrementalRewritingRule.java
##########
@@ -0,0 +1,200 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
+
+import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveHepExtractRelNodeRule;
+
+/**
+ * This rule will perform a rewriting to prepare the plan for incremental
+ * view maintenance in case there exist aggregation operator, so we can
+ * avoid the INSERT OVERWRITE and use a MERGE statement instead.
+ *
+ * In particular, the INSERT OVERWRITE maintenance will look like this
+ * (in SQL):
+ * INSERT OVERWRITE mv
+ * SELECT a, b, SUM(s) as s, SUM(c) AS c
+ * FROM (
+ *   SELECT * from mv --OLD DATA
+ *   UNION ALL
+ *   SELECT a, b, SUM(x) AS s, COUNT(*) AS c --NEW DATA
+ *   FROM TAB_A
+ *   JOIN TAB_B ON (TAB_A.a = TAB_B.z)
+ *   WHERE TAB_A.ROW_ID &gt; 5
+ *   GROUP BY a, b) inner_subq
+ * GROUP BY a, b;
+ *
+ * We need to transform that into:
+ * MERGE INTO mv
+ * USING (
+ *   SELECT a, b, SUM(x) AS s, COUNT(*) AS c --NEW DATA
+ *   FROM TAB_A
+ *   JOIN TAB_B ON (TAB_A.a = TAB_B.z)
+ *   WHERE TAB_A.ROW_ID &gt; 5
+ *   GROUP BY a, b) source
+ * ON (mv.a <=> source.a AND mv.b <=> source.b)
+ * WHEN MATCHED AND mv.c + source.c &lt;&gt; 0
+ *   THEN UPDATE SET mv.s = mv.s + source.s, mv.c = mv.c + source.c
+ * WHEN NOT MATCHED
+ *   THEN INSERT VALUES (source.a, source.b, s, c);
+ * WHEN MATCHED AND countStar = 0 THEN DELETE

Review comment:
       fixed




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
jcamachor commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r621843889



##########
File path: ql/src/test/queries/clientpositive/materialized_view_create_rewrite_6.q
##########
@@ -0,0 +1,75 @@
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.materializedview.rewriting.sql=false;
+
+create table t1(a char(15), b int, c int) stored as orc TBLPROPERTIES ('transactional'='true');
+create table t2(a char(15), b int) stored as orc TBLPROPERTIES ('transactional'='true');
+
+insert into t1(a, b, c) values
+('update', 1, 1), ('update', 2, 1),
+('null_update', null, 1), ('null_update', null, 2);
+insert into t1(a, b, c) values ('remove', 3, 1), ('null_remove', null, 1);
+insert into t1(a, b, c) values ('sum0', 0, 1), ('sum0', 0, 2);
+
+insert into t2(a, b) values
+('update', 10),
+('null_update', null);
+insert into t2(a, b) values ('remove', 30), ('null_remove', null);
+insert into t2(a, b) values ('sum0', 0);
+
+-- Aggregate with count(*): incremental rebuild should be triggered even if there were deletes from source table
+create materialized view mat1 stored as orc TBLPROPERTIES ('transactional'='true') as
+select t1.a, sum(t1.b), count(*) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+
+explain cbo
+select t1.a, sum(t1.b) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+-- do some changes on source table data
+delete from t1 where b = 1;
+delete from t1 where a = 'remove';
+delete from t1 where c = 2;
+
+insert into t1(a,b,c) values
+('update', 5, 1),
+('add', 5, 1),
+('add/remove', 0, 0),
+('null_update', null, 0),
+('null_add', null, 0),
+('null_add/remove', null, 0);
+
+delete from t1 where a like 'add/remove';
+
+-- view can not be used
+explain cbo
+select t1.a, sum(t1.b) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+
+-- rebuild the view (incrementally)
+explain cbo
+alter materialized view mat1 rebuild;
+explain
+alter materialized view mat1 rebuild;
+alter materialized view mat1 rebuild;

Review comment:
       Does this example contain rows that hit the three branches in the merge, i.e., insert, update, delete?




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on pull request #2196:
URL: https://github.com/apache/hive/pull/2196#issuecomment-828359629


   Created a follow-up for extending 'show materialized views': https://issues.apache.org/jira/browse/HIVE-25066


-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r622058227



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/views/HiveAggregateIncrementalRewritingRuleBase.java
##########
@@ -203,17 +147,42 @@ public void onMatch(RelOptRuleCall call) {
               elseReturn));
     }
 
+    int flagIndex = joinLeftInput.getRowType().getFieldCount() - 1;
+    RexNode flagNode = rexBuilder.makeInputRef(
+            join.getRowType().getFieldList().get(flagIndex).getType(), flagIndex);
+
     // 6) Build plan
-    // Split this filter condition in CalcitePlanner.fixUpASTAggregateIncrementalRebuild:
-    // First disjunct for update branch
-    // Second disjunct for insert branch
-    RexNode filterCond = rexBuilder.makeCall(
-            SqlStdOperatorTable.OR, flagNode, rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, flagNode));
-    RelNode newNode = call.builder()
+    RelNode newNode = relBuilder
         .push(join)
-        .filter(filterCond)
+        .filter(createFilterCondition(joinRightInput, flagNode, projExprs, relBuilder))
         .project(projExprs)
         .build();
     call.transformTo(newNode);
   }
+
+  protected abstract T createJoinRightInput(RelOptRuleCall call);
+
+  protected static class RightInput {

Review comment:
       Renamed




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz merged pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz merged pull request #2196:
URL: https://github.com/apache/hive/pull/2196


   


-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r622059105



##########
File path: ql/src/test/queries/clientpositive/materialized_view_create_rewrite_6.q
##########
@@ -0,0 +1,75 @@
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.materializedview.rewriting.sql=false;
+
+create table t1(a char(15), b int, c int) stored as orc TBLPROPERTIES ('transactional'='true');
+create table t2(a char(15), b int) stored as orc TBLPROPERTIES ('transactional'='true');
+
+insert into t1(a, b, c) values
+('update', 1, 1), ('update', 2, 1),
+('null_update', null, 1), ('null_update', null, 2);
+insert into t1(a, b, c) values ('remove', 3, 1), ('null_remove', null, 1);
+insert into t1(a, b, c) values ('sum0', 0, 1), ('sum0', 0, 2);
+
+insert into t2(a, b) values
+('update', 10),
+('null_update', null);
+insert into t2(a, b) values ('remove', 30), ('null_remove', null);
+insert into t2(a, b) values ('sum0', 0);
+
+-- Aggregate with count(*): incremental rebuild should be triggered even if there were deletes from source table
+create materialized view mat1 stored as orc TBLPROPERTIES ('transactional'='true') as
+select t1.a, sum(t1.b), count(*) from t1

Review comment:
       `materialized_view_create_rewrite_4.q` covers that case.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] kasakrisz commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
kasakrisz commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r622060653



##########
File path: ql/src/test/queries/clientpositive/materialized_view_create_rewrite_6.q
##########
@@ -0,0 +1,75 @@
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.materializedview.rewriting.sql=false;
+
+create table t1(a char(15), b int, c int) stored as orc TBLPROPERTIES ('transactional'='true');
+create table t2(a char(15), b int) stored as orc TBLPROPERTIES ('transactional'='true');
+
+insert into t1(a, b, c) values
+('update', 1, 1), ('update', 2, 1),
+('null_update', null, 1), ('null_update', null, 2);
+insert into t1(a, b, c) values ('remove', 3, 1), ('null_remove', null, 1);
+insert into t1(a, b, c) values ('sum0', 0, 1), ('sum0', 0, 2);
+
+insert into t2(a, b) values
+('update', 10),
+('null_update', null);
+insert into t2(a, b) values ('remove', 30), ('null_remove', null);
+insert into t2(a, b) values ('sum0', 0);
+
+-- Aggregate with count(*): incremental rebuild should be triggered even if there were deletes from source table
+create materialized view mat1 stored as orc TBLPROPERTIES ('transactional'='true') as
+select t1.a, sum(t1.b), count(*) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+
+explain cbo
+select t1.a, sum(t1.b) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+-- do some changes on source table data
+delete from t1 where b = 1;
+delete from t1 where a = 'remove';
+delete from t1 where c = 2;
+
+insert into t1(a,b,c) values
+('update', 5, 1),
+('add', 5, 1),
+('add/remove', 0, 0),
+('null_update', null, 0),
+('null_add', null, 0),
+('null_add/remove', null, 0);
+
+delete from t1 where a like 'add/remove';
+
+-- view can not be used
+explain cbo
+select t1.a, sum(t1.b) from t1
+join t2 on (t1.a = t2.a)
+group by t1.a;
+
+
+-- rebuild the view (incrementally)
+explain cbo
+alter materialized view mat1 rebuild;
+explain
+alter materialized view mat1 rebuild;
+alter materialized view mat1 rebuild;

Review comment:
       I found that I inserted rows to only one of the source tables that is why new records were not inserted into the view. Added some inserts to the second source table. All three branches are covered.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] jcamachor commented on a change in pull request #2196: HIVE-24992: Incremental rebuild of MV having aggregate in presence of delete operation

Posted by GitBox <gi...@apache.org>.
jcamachor commented on a change in pull request #2196:
URL: https://github.com/apache/hive/pull/2196#discussion_r621840197



##########
File path: ql/src/test/queries/clientpositive/materialized_view_create_rewrite_6.q
##########
@@ -0,0 +1,75 @@
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.materializedview.rewriting.sql=false;
+
+create table t1(a char(15), b int, c int) stored as orc TBLPROPERTIES ('transactional'='true');
+create table t2(a char(15), b int) stored as orc TBLPROPERTIES ('transactional'='true');
+
+insert into t1(a, b, c) values
+('update', 1, 1), ('update', 2, 1),
+('null_update', null, 1), ('null_update', null, 2);
+insert into t1(a, b, c) values ('remove', 3, 1), ('null_remove', null, 1);
+insert into t1(a, b, c) values ('sum0', 0, 1), ('sum0', 0, 2);
+
+insert into t2(a, b) values
+('update', 10),
+('null_update', null);
+insert into t2(a, b) values ('remove', 30), ('null_remove', null);
+insert into t2(a, b) values ('sum0', 0);
+
+-- Aggregate with count(*): incremental rebuild should be triggered even if there were deletes from source table
+create materialized view mat1 stored as orc TBLPROPERTIES ('transactional'='true') as
+select t1.a, sum(t1.b), count(*) from t1

Review comment:
       Can we add the corresponding test without `count(*)`, making sure that we fallback to full rebuild?

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/views/HiveAggregateInsertDeleteIncrementalRewritingRule.java
##########
@@ -0,0 +1,200 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.List;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
+
+import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveHepExtractRelNodeRule;
+
+/**
+ * This rule will perform a rewriting to prepare the plan for incremental
+ * view maintenance in case there exist aggregation operator, so we can
+ * avoid the INSERT OVERWRITE and use a MERGE statement instead.
+ *
+ * In particular, the INSERT OVERWRITE maintenance will look like this
+ * (in SQL):
+ * INSERT OVERWRITE mv
+ * SELECT a, b, SUM(s) as s, SUM(c) AS c
+ * FROM (
+ *   SELECT * from mv --OLD DATA
+ *   UNION ALL
+ *   SELECT a, b, SUM(x) AS s, COUNT(*) AS c --NEW DATA
+ *   FROM TAB_A
+ *   JOIN TAB_B ON (TAB_A.a = TAB_B.z)
+ *   WHERE TAB_A.ROW_ID &gt; 5
+ *   GROUP BY a, b) inner_subq
+ * GROUP BY a, b;
+ *
+ * We need to transform that into:
+ * MERGE INTO mv
+ * USING (
+ *   SELECT a, b, SUM(x) AS s, COUNT(*) AS c --NEW DATA
+ *   FROM TAB_A
+ *   JOIN TAB_B ON (TAB_A.a = TAB_B.z)
+ *   WHERE TAB_A.ROW_ID &gt; 5
+ *   GROUP BY a, b) source
+ * ON (mv.a <=> source.a AND mv.b <=> source.b)
+ * WHEN MATCHED AND mv.c + source.c &lt;&gt; 0
+ *   THEN UPDATE SET mv.s = mv.s + source.s, mv.c = mv.c + source.c
+ * WHEN NOT MATCHED
+ *   THEN INSERT VALUES (source.a, source.b, s, c);
+ * WHEN MATCHED AND countStar = 0 THEN DELETE

Review comment:
       nit. Reorder so the WHEN NOT MATCH INSERT is the last branch in the MERGE statement

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/views/HiveAggregateIncrementalRewritingRuleBase.java
##########
@@ -203,17 +147,42 @@ public void onMatch(RelOptRuleCall call) {
               elseReturn));
     }
 
+    int flagIndex = joinLeftInput.getRowType().getFieldCount() - 1;
+    RexNode flagNode = rexBuilder.makeInputRef(
+            join.getRowType().getFieldList().get(flagIndex).getType(), flagIndex);
+
     // 6) Build plan
-    // Split this filter condition in CalcitePlanner.fixUpASTAggregateIncrementalRebuild:
-    // First disjunct for update branch
-    // Second disjunct for insert branch
-    RexNode filterCond = rexBuilder.makeCall(
-            SqlStdOperatorTable.OR, flagNode, rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, flagNode));
-    RelNode newNode = call.builder()
+    RelNode newNode = relBuilder
         .push(join)
-        .filter(filterCond)
+        .filter(createFilterCondition(joinRightInput, flagNode, projExprs, relBuilder))
         .project(projExprs)
         .build();
     call.transformTo(newNode);
   }
+
+  protected abstract T createJoinRightInput(RelOptRuleCall call);
+
+  protected static class RightInput {

Review comment:
       `RightInput` -> `IncrementalComputePlan` ? That would imply a bit of renaming all over but I think it's more expressive name.




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org