>From Vijay Sarathy <[email protected]>:

Vijay Sarathy has uploaded this change for review. ( 
https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18023 )


Change subject: [ASTERIXDB-3335][COMP]: CBO - CH2 bugs
......................................................................

[ASTERIXDB-3335][COMP]: CBO - CH2 bugs

Change-Id: Ib65cf139a34d087207dd9a4c7de2ffff4ac50d89
---
M 
asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/Stats.java
M 
asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/JoinEnum.java
2 files changed, 44 insertions(+), 20 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/23/18023/1

diff --git 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/JoinEnum.java
 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/JoinEnum.java
index e5166f1..243df18 100644
--- 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/JoinEnum.java
+++ 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/JoinEnum.java
@@ -402,6 +402,7 @@
     // This finds all the join Conditions in the whole query. This is a global 
list of all join predicates.
     // It also fills in the dataset Bits for each join predicate.
     private void findJoinConditionsAndAssignSels() throws AlgebricksException {
+
         List<Mutable<ILogicalExpression>> conjs = new ArrayList<>();
         for (JoinOperator jOp : allJoinOps) {
             AbstractBinaryJoinOperator joinOp = jOp.getAbstractJoinOp();
@@ -417,7 +418,6 @@
                     }
                     jc.joinCondition = conj.getValue().cloneExpression();
                     joinConditions.add(jc);
-                    jc.selectivity = 
stats.getSelectivityFromAnnotationMain(jc.joinCondition, true);
                 }
             } else {
                 if ((expr.getExpressionTag() == 
LogicalExpressionTag.FUNCTION_CALL)) {
@@ -429,32 +429,31 @@
                     // change to not a true condition
                     jc.joinCondition = expr.cloneExpression();
                     joinConditions.add(jc);
-                    jc.selectivity = 
stats.getSelectivityFromAnnotationMain(jc.joinCondition, true);
                 }
             }
         }

         // now patch up any join conditions that have variables referenced in 
any internal assign statements.
         List<LogicalVariable> usedVars = new ArrayList<>();
+        List<AssignOperator> erase = new ArrayList<>();
         for (JoinCondition jc : joinConditions) {
             usedVars.clear();
             ILogicalExpression expr = jc.joinCondition;
             expr.getUsedVariables(usedVars);
-            List<AssignOperator> erase = new ArrayList<>();
             for (AssignOperator aOp : assignOps) {
                 for (int i = 0; i < aOp.getVariables().size(); i++) {
                     if (usedVars.contains(aOp.getVariables().get(i))) {
                         
OperatorManipulationUtil.replaceVarWithExpr((AbstractFunctionCallExpression) 
expr,
                                 aOp.getVariables().get(i), 
aOp.getExpressions().get(i).getValue());
                         jc.joinCondition = expr;
-                        jc.selectivity = 
stats.getSelectivityFromAnnotationMain(jc.joinCondition, true);
                         erase.add(aOp);
                     }
                 }
             }
-            for (int i = erase.size() - 1; i >= 0; i--) {
-                assignOps.remove(erase.get(i));
-            }
+            jc.selectivity = 
stats.getSelectivityFromAnnotationMain(jc.joinCondition, true, false);
+        }
+        for (int i = erase.size() - 1; i >= 0; i--) {
+            assignOps.remove(erase.get(i));
         }

         // now fill the datasetBits for each join condition.
@@ -1143,8 +1142,11 @@

         if (this.singleDatasetPreds.size() > 0) { // We did not have 
selectivities for these before. Now we do.
             for (JoinCondition jc : joinConditions) {
-                jc.selectivity = 
stats.getSelectivityFromAnnotationMain(jc.getJoinCondition(), false);
                 // we may be repeating some work here, but that is ok. This 
will rarely happen (happens in q7 tpch)
+                double sel = 
stats.getSelectivityFromAnnotationMain(jc.getJoinCondition(), false, true);
+                if (sel != -1) {
+                    jc.selectivity = sel;
+                }
             }
         }
     }
diff --git 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/Stats.java
 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/Stats.java
index ae070b4..bfc43fb 100644
--- 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/Stats.java
+++ 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/Stats.java
@@ -172,18 +172,20 @@

     // The expression we get may not be a base condition. It could be 
comprised of ors and ands and nots. So have to
     //recursively find the overall selectivity.
