You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by bu...@apache.org on 2016/03/16 21:33:55 UTC

incubator-asterixdb-hyracks git commit: ASTERIXDB-865: fix for if-else expression.

Repository: incubator-asterixdb-hyracks
Updated Branches:
  refs/heads/master f6d1e0544 -> 8f813639a


ASTERIXDB-865: fix for if-else expression.

Change-Id: I17978d2f694e2a5082903002b8388c5bd42811a5
Reviewed-on: https://asterix-gerrit.ics.uci.edu/702
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <ti...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/commit/8f813639
Tree: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/tree/8f813639
Diff: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/diff/8f813639

Branch: refs/heads/master
Commit: 8f813639aca92dfba4cbe5ceee66590e67de0568
Parents: f6d1e05
Author: Yingyi Bu <yi...@couchbase.com>
Authored: Wed Mar 16 00:38:29 2016 -0700
Committer: Yingyi Bu <bu...@gmail.com>
Committed: Wed Mar 16 13:28:44 2016 -0700

----------------------------------------------------------------------
 .../algebra/util/OperatorPropertiesUtil.java    | 39 ++++++++++++--------
 .../rewriter/rules/ConsolidateSelectsRule.java  |  4 +-
 .../rewriter/rules/InlineVariablesRule.java     | 18 ++++++---
 .../rewriter/rules/PushSelectDownRule.java      |  2 +-
 .../rules/SimpleUnnestToProductRule.java        |  4 +-
 5 files changed, 41 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/blob/8f813639/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/util/OperatorPropertiesUtil.java
----------------------------------------------------------------------
diff --git a/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/util/OperatorPropertiesUtil.java b/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/util/OperatorPropertiesUtil.java
index 0a434ad..e78db9b 100644
--- a/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/util/OperatorPropertiesUtil.java
+++ b/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/util/OperatorPropertiesUtil.java
@@ -37,14 +37,14 @@ import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression
 import org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans;
