asterixdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From buyin...@apache.org
Subject [1/8] asterixdb git commit: Clean up GROUP BY and WITH clause.
Date Mon, 15 Aug 2016 15:45:32 GMT
Repository: asterixdb
Updated Branches:
  refs/heads/master c3e84a518 -> 8671ddf83


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementAggregateDescriptor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementAggregateDescriptor.java
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementAggregateDescriptor.java
new file mode 100644
index 0000000..2a94418
--- /dev/null
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementAggregateDescriptor.java
@@ -0,0 +1,46 @@
+/*
+ * 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.asterix.runtime.aggregates.collections;
+
+import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
+import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
+import org.apache.asterix.runtime.aggregates.base.AbstractAggregateFunctionDynamicDescriptor;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluatorFactory;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+
+// This aggregate function simply returns the first item in an input stream.
+public class FirstElementAggregateDescriptor extends AbstractAggregateFunctionDynamicDescriptor
{
+
+    private static final long serialVersionUID = 1L;
+    public static final IFunctionDescriptorFactory FACTORY = () -> new FirstElementAggregateDescriptor();
+
+    @Override
+    public FunctionIdentifier getIdentifier() {
+        return AsterixBuiltinFunctions.FIRST_ELEMENT;
+    }
+
+    @Override
+    public IAggregateEvaluatorFactory createAggregateEvaluatorFactory(final IScalarEvaluatorFactory[]
args)
+            throws AlgebricksException {
+        return new FirstElementEvalFactory(args, false);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementEvalFactory.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementEvalFactory.java
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementEvalFactory.java
new file mode 100644
index 0000000..fe3a7ec
--- /dev/null
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/FirstElementEvalFactory.java
@@ -0,0 +1,95 @@
+/*
+ * 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.asterix.runtime.aggregates.collections;
+
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluatorFactory;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.primitive.VoidPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
+
+class FirstElementEvalFactory implements IAggregateEvaluatorFactory {
+
+    private static final long serialVersionUID = 1L;
+    private final IScalarEvaluatorFactory[] args;
+    private final boolean isLocal;
+
+    FirstElementEvalFactory(IScalarEvaluatorFactory[] args, boolean isLocal) {
+        this.args = args;
+        this.isLocal = isLocal;
+    }
+
+    @Override
+    public IAggregateEvaluator createAggregateEvaluator(final IHyracksTaskContext ctx) throws
AlgebricksException {
+
+        return new IAggregateEvaluator() {
+
+            private boolean first = true;
+            // Needs to copy the bytes from inputVal to outputVal because the byte space
of inputVal could be re-used
+            // by consequent tuples.
+            private ArrayBackedValueStorage outputVal = new ArrayBackedValueStorage();
+            private IPointable inputVal = new VoidPointable();
+            private IScalarEvaluator eval = args[0].createScalarEvaluator(ctx);
+            private final byte[] nullBytes = new byte[] { ATypeTag.SERIALIZED_NULL_TYPE_TAG
};
+            private final byte[] systemNullBytes = new byte[] { ATypeTag.SERIALIZED_SYSTEM_NULL_TYPE_TAG
};
+
+            @Override
+            public void init() throws AlgebricksException {
+                first = true;
+            }
+
+            @Override
+            public void step(IFrameTupleReference tuple) throws AlgebricksException {
+                if (!first) {
+                    return;
+                }
+                eval.evaluate(tuple, inputVal);
+                byte typeTagByte = inputVal.getByteArray()[inputVal.getStartOffset()];
+                if(typeTagByte == ATypeTag.SERIALIZED_SYSTEM_NULL_TYPE_TAG){
+                    // Ignores SYSTEM_NULLs generated by local-first-element.
+                    return;
+                }
+                outputVal.assign(inputVal);
+                first = false;
+            }
+
+            @Override
+            public void finish(IPointable result) throws AlgebricksException {
+                if (first) {
+                    result.set(isLocal ? systemNullBytes : nullBytes, 0, 1);
+                    return;
+                }
+                result.set(outputVal);
+            }
+
+            @Override
+            public void finishPartial(IPointable result) throws AlgebricksException {
+                finish(result);
+            }
+
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/LocalFirstElementAggregateDescriptor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/LocalFirstElementAggregateDescriptor.java
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/LocalFirstElementAggregateDescriptor.java
new file mode 100644
index 0000000..c1465d5
--- /dev/null
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/collections/LocalFirstElementAggregateDescriptor.java
@@ -0,0 +1,45 @@
+/*
+ * 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.asterix.runtime.aggregates.collections;
+
+import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
+import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
+import org.apache.asterix.runtime.aggregates.base.AbstractAggregateFunctionDynamicDescriptor;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import org.apache.hyracks.algebricks.runtime.base.IAggregateEvaluatorFactory;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+
+public class LocalFirstElementAggregateDescriptor extends AbstractAggregateFunctionDynamicDescriptor
{
+
+    private static final long serialVersionUID = 1L;
+    public static final IFunctionDescriptorFactory FACTORY = () -> new LocalFirstElementAggregateDescriptor();
+
+    @Override
+    public FunctionIdentifier getIdentifier() {
+        return AsterixBuiltinFunctions.LOCAL_FIRST_ELEMENT;
+    }
+
+    @Override
+    public IAggregateEvaluatorFactory createAggregateEvaluatorFactory(final IScalarEvaluatorFactory[]
args)
+            throws AlgebricksException {
+        return new FirstElementEvalFactory(args, true);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/properties/LocalGroupingProperty.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/properties/LocalGroupingProperty.java
b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/properties/LocalGroupingProperty.java
index 6f4c296..159ed04 100644
--- a/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/properties/LocalGroupingProperty.java
+++ b/hyracks-fullstack/algebricks/algebricks-core/src/main/java/org/apache/hyracks/algebricks/core/algebra/properties/LocalGroupingProperty.java
@@ -100,8 +100,8 @@ public class LocalGroupingProperty extends AbstractGroupingProperty implements
I
             }
         }
         if (!newColumns.isEmpty()) {
-            return new LocalGroupingProperty(newColumns,
-                    preferredOrderEnforcer.subList(groupKeys.size(), newColumns.size()));
+            return new LocalGroupingProperty(newColumns, preferredOrderEnforcer == null ?
null
+                    : preferredOrderEnforcer.subList(groupKeys.size(), newColumns.size()));
         } else {
             return null;
         }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/EliminateGroupByEmptyKeyRule.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/EliminateGroupByEmptyKeyRule.java
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/EliminateGroupByEmptyKeyRule.java
index aab6ce1..8a91cfc 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/EliminateGroupByEmptyKeyRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/EliminateGroupByEmptyKeyRule.java
@@ -21,8 +21,9 @@ package org.apache.hyracks.algebricks.rewriter.rules;
 import java.util.List;
 
 import org.apache.commons.lang3.mutable.Mutable;
-
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.common.utils.Pair;
+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;
@@ -41,7 +42,8 @@ import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 public class EliminateGroupByEmptyKeyRule implements IAlgebraicRewriteRule {
 
     @Override
-    public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext
context) throws AlgebricksException {
+    public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext
context)
+            throws AlgebricksException {
         return false;
     }
 
@@ -54,7 +56,8 @@ public class EliminateGroupByEmptyKeyRule implements IAlgebraicRewriteRule
{
         }
         GroupByOperator groupOp = (GroupByOperator) op;
         List<LogicalVariable> groupVars = groupOp.getGbyVarList();
-        if (groupVars.size() > 0) {
+        List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorList
= groupOp.getDecorList();
+        if (!groupVars.isEmpty() || !decorList.isEmpty()) {
             return false;
         }
         List<ILogicalPlan> nestedPlans = groupOp.getNestedPlans();
@@ -81,7 +84,7 @@ public class EliminateGroupByEmptyKeyRule implements IAlgebraicRewriteRule
{
 
     private Mutable<ILogicalOperator> getNestedTupleSourceReference(Mutable<ILogicalOperator>
nestedTopOperatorRef) {
         Mutable<ILogicalOperator> currentOpRef = nestedTopOperatorRef;
-        while (currentOpRef.getValue().getInputs() != null && currentOpRef.getValue().getInputs().size()
> 0) {
+        while (currentOpRef.getValue().getInputs() != null && !currentOpRef.getValue().getInputs().isEmpty())
{
             currentOpRef = currentOpRef.getValue().getInputs().get(0);
         }
         return currentOpRef;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ExtractGroupByDecorVariablesRule.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ExtractGroupByDecorVariablesRule.java
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ExtractGroupByDecorVariablesRule.java
new file mode 100644
index 0000000..a2ad732
--- /dev/null
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ExtractGroupByDecorVariablesRule.java
@@ -0,0 +1,91 @@
+/*
+ * 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.hyracks.algebricks.rewriter.rules;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.mutable.Mutable;
+import org.apache.commons.lang3.mutable.MutableObject;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.common.utils.Pair;
+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.IOptimizationContext;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable;
+import org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator;
+import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+
+/**
+ * This rule normalizes an GroupBy operator. It extracts non-variable-reference decoration
expressions
+ * as a separate assign operator before the GroupBy operator.
+ * The reason that we have this rule is that in various rules as well as the code generation
for the
+ * GroupBy operator we assumed that decoration expressions can only be variable reference
expressions.
+ */
+public class ExtractGroupByDecorVariablesRule implements IAlgebraicRewriteRule {
+
+    @Override
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext
context)
+            throws AlgebricksException {
+        ILogicalOperator op = opRef.getValue();
+        if (op.getOperatorTag() != LogicalOperatorTag.GROUP) {
+            return false;
+        }
+        GroupByOperator groupByOperator = (GroupByOperator) op;
+        List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorList
= groupByOperator.getDecorList();
+
+        // Returns immediately if there is no decoration entry.
+        if (groupByOperator.getDecorList() == null || groupByOperator.getDecorList().isEmpty())
{
+            return false;
+        }
+
+        // Goes over the decoration list and performs the rewrite.
+        boolean changed = false;
+        List<LogicalVariable> vars = new ArrayList<>();
+        List<Mutable<ILogicalExpression>> exprs = new ArrayList<>();
+        for (Pair<LogicalVariable, Mutable<ILogicalExpression>> decorVarExpr
: decorList) {
+            Mutable<ILogicalExpression> exprRef = decorVarExpr.second;
+            ILogicalExpression expr = exprRef.getValue();
+            if (expr ==null || expr.getExpressionTag() == LogicalExpressionTag.VARIABLE)
{
+                continue;
+            }
+            // Rewrites the decoration entry if the decoration expression is not a variable
reference expression.
+            changed = true;
+            LogicalVariable newVar = context.newVar();
+            vars.add(newVar);
+            exprs.add(exprRef);
+
+            // Normalizes the decor entry -- expression be a variable reference
+            decorVarExpr.second = new MutableObject<>(new VariableReferenceExpression(newVar));
+        }
+        if (!changed) {
+            return false;
+        }
+
+        // Injects an assign operator to evaluate the decoration expression.
+        AssignOperator assignOperator = new AssignOperator(vars, exprs);
+        assignOperator.getInputs().addAll(op.getInputs());
+        op.getInputs().set(0, new MutableObject<>(assignOperator));
+        return changed;
+    }
+}

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveCartesianProductWithEmptyBranchRule.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveCartesianProductWithEmptyBranchRule.java
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveCartesianProductWithEmptyBranchRule.java
index de35fb5..bd5d646 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveCartesianProductWithEmptyBranchRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveCartesianProductWithEmptyBranchRule.java
@@ -18,13 +18,18 @@
  */
 package org.apache.hyracks.algebricks.rewriter.rules;
 
