Repository: calcite
Updated Branches:
  refs/heads/master 89f25e221 -> 353a8be07


[CALCITE-2179] General improvements for materialized view rewriting rule

* Addendum to create references based on input node type during the rewriting 
(may prevent difference in nullability issues)


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/353a8be0
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/353a8be0
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/353a8be0

Branch: refs/heads/master
Commit: 353a8be07b431d04951c05ef2c195f207272f390
Parents: 89f25e2
Author: Jesus Camacho Rodriguez <jcama...@apache.org>
Authored: Fri Feb 23 15:37:01 2018 -0800
Committer: Jesus Camacho Rodriguez <jcama...@apache.org>
Committed: Fri Feb 23 15:37:01 2018 -0800

----------------------------------------------------------------------
 .../rel/rules/AbstractMaterializedViewRule.java | 29 ++++++++++----------
 1 file changed, 15 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/353a8be0/core/src/main/java/org/apache/calcite/rel/rules/AbstractMaterializedViewRule.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/AbstractMaterializedViewRule.java
 
b/core/src/main/java/org/apache/calcite/rel/rules/AbstractMaterializedViewRule.java
index a86f382..9192ba1 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/rules/AbstractMaterializedViewRule.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/rules/AbstractMaterializedViewRule.java
@@ -484,7 +484,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
                 // since we want to enforce the rest
                 if (!compensationColumnsEquiPred.isAlwaysTrue()) {
                   compensationColumnsEquiPred = rewriteExpression(rexBuilder, 
mq,
-                      viewNode, viewExprs, queryToViewTableMapping.inverse(), 
queryBasedVEC,
+                      view, viewNode, viewExprs, 
queryToViewTableMapping.inverse(), queryBasedVEC,
                       false, compensationColumnsEquiPred);
                   if (compensationColumnsEquiPred == null) {
                     // Skip it
@@ -494,7 +494,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
                 // For the rest, we use the query equivalence classes
                 if (!otherCompensationPred.isAlwaysTrue()) {
                   otherCompensationPred = rewriteExpression(rexBuilder, mq,
-                      viewNode, viewExprs, queryToViewTableMapping.inverse(), 
currQEC,
+                      view, viewNode, viewExprs, 
queryToViewTableMapping.inverse(), currQEC,
                       true, otherCompensationPred);
                   if (otherCompensationPred == null) {
                     // Skip it
@@ -738,7 +738,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
 
       if (!compensationColumnsEquiPred.isAlwaysTrue()) {
         compensationColumnsEquiPred = rewriteExpression(rexBuilder, mq,
-            target, queryExprs, viewToQueryTableMapping.inverse(), queryEC, 
false,
+            target, target, queryExprs, viewToQueryTableMapping.inverse(), 
queryEC, false,
             compensationColumnsEquiPred);
         if (compensationColumnsEquiPred == null) {
           // Skip it
@@ -748,7 +748,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
       // For the rest, we use the query equivalence classes
       if (!otherCompensationPred.isAlwaysTrue()) {
         otherCompensationPred = rewriteExpression(rexBuilder, mq,
-            target, queryExprs, viewToQueryTableMapping.inverse(), viewEC, 
true,
+            target, target, queryExprs, viewToQueryTableMapping.inverse(), 
viewEC, true,
             otherCompensationPred);
         if (otherCompensationPred == null) {
           // Skip it
@@ -833,7 +833,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
       List<RexNode> viewExprs = topViewProject == null
           ? extractReferences(rexBuilder, viewNode)
           : topViewProject.getChildExps();
-      List<RexNode> rewrittenExprs = rewriteExpressions(rexBuilder, mq, 
viewNode, viewExprs,
+      List<RexNode> rewrittenExprs = rewriteExpressions(rexBuilder, mq, input, 
viewNode, viewExprs,
           queryToViewTableMapping.inverse(), queryEC, true, exprsLineage);
       if (rewrittenExprs == null) {
         return null;
@@ -841,6 +841,7 @@ public abstract class AbstractMaterializedViewRule extends 
RelOptRule {
       return relBuilder
           .push(input)
           .project(rewrittenExprs)
+          .convert(topProject != null ? topProject.getRowType() : 
node.getRowType(), false)
           .build();
     }
 
@@ -1130,7 +1131,7 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
       List<RexNode> queryExprs = extractReferences(rexBuilder, target);
       if (!compensationColumnsEquiPred.isAlwaysTrue()) {
         compensationColumnsEquiPred = rewriteExpression(rexBuilder, mq,
-            target, queryExprs, queryToViewTableMapping, queryEC, false,
+            target, target, queryExprs, queryToViewTableMapping, queryEC, 
false,
             compensationColumnsEquiPred);
         if (compensationColumnsEquiPred == null) {
           // Skip it
@@ -1140,7 +1141,7 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
       // For the rest, we use the query equivalence classes
       if (!otherCompensationPred.isAlwaysTrue()) {
         otherCompensationPred = rewriteExpression(rexBuilder, mq,
-            target, queryExprs, queryToViewTableMapping, viewEC, true,
+            target, target, queryExprs, queryToViewTableMapping, viewEC, true,
             otherCompensationPred);
         if (otherCompensationPred == null) {
           // Skip it
@@ -2229,13 +2230,14 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
   private static RexNode rewriteExpression(
       RexBuilder rexBuilder,
       RelMetadataQuery mq,
+      RelNode targetNode,
       RelNode node,
       List<RexNode> nodeExprs,
       BiMap<RelTableRef, RelTableRef> tableMapping,
       EquivalenceClasses ec,
       boolean swapTableColumn,
       RexNode exprToRewrite) {
-    List<RexNode> rewrittenExprs = rewriteExpressions(rexBuilder, mq, node, 
nodeExprs,
+    List<RexNode> rewrittenExprs = rewriteExpressions(rexBuilder, mq, 
targetNode, node, nodeExprs,
         tableMapping, ec, swapTableColumn, ImmutableList.of(exprToRewrite));
     if (rewrittenExprs == null) {
       return null;
@@ -2259,6 +2261,7 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
   private static List<RexNode> rewriteExpressions(
       RexBuilder rexBuilder,
       RelMetadataQuery mq,
+      RelNode targetNode,
       RelNode node,
       List<RexNode> nodeExprs,
       BiMap<RelTableRef, RelTableRef> tableMapping,
@@ -2277,7 +2280,7 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
     List<RexNode> rewrittenExprs = new ArrayList<>(exprsToRewrite.size());
     for (RexNode exprToRewrite : exprsToRewrite) {
       RexNode rewrittenExpr = replaceWithOriginalReferences(
-          rexBuilder, nodeExprs, nodeLineage, exprToRewrite);
+          rexBuilder, targetNode, nodeLineage, exprToRewrite);
       if (RexUtil.containsTableInputRef(rewrittenExpr) != null) {
         // Some expressions were not present in view output
         return null;
@@ -2362,8 +2365,7 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
    * point to.
    */
   private static RexNode replaceWithOriginalReferences(final RexBuilder 
rexBuilder,
-      final List<RexNode> nodeExprs, final NodeLineage nodeLineage,
-      final RexNode exprToRewrite) {
+      final RelNode node, final NodeLineage nodeLineage, final RexNode 
exprToRewrite) {
     // Currently we allow the following:
     // 1) compensation pred can be directly map to expression
     // 2) all references in compensation pred can be map to expressions
@@ -2384,14 +2386,13 @@ public abstract class AbstractMaterializedViewRule 
extends RelOptRule {
             Integer pos = nodeLineage.exprsLineage.get(e.toString());
             if (pos != null) {
               // Found it
-              return rexBuilder.makeInputRef(e.getType(), pos);
+              return rexBuilder.makeInputRef(node, pos);
             }
             pos = nodeLineage.exprsLineageLosslessCasts.get(e.toString());
             if (pos != null) {
               // Found it
               return rexBuilder.makeCast(
-                  e.getType(), rexBuilder.makeInputRef(
-                      nodeExprs.get(pos).getType(), pos));
+                  e.getType(), rexBuilder.makeInputRef(node, pos));
             }
             return null;
           }

Reply via email to