-import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.CardinalityInferenceVisitor;
-import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.IsExpressionStatefulVisitor;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
 
 public class OperatorPropertiesUtil {
 
+    private static final String MOVABLE = "isMovable";
+
     public static <T> boolean disjoint(Collection<T> c1, Collection<T> c2) {
         for (T m : c1) {
             if (c2.contains(m)) {
@@ -269,25 +269,32 @@ public class OperatorPropertiesUtil {
     }
 
     /**
-     * Whether the operator is an assign operator that calls a stateful function.
+     * Whether an operator can be moved around in the query plan.
      *
      * @param op
      *            the operator to consider.
-     * @return true if the operator is an assign operator and it calls a stateful function.
+     * @return true if the operator can be moved, false if the operator cannot be moved.
      * @throws AlgebricksException
      */
-    public static boolean isStatefulAssign(ILogicalOperator op) throws AlgebricksException {
-        if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
-            return false;
-        }
-        AssignOperator assignOp = (AssignOperator) op;
-        IsExpressionStatefulVisitor visitor = new IsExpressionStatefulVisitor();
-        for (Mutable<ILogicalExpression> exprRef : assignOp.getExpressions()) {
-            ILogicalExpression expr = exprRef.getValue();
-            if (expr.accept(visitor, null)) {
-                return true;
-            }
+    public static boolean isMovable(ILogicalOperator op) {
+        Object annotation = op.getAnnotations().get(MOVABLE);
+        if (annotation == null) {
+            // By default, it is movable.
+            return true;
         }
-        return false;
+        Boolean movable = (Boolean) annotation;
+        return movable;
+    }
+
+    /**
+     * Mark an operator to be either movable or not.
+     *
+     * @param op
+     *            the operator to consider.
+     * @param movable
+     *            true means it is movable, false means it is not movable.
+     */
+    public static void markMovable(ILogicalOperator op, boolean movable) {
+        op.getAnnotations().put(MOVABLE, movable);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/blob/8f813639/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateSelectsRule.java
----------------------------------------------------------------------
diff --git a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateSelectsRule.java b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateSelectsRule.java
index 2aee1a7..ae52c35 100644
--- a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateSelectsRule.java
+++ b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ConsolidateSelectsRule.java
@@ -67,8 +67,8 @@ public class ConsolidateSelectsRule implements IAlgebraicRewriteRule {
             do {
                 selectParent = nextSelect;
                 nextSelect = (AbstractLogicalOperator) selectParent.getInputs().get(0).getValue();
-            } while (nextSelect.getOperatorTag() == LogicalOperatorTag.ASSIGN && !OperatorPropertiesUtil
-                    .isStatefulAssign(nextSelect) /* Select cannot be pushed through stateful assigns.*/);
+            } while (nextSelect.getOperatorTag() == LogicalOperatorTag.ASSIGN && OperatorPropertiesUtil
+                    .isMovable(nextSelect) /* Select cannot be pushed through un-movable operators.*/);
             // Stop if the child op is not a select.
             if (nextSelect.getOperatorTag() != LogicalOperatorTag.SELECT) {
                 break;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/blob/8f813639/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/InlineVariablesRule.java
----------------------------------------------------------------------
diff --git a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/InlineVariablesRule.java b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/InlineVariablesRule.java
index f2645f5..e7cf912 100644
--- a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/InlineVariablesRule.java
+++ b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/InlineVariablesRule.java
@@ -29,6 +29,7 @@ import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator;
+import org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan;
 import org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext;
 import org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
 import org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
@@ -41,7 +42,6 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogi
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
-import org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl;
 import org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalExpressionReferenceTransform;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 
@@ -168,10 +168,18 @@ public class InlineVariablesRule implements IAlgebraicRewriteRule {
 
         // Descend into subplan
         if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
-            ALogicalPlanImpl subPlan = (ALogicalPlanImpl) ((SubplanOperator) op).getNestedPlans().get(0);
-            Mutable<ILogicalOperator> subPlanRootOpRef = subPlan.getRoots().get(0);
-            if (inlineVariables(subPlanRootOpRef, context)) {
-                modified = true;
+            SubplanOperator subplanOp = (SubplanOperator) op;
+            for (ILogicalPlan nestedPlan : subplanOp.getNestedPlans()) {
+                for (Mutable<ILogicalOperator> root : nestedPlan.getRoots()) {
+                    if (inlineVariables(root, context)) {
+                        modified = true;
+                    }
+                    // Variables produced by a nested subplan cannot be inlined
+                    // in operators above the subplan.
+                    Set<LogicalVariable> producedVars = new HashSet<LogicalVariable>();
+                    VariableUtilities.getProducedVariables(root.getValue(), producedVars);
+                    varAssignRhs.keySet().removeAll(producedVars);
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/blob/8f813639/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/PushSelectDownRule.java
----------------------------------------------------------------------
diff --git a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/PushSelectDownRule.java b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/PushSelectDownRule.java
index 3d3331c..aab6d12 100644
--- a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/PushSelectDownRule.java
+++ b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/PushSelectDownRule.java
@@ -73,7 +73,7 @@ public class PushSelectDownRule implements IAlgebraicRewriteRule {
             throws AlgebricksException {
         AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
         if (op2.getInputs().size() != 1 || op2.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN
-                || OperatorPropertiesUtil.isStatefulAssign(op2)) {
+                || !OperatorPropertiesUtil.isMovable(op2)) {
             return false;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb-hyracks/blob/8f813639/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SimpleUnnestToProductRule.java
----------------------------------------------------------------------
diff --git a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SimpleUnnestToProductRule.java b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SimpleUnnestToProductRule.java
index 1ba1ea3..06fb360 100644
--- a/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SimpleUnnestToProductRule.java
+++ b/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SimpleUnnestToProductRule.java
@@ -110,8 +110,8 @@ public class SimpleUnnestToProductRule implements IAlgebraicRewriteRule {
                     && boundaryOpRef.getValue().getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN) {
                 List<LogicalVariable> opUsedVars = new ArrayList<LogicalVariable>();
                 VariableUtilities.getUsedVariables(boundaryOpRef.getValue(), opUsedVars);
-                if (opUsedVars.size() == 0 && !OperatorPropertiesUtil.isStatefulAssign(boundaryOpRef.getValue())
-                /* We cannot freely move the location of stateful assigns. */) {
+                if (opUsedVars.size() == 0 && OperatorPropertiesUtil.isMovable(boundaryOpRef.getValue())
+                /* We cannot freely move the location of operators tagged as un-movable. */) {
                     // move down the boundary if the operator is a const assigns.
                     boundaryOpRef = boundaryOpRef.getValue().getInputs().get(0);
                 } else {