You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by dl...@apache.org on 2019/03/21 17:24:11 UTC

[asterixdb] branch master updated: [NO ISSUE][COMP] Documenting BreakSelectIntoConjunctsRule

This is an automated email from the ASF dual-hosted git repository.

dlych pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 00fa68a  [NO ISSUE][COMP] Documenting BreakSelectIntoConjunctsRule
00fa68a is described below

commit 00fa68a849c46785f7cc7f430215a15f7cecc741
Author: Hussain Towaileb <Hu...@Gmail.com>
AuthorDate: Wed Mar 13 17:58:54 2019 +0300

    [NO ISSUE][COMP] Documenting BreakSelectIntoConjunctsRule
    
    - user model changes: no
    - storage format changes: no
    - interface changes: no
    
    Details:
    - Added documentation for BreakSelectIntoConjunctsRule.
    - Renamed some variables for easier readability and maintenance.
    - No actual code/logic changes.
    
    Change-Id: Iadc5dc41115f91caa835255396969eaf47e1356d
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/3230
    Sonar-Qube: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Contrib: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
    Reviewed-by: Dmitry Lychagin <dm...@couchbase.com>
---
 .../rules/BreakSelectIntoConjunctsRule.java        | 112 +++++++++++++++------
 1 file changed, 80 insertions(+), 32 deletions(-)

diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/BreakSelectIntoConjunctsRule.java b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/BreakSelectIntoConjunctsRule.java
index ab665b3..0f8bb15 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/BreakSelectIntoConjunctsRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/BreakSelectIntoConjunctsRule.java
@@ -34,57 +34,105 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperat
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 import org.apache.hyracks.api.exceptions.SourceLocation;
 
+/**
+ * This rule breaks the select operator condition into conjuncts and create a different select operator per conjunct
+ * when applicable.
+ *
+ * Example (simplified):
+ * Before:
+ * select (and(lt($$test.getField("field1"), 100), lt($$test.getField("field2"), 20)))
+ * unnest $$test <- dataset("test.test")
+ *
+ * After:
+ * select (lt($$test.getField("field1"), 100))
+ * select (lt($$test.getField("field2"), 20))
+ * unnest $$test <- dataset("test.test")
+ */
 public class BreakSelectIntoConjunctsRule implements IAlgebraicRewriteRule {
 
-    private List<Mutable<ILogicalExpression>> conjs = new ArrayList<Mutable<ILogicalExpression>>();
+    // Conjuncts of the select operator condition
+    private List<Mutable<ILogicalExpression>> selectOperatorConditionConjuncts = new ArrayList<>();
 
     @Override
-    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) {
+    public boolean rewritePost(Mutable<ILogicalOperator> operatorRef, IOptimizationContext context) {
         return false;
     }
 
     @Override
-    public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+    public boolean rewritePre(Mutable<ILogicalOperator> operatorRef, IOptimizationContext context)
             throws AlgebricksException {
-        AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
-        if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
+        // Expected select operator
+        AbstractLogicalOperator expectedSelectOperator = (AbstractLogicalOperator) operatorRef.getValue();
+
+        // Bail if it's not a select operator
+        if (expectedSelectOperator.getOperatorTag() != LogicalOperatorTag.SELECT) {
             return false;
         }
-        SelectOperator select = (SelectOperator) op;
 
-        ILogicalExpression cond = select.getCondition().getValue();
+        // Select operator
+        SelectOperator originalSelectOperator = (SelectOperator) expectedSelectOperator;
+
+        // Select operator condition
+        ILogicalExpression selectOperatorCondition = originalSelectOperator.getCondition().getValue();
 
-        conjs.clear();
-        if (!cond.splitIntoConjuncts(conjs)) {
+        // Clear the condition conjuncts
+        selectOperatorConditionConjuncts.clear();
+
+        // Break the condition into conjuncts, bail if it's not applicable
+        if (!selectOperatorCondition.splitIntoConjuncts(selectOperatorConditionConjuncts)) {
             return false;
         }
 
-        SourceLocation sourceLoc = select.getSourceLocation();
-
-        Mutable<ILogicalOperator> childOfSelect = select.getInputs().get(0);
-        boolean fst = true;
-        ILogicalOperator botOp = select;
-        ILogicalExpression firstExpr = null;
-        for (Mutable<ILogicalExpression> eRef : conjs) {
-            ILogicalExpression e = eRef.getValue();
-            if (fst) {
-                fst = false;
-                firstExpr = e;
+        // Source location
+        SourceLocation sourceLoc = originalSelectOperator.getSourceLocation();
+
+        // Inputs of original select operator
+        Mutable<ILogicalOperator> originalSelectOperatorInputs = originalSelectOperator.getInputs().get(0);
+
+        // First expression read of the conjuncts, this will be set to the original select operator at the end
+        boolean isFirst = true;
+        ILogicalExpression firstExpression = null;
+
+        // Bottom operator points to the original select operator at first
+        ILogicalOperator bottomOperator = originalSelectOperator;
+
+        // Start creating the select operators for the condition conjuncts
+        for (Mutable<ILogicalExpression> expressionRef : selectOperatorConditionConjuncts) {
+            ILogicalExpression expression = expressionRef.getValue();
+
+            // Hold reference to the first condition
+            if (isFirst) {
+                isFirst = false;
+                firstExpression = expression;
             } else {
-                SelectOperator newSelect = new SelectOperator(new MutableObject<ILogicalExpression>(e),
-                        select.getRetainMissing(), select.getMissingPlaceholderVariable());
-                newSelect.setSourceLocation(sourceLoc);
-                List<Mutable<ILogicalOperator>> botInpList = botOp.getInputs();
-                botInpList.clear();
-                botInpList.add(new MutableObject<ILogicalOperator>(newSelect));
-                context.computeAndSetTypeEnvironmentForOperator(botOp);
-                botOp = newSelect;
+                // New select operator
+                SelectOperator newSelectOperator =
+                        new SelectOperator(new MutableObject<>(expression), originalSelectOperator.getRetainMissing(),
+                                originalSelectOperator.getMissingPlaceholderVariable());
+                newSelectOperator.setSourceLocation(sourceLoc);
+
+                // Put the new operator at the bottom (child of current operator)
+                List<Mutable<ILogicalOperator>> bottomOperatorInputs = bottomOperator.getInputs();
+                bottomOperatorInputs.clear();
+                bottomOperatorInputs.add(new MutableObject<>(newSelectOperator));
+
+                // Compute the output type environment
+                context.computeAndSetTypeEnvironmentForOperator(bottomOperator);
+
+                // Bottom operator points to the new operator
+                bottomOperator = newSelectOperator;
             }
         }
-        botOp.getInputs().add(childOfSelect);
-        select.getCondition().setValue(firstExpr);
-        context.computeAndSetTypeEnvironmentForOperator(botOp);
-        context.computeAndSetTypeEnvironmentForOperator(select);
+
+        // Add the original select operator inputs to the bottom operator
+        bottomOperator.getInputs().add(originalSelectOperatorInputs);
+
+        // Assign the first expression to the original operator (top)
+        originalSelectOperator.getCondition().setValue(firstExpression);
+
+        // (Re)compute the output type environment
+        context.computeAndSetTypeEnvironmentForOperator(bottomOperator);
+        context.computeAndSetTypeEnvironmentForOperator(originalSelectOperator);
 
         return true;
     }