+import java.util.HashSet;
+import java.util.Set;
+
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator;
 import org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext;
 import org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable;
 import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBinaryJoinOperator;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 
 /**
@@ -58,7 +63,12 @@ public class RemoveCartesianProductWithEmptyBranchRule implements IAlgebraicRewr
         return false;
     }
 
-    private boolean emptyBranch(ILogicalOperator op) {
-        return op.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE;
+    private boolean emptyBranch(ILogicalOperator op) throws AlgebricksException {
+        if (op.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
+            return true;
+        }
+        Set<LogicalVariable> liveVariables = new HashSet<>();
+        VariableUtilities.getLiveVariables(op, liveVariables);
+        return liveVariables.isEmpty();
     }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/8671ddf8/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
index b25497c..139f1ed 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/RemoveUnusedAssignAndAggregateRule.java
@@ -26,9 +26,9 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.commons.lang3.mutable.Mutable;
-
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.common.utils.ListSet;
+import org.apache.hyracks.algebricks.common.utils.Pair;
 import org.apache.hyracks.algebricks.common.utils.Triple;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator;
@@ -40,6 +40,7 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogi
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
@@ -119,35 +120,40 @@ public class RemoveUnusedAssignAndAggregateRule implements IAlgebraicRewriteRule
     private int removeFromAssigns(AbstractLogicalOperator op, Set<LogicalVariable>
toRemove,
             IOptimizationContext context) throws AlgebricksException {
         switch (op.getOperatorTag()) {
-            case ASSIGN: {
+            case ASSIGN:
                 AssignOperator assign = (AssignOperator) op;
                 if (removeUnusedVarsAndExprs(toRemove, assign.getVariables(), assign.getExpressions()))
{
                     context.computeAndSetTypeEnvironmentForOperator(assign);
                 }
                 return assign.getVariables().size();
-            }
-            case AGGREGATE: {
+            case AGGREGATE:
                 AggregateOperator agg = (AggregateOperator) op;
                 if (removeUnusedVarsAndExprs(toRemove, agg.getVariables(), agg.getExpressions()))
{
                     context.computeAndSetTypeEnvironmentForOperator(agg);
                 }
                 return agg.getVariables().size();
-            }
-            case UNNEST: {
+            case UNNEST:
                 UnnestOperator uOp = (UnnestOperator) op;
                 LogicalVariable pVar = uOp.getPositionalVariable();
                 if (pVar != null && toRemove.contains(pVar)) {
                     uOp.setPositionalVariable(null);
                 }
                 break;
-            }
-            case UNIONALL: {
+            case UNIONALL:
                 UnionAllOperator unionOp = (UnionAllOperator) op;
                 if (removeUnusedVarsFromUnionAll(unionOp, toRemove)) {
                     context.computeAndSetTypeEnvironmentForOperator(unionOp);
                 }
                 return unionOp.getVariableMappings().size();
-            }
+            case GROUP:
+                GroupByOperator groupByOp = (GroupByOperator) op;
+                if (removeUnusedVarsFromGroupBy(groupByOp, toRemove)) {
+                    context.computeAndSetTypeEnvironmentForOperator(groupByOp);
+                }
+                return groupByOp.getGroupByList().size() + groupByOp.getNestedPlans().size()
+                        + groupByOp.getDecorList().size();
+            default:
+                break;
         }
         return -1;
     }
@@ -171,6 +177,20 @@ public class RemoveUnusedAssignAndAggregateRule implements IAlgebraicRewriteRule
         return modified;
     }
 
+    private boolean removeUnusedVarsFromGroupBy(GroupByOperator groupByOp, Set<LogicalVariable>
toRemove) {
+        Iterator<Pair<LogicalVariable, Mutable<ILogicalExpression>>> iter
= groupByOp.getDecorList().iterator();
+        boolean modified = false;
+        while (iter.hasNext()) {
+            Pair<LogicalVariable, Mutable<ILogicalExpression>> varMapping = iter.next();
+            LogicalVariable decorVar = varMapping.first;
+            if (decorVar != null && toRemove.contains(decorVar)) {
+                iter.remove();
+                modified = true;
+            }
+        }
+        return modified;
+    }
+
     private boolean removeUnusedVarsAndExprs(Set<LogicalVariable> toRemove, List<LogicalVariable>
varList,
             List<Mutable<ILogicalExpression>> exprList) {
         boolean changed = false;
@@ -206,25 +226,22 @@ public class RemoveUnusedAssignAndAggregateRule implements IAlgebraicRewriteRule
         }
         boolean removeUsedVars = true;
         switch (op.getOperatorTag()) {
-            case ASSIGN: {
+            case ASSIGN:
                 AssignOperator assign = (AssignOperator) op;
                 toRemove.addAll(assign.getVariables());
                 break;
-            }
-            case AGGREGATE: {
+            case AGGREGATE:
                 AggregateOperator agg = (AggregateOperator) op;
                 toRemove.addAll(agg.getVariables());
                 break;
-            }
-            case UNNEST: {
+            case UNNEST:
                 UnnestOperator uOp = (UnnestOperator) op;
                 LogicalVariable pVar = uOp.getPositionalVariable();
                 if (pVar != null) {
                     toRemove.add(pVar);
                 }
                 break;
-            }
-            case UNIONALL: {
+            case UNIONALL:
                 UnionAllOperator unionOp = (UnionAllOperator) op;
                 for (Triple<LogicalVariable, LogicalVariable, LogicalVariable> varMapping
: unionOp
                         .getVariableMappings()) {
@@ -232,7 +249,17 @@ public class RemoveUnusedAssignAndAggregateRule implements IAlgebraicRewriteRule
                 }
                 removeUsedVars = false;
                 break;
-            }
+            case GROUP:
+                GroupByOperator groupByOp = (GroupByOperator) op;
+                for (Pair<LogicalVariable, Mutable<ILogicalExpression>> decorMapping
: groupByOp.getDecorList()) {
+                    LogicalVariable decorVar = decorMapping.first;
+                    if (decorVar != null) {
+                        toRemove.add(decorVar);
+                    }
+                }
+                break;
+            default:
+                break;
         }
         if (removeUsedVars) {
             List<LogicalVariable> used = new LinkedList<LogicalVariable>();


Mime
View raw message