-    private double getSelectivityFromAnnotation(AbstractFunctionCallExpression 
afcExpr, boolean join)
-            throws AlgebricksException {
+    private double getSelectivityFromAnnotation(AbstractFunctionCallExpression 
afcExpr, boolean join,
+            boolean singleDatasetPreds) throws AlgebricksException {
         double sel = 1.0;

         if 
(afcExpr.getFunctionIdentifier().equals(AlgebricksBuiltinFunctions.OR)) {
             double orSel = getSelectivityFromAnnotation(
-                    (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(0).getValue(), join);
+                    (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(0).getValue(), join,
+                    singleDatasetPreds);
             for (int i = 1; i < afcExpr.getArguments().size(); i++) {
                 ILogicalExpression lexpr = 
afcExpr.getArguments().get(i).getValue();
                 if 
(lexpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
                     sel = getSelectivityFromAnnotation(
-                            (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(i).getValue(), join);
+                            (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(i).getValue(), join,
+                            singleDatasetPreds);
                     orSel = orSel + sel - orSel * sel;
                 }
             }
@@ -194,7 +196,8 @@
                 ILogicalExpression lexpr = 
afcExpr.getArguments().get(i).getValue();
                 if 
(lexpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
                     sel = getSelectivityFromAnnotation(
-                            (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(i).getValue(), join);
+                            (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(i).getValue(), join,
+                            singleDatasetPreds);
                     andSel *= sel;
                 }
             }
@@ -203,7 +206,8 @@
             ILogicalExpression lexpr = 
afcExpr.getArguments().get(0).getValue();
             if 
(lexpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
                 sel = getSelectivityFromAnnotation(
-                        (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(0).getValue(), join);
+                        (AbstractFunctionCallExpression) 
afcExpr.getArguments().get(0).getValue(), join,
+                        singleDatasetPreds);
                 // We want to return 1.0 and not 0.0 if there was no annotation
                 return (sel == 1.0) ? 1.0 : 1.0 - sel;
             }
@@ -224,26 +228,35 @@
                 } else {
                     sel *= s;
                 }
+            } else if (singleDatasetPreds) {
+                // Single dataset predicates inside join predicates will have 
a selectivity annotation.
+                // If the annotation is not present, return -1, so we don't 
overwrite previously
+                // computed selectivity.
+                return -1;
             }
         } else {
             JoinProductivityAnnotation jpa = 
afcExpr.getAnnotation(JoinProductivityAnnotation.class);
             s = findJoinSelectivity(jpa, afcExpr);
             sel *= s;
         }
-        if (join && sel == 1.0) {
+
+        List<LogicalVariable> usedVars = new ArrayList<>();
+        usedVars.clear();
+        afcExpr.getUsedVariables(usedVars);
+        if (join && sel == 1.0 && usedVars.size() == 1) {
             // assume no selectivity was assigned
             joinEnum.singleDatasetPreds.add(afcExpr);
         }
         return sel;
     }

-    protected double getSelectivityFromAnnotationMain(ILogicalExpression 
leExpr, boolean join)
-            throws AlgebricksException {
+    protected double getSelectivityFromAnnotationMain(ILogicalExpression 
leExpr, boolean join,
+            boolean singleDatasetPreds) throws AlgebricksException {
         double sel = 1.0;

         if 
(leExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
             AbstractFunctionCallExpression afcExpr = 
(AbstractFunctionCallExpression) leExpr;
-            sel = getSelectivityFromAnnotation(afcExpr, join);
+            sel = getSelectivityFromAnnotation(afcExpr, join, 
singleDatasetPreds);
         }

         return sel;
@@ -261,7 +274,7 @@
         while (op.getOperatorTag() != LogicalOperatorTag.EMPTYTUPLESOURCE) {
             if (op.getOperatorTag() == LogicalOperatorTag.SELECT) {
                 SelectOperator selOper = (SelectOperator) op;
-                sel *= 
getSelectivityFromAnnotationMain(selOper.getCondition().getValue(), join);
+                sel *= 
getSelectivityFromAnnotationMain(selOper.getCondition().getValue(), join, 
false);
             }
             if (op.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
                 sel *= getSelectivity((SubplanOperator) op);
@@ -278,7 +291,7 @@
         while (true) {
             if (op.getOperatorTag() == LogicalOperatorTag.SELECT) {
                 SelectOperator selOper = (SelectOperator) op;
-                sel *= 
getSelectivityFromAnnotationMain(selOper.getCondition().getValue(), false);
+                sel *= 
getSelectivityFromAnnotationMain(selOper.getCondition().getValue(), false, 
false);
             }
             if (op.getInputs().size() > 0) {
                 op = op.getInputs().get(0).getValue();

--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18023
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: Ib65cf139a34d087207dd9a4c7de2ffff4ac50d89
Gerrit-Change-Number: 18023
Gerrit-PatchSet: 1
Gerrit-Owner: Vijay Sarathy <[email protected]>
Gerrit-MessageType: newchange

Reply via email to