>From <[email protected]>:
[email protected] has uploaded this change for review. (
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19495 )
Change subject: WIP: Array Unnest Optimization
......................................................................
WIP: Array Unnest Optimization
Change-Id: I5a78debddc02f12c899a741a6cc5d19c6824926a
---
M
asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
A
asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PullUnnestOutOfJoin.java
2 files changed, 95 insertions(+), 0 deletions(-)
git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb
refs/changes/95/19495/1
diff --git
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
index d786e9d..49676bf 100644
---
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
+++
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/base/RuleCollections.java
@@ -74,6 +74,7 @@
import org.apache.asterix.optimizer.rules.NestGroupByRule;
import org.apache.asterix.optimizer.rules.NormalizeWritingPathRule;
import org.apache.asterix.optimizer.rules.PullSelectOutOfSpatialJoin;
+import org.apache.asterix.optimizer.rules.PullUnnestOutOfJoin;
import
org.apache.asterix.optimizer.rules.PushAggFuncIntoStandaloneAggregateRule;
import org.apache.asterix.optimizer.rules.PushAggregateIntoNestedSubplanRule;
import org.apache.asterix.optimizer.rules.PushFieldAccessRule;
@@ -284,6 +285,7 @@
List<IAlgebraicRewriteRule> fieldLoads = new LinkedList<>();
fieldLoads.add(new LoadRecordFieldsRule());
fieldLoads.add(new PushFieldAccessRule());
+ fieldLoads.add(new PullUnnestOutOfJoin());
// fieldLoads.add(new ByNameToByHandleFieldAccessRule()); -- disabled
fieldLoads.add(new ReinferAllTypesRule());
fieldLoads.add(new
ByNameToByIndexFieldAccessRule(includeNameToIndexRewrite));
diff --git
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PullUnnestOutOfJoin.java
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PullUnnestOutOfJoin.java
new file mode 100644
index 0000000..e96bc7d
--- /dev/null
+++
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PullUnnestOutOfJoin.java
@@ -0,0 +1,84 @@
+/*
+ * 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.optimizer.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.operators.logical.AbstractBinaryJoinOperator;
+import
org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
+import
org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator;
+import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+
+public class PullUnnestOutOfJoin implements IAlgebraicRewriteRule {
+
+ @Override
+ public boolean rewritePre(Mutable<ILogicalOperator> opRef,
IOptimizationContext context)
+ throws AlgebricksException {
+ return false;
+ }
+
+ @Override
+ public boolean rewritePost(Mutable<ILogicalOperator> opRef,
IOptimizationContext context)
+ throws AlgebricksException {
+
+ AbstractLogicalOperator op = (AbstractLogicalOperator)
opRef.getValue();
+ if (op.getOperatorTag() != LogicalOperatorTag.INNERJOIN
+ && op.getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
+ return false;
+ }
+
+ AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) op;
+ Set<LogicalVariable> usedInJoin = new HashSet<>();
+ join.getCondition().getValue().getUsedVariables(usedInJoin);
+
+ // This is to skip the case where the join condition is a constant true
+ if (usedInJoin.isEmpty()) {
+ return false;
+ }
+
+ AbstractLogicalOperator left = (AbstractLogicalOperator)
join.getInputs().get(0).getValue();
+ AbstractLogicalOperator right = (AbstractLogicalOperator)
join.getInputs().get(1).getValue();
+ if (left.getOperatorTag() != LogicalOperatorTag.UNNEST &&
right.getOperatorTag() != LogicalOperatorTag.UNNEST) {
+ return false;
+ }
+
+ UnnestOperator unnest =
+ left.getOperatorTag() == LogicalOperatorTag.UNNEST ?
(UnnestOperator) left : (UnnestOperator) right;
+ boolean isLeft = left.getOperatorTag() == LogicalOperatorTag.UNNEST;
+ Mutable<ILogicalOperator> unnestInputSide = isLeft ?
join.getInputs().get(0) : join.getInputs().get(1);
+
+ if (usedInJoin.contains(unnest.getVariable())) {
+ return false;
+ }
+
+ unnestInputSide.setValue(unnest.getInputs().get(0).getValue());
+ unnest.getInputs().get(0).setValue(join);
+
+ return true;
+ }
+
+}
--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19495
To unsubscribe, or for help writing mail filters, visit
https://asterix-gerrit.ics.uci.edu/settings
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I5a78debddc02f12c899a741a6cc5d19c6824926a
Gerrit-Change-Number: 19495
Gerrit-PatchSet: 1
Gerrit-Owner: [email protected]
Gerrit-MessageType: newchange