Added new InlineNestedVariablesRule. The rule modifies the Algebricks rule for InlineVariablesRule to include nested plans in the processing.
Project: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/commit/d90258ff Tree: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/tree/d90258ff Diff: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/diff/d90258ff Branch: refs/heads/prestonc/hash_join Commit: d90258ff34e3dcdd4674c4db6ce53bd69678f2f5 Parents: 231cdf2 Author: Preston Carman <[email protected]> Authored: Tue Mar 4 15:51:59 2014 -0800 Committer: Preston Carman <[email protected]> Committed: Tue Mar 4 15:51:59 2014 -0800 ---------------------------------------------------------------------- .../compiler/rewriter/RewriteRuleset.java | 3 +- .../rules/InlineNestedVariablesRule.java | 97 ++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-vxquery/blob/d90258ff/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/RewriteRuleset.java ---------------------------------------------------------------------- diff --git a/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/RewriteRuleset.java b/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/RewriteRuleset.java index 70a2b8e..16c44b8 100644 --- a/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/RewriteRuleset.java +++ b/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/RewriteRuleset.java @@ -20,6 +20,7 @@ import java.util.LinkedList; import java.util.List; import org.apache.vxquery.compiler.rewriter.rules.ConsolidateAssignAggregateRule; +import org.apache.vxquery.compiler.rewriter.rules.InlineNestedVariablesRule; import org.apache.vxquery.compiler.rewriter.rules.PushChildIntoDataScanRule; import org.apache.vxquery.compiler.rewriter.rules.ConsolidateUnnestsRule; import org.apache.vxquery.compiler.rewriter.rules.ConvertAssignToAggregateRule; @@ -153,7 +154,7 @@ public class RewriteRuleset { xquery.add(new SimpleUnnestToProductRule()); xquery.add(new PushMapOperatorDownThroughProductRule()); xquery.add(new PushSubplanWithAggregateDownThroughProductRule()); - xquery.add(new InlineVariablesRule()); + xquery.add(new InlineNestedVariablesRule()); xquery.add(new PushSelectDownRule()); xquery.add(new PushSelectIntoJoinRule()); // Clean up http://git-wip-us.apache.org/repos/asf/incubator-vxquery/blob/d90258ff/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/rules/InlineNestedVariablesRule.java ---------------------------------------------------------------------- diff --git a/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/rules/InlineNestedVariablesRule.java b/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/rules/InlineNestedVariablesRule.java new file mode 100644 index 0000000..8cd86f8 --- /dev/null +++ b/vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/rules/InlineNestedVariablesRule.java @@ -0,0 +1,97 @@ +/* + * 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.vxquery.compiler.rewriter.rules; + +import java.util.List; + +import org.apache.commons.lang3.mutable.Mutable; + +import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalPlan; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.IOptimizationContext; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag; +import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalVariable; +import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression; +import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator; +import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans; +import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AssignOperator; +import edu.uci.ics.hyracks.algebricks.rewriter.rules.InlineVariablesRule; + +/** + * Modifies the InlineVariablesRule to also process nested plans. + */ +public class InlineNestedVariablesRule extends InlineVariablesRule { + + protected boolean inlineVariables(Mutable<ILogicalOperator> opRef, IOptimizationContext context) + throws AlgebricksException { + AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); + + // Update mapping from variables to expressions during top-down traversal. + if (op.getOperatorTag() == LogicalOperatorTag.ASSIGN) { + AssignOperator assignOp = (AssignOperator) op; + List<LogicalVariable> vars = assignOp.getVariables(); + List<Mutable<ILogicalExpression>> exprs = assignOp.getExpressions(); + for (int i = 0; i < vars.size(); i++) { + ILogicalExpression expr = exprs.get(i).getValue(); + // Ignore functions that are either in the doNotInline set or are non-functional + if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { + AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr; + if (doNotInlineFuncs.contains(funcExpr.getFunctionIdentifier()) || !funcExpr.isFunctional()) { + continue; + } + } + varAssignRhs.put(vars.get(i), exprs.get(i).getValue()); + } + } + + boolean modified = false; + // Descend into nested plans removing projects on the way. + if (op.hasNestedPlans()) { + AbstractOperatorWithNestedPlans nestedOp = (AbstractOperatorWithNestedPlans) op; + for (ILogicalPlan nestedPlan : nestedOp.getNestedPlans()) { + for (Mutable<ILogicalOperator> nestedOpRef : nestedPlan.getRoots()) { + if (inlineVariables(nestedOpRef, context)) { + modified = true; + } + } + } + } + + // Descend into children removing projects on the way. + for (Mutable<ILogicalOperator> inputOpRef : op.getInputs()) { + if (inlineVariables(inputOpRef, context)) { + modified = true; + } + } + + if (performBottomUpAction(op)) { + modified = true; + } + + if (modified) { + context.computeAndSetTypeEnvironmentForOperator(op); + context.addToDontApplySet(this, op); + // Re-enable rules that we may have already tried. They could be applicable now after inlining. + context.removeFromAlreadyCompared(opRef.getValue()); + } + + return modified; + } +}
