[NO ISSUE][COMP] Fix compile time errors in Distinct Cardinality Estimation.

Details:
 - Fixed each sampling query for tpcds queries compile error
 - Handled sampling queries for ASTERIXDB-3334 query

Change-Id: I5e8d2d991535d68262462a9564f7f2e5e3432e3b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18140
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Vijay Sarathy <vijay.sarathy@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/ICostMethods.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/ICostMethods.java
index 34143c1..a1dd374 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/ICostMethods.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/ICostMethods.java
@@ -47,4 +47,4 @@
     Cost costDistinct(DistinctOperator distinctOperator);
 
     Cost costOrderBy(OrderOperator orderOp);
-}
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
index 6017774..9f69d17 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
@@ -430,4 +430,4 @@
             return true;
         }
     }
-}
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EnumerateJoinsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EnumerateJoinsRule.java
index fb99151..187a8ee 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EnumerateJoinsRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EnumerateJoinsRule.java
@@ -84,7 +84,6 @@
     private List<AssignOperator> assignOps;
     private List<ILogicalExpression> assignJoinExprs; // These are the join expressions below the assign operator.
 
-    // The Distinct operators for each Select or DataSourceScan operator (if applicable)
     // The Distinct operators for each DataSourceScan operator (if applicable)
     private HashMap<DataSourceScanOperator, ILogicalOperator> dataScanAndGroupByDistinctOps;
 
@@ -133,7 +132,6 @@
             return false;
         }
 
-        // If cboMode or cboTestMode is true, identify each DistinctOp or GroupByOp for the corresponding DataScanOp
         if (op.getOperatorTag() == LogicalOperatorTag.DISTRIBUTE_RESULT) {
             // If cboMode or cboTestMode is true, identify each DistinctOp or GroupByOp for the corresponding DataScanOp
             getDistinctOpsForJoinNodes(op, context);
@@ -455,9 +453,11 @@
                 this.rootGroupByDistinctOp = grpByDistinctOp;
             } else if (tag == LogicalOperatorTag.INNERJOIN || tag == LogicalOperatorTag.LEFTOUTERJOIN) {
                 if (grpByDistinctOp != null) {
+                    Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> distinctVarsFuncPair =
+                            OperatorUtils.getGroupByDistinctVarFuncPair(grpByDistinctOp);
                     for (int i = 0; i < currentOp.getInputs().size(); i++) {
                         ILogicalOperator nextOp = currentOp.getInputs().get(i).getValue();
-                        OperatorUtils.createDistinctOpsForJoinNodes(nextOp, grpByDistinctOp, context,
+                        OperatorUtils.createDistinctOpsForJoinNodes(nextOp, distinctVarsFuncPair, context,
                                 dataScanAndGroupByDistinctOps);
                     }
                 }
@@ -1037,4 +1037,4 @@
         }
         return true;
     }
-}
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EstimatedCostComputationVisitor.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EstimatedCostComputationVisitor.java
index 44d9da6..4874417 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EstimatedCostComputationVisitor.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/EstimatedCostComputationVisitor.java
@@ -403,4 +403,4 @@
 
         return cardCost;
     }
-}
+}
\ No newline at end of file
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 aee590b..7c7e20c 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
@@ -1095,4 +1095,4 @@
         PhysicalOptimizationConfig physOptConfig = context.getPhysicalOptimizationConfig();
         return physOptConfig.getQueryPlanShapeMode();
     }
-}
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/OperatorUtils.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/OperatorUtils.java
index f20e912..27c37d7 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/OperatorUtils.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/cbo/OperatorUtils.java
@@ -20,9 +20,9 @@
 package org.apache.asterix.optimizer.rules.cbo;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.asterix.om.base.AString;
 import org.apache.asterix.om.constants.AsterixConstantValue;
@@ -48,9 +48,13 @@
 
 public class OperatorUtils {
 
-    public static void createDistinctOpsForJoinNodes(ILogicalOperator op, ILogicalOperator grpByDistinctOp,
-            IOptimizationContext context, Map<DataSourceScanOperator, ILogicalOperator> scanAndDistinctOps) {
-        if (op == null) {
+    public static void createDistinctOpsForJoinNodes(ILogicalOperator op,
+            Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> distinctVarsFuncPair,
+            IOptimizationContext context, HashMap<DataSourceScanOperator, ILogicalOperator> scanAndDistinctOps) {
+
+        List<LogicalVariable> distinctVars = distinctVarsFuncPair.getFirst();
+        List<AbstractFunctionCallExpression> distinctFunctions = distinctVarsFuncPair.getSecond();
+        if (op == null || distinctVars.size() == 0) {
             return;
         }
 
@@ -60,20 +64,23 @@
         ILogicalOperator currentOp = op;
         LogicalOperatorTag tag = currentOp.getOperatorTag();
         // add DistinctOp to count distinct values in an attribute
-        if (tag == LogicalOperatorTag.ASSIGN || tag == LogicalOperatorTag.SELECT
-                || tag == LogicalOperatorTag.DATASOURCESCAN) {
-            Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> distinctPair =
-                    getGroupByDistinctVarFuncPair(grpByDistinctOp);
-            List<LogicalVariable> distinctVars = distinctPair.first;
-            if (distinctVars.size() == 0) {
-                return;
+        if (tag == LogicalOperatorTag.INNERJOIN || tag == LogicalOperatorTag.LEFTOUTERJOIN) {
+            for (int i = 0; i < currentOp.getInputs().size(); i++) {
+                ILogicalOperator nextOp = currentOp.getInputs().get(i).getValue();
+                createDistinctOpsForJoinNodes(nextOp, distinctVarsFuncPair, context, scanAndDistinctOps);
             }
-
+        } else {
             DataSourceScanOperator scanOp = null;
             LogicalVariable assignVar;
             while (tag != LogicalOperatorTag.EMPTYTUPLESOURCE) {
                 if (tag == LogicalOperatorTag.SELECT) {
-                    selOp = currentOp;
+                    ILogicalOperator nextOp = currentOp.getInputs().get(0).getValue();
+                    if (nextOp.getOperatorTag() == LogicalOperatorTag.ASSIGN
+                            || nextOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
+                        if (selOp == null && assignOp == null) { // first corresponding SelectOp found iff no corresponding AssignOp appeared before
+                            selOp = currentOp; // one DataSourceScanOp possible, save the corresponding SelectOp
+                        }
+                    }
                 } else if (tag == LogicalOperatorTag.ASSIGN) {
                     assignVar = ((AssignOperator) currentOp).getVariables().get(0);
                     int idx = distinctVars.indexOf(assignVar);
@@ -85,6 +92,7 @@
                     }
                 } else if (tag == LogicalOperatorTag.DATASOURCESCAN) {
                     scanOp = (DataSourceScanOperator) currentOp;
+                    // will work for any attributes present in GroupByOp or DistinctOp
                     List<LogicalVariable> scanVars = scanOp.getVariables();
                     for (LogicalVariable scanVar : scanVars) { // add all required variables of the DataSourceScanOp
                         if (distinctVars.contains(scanVar)) {
@@ -94,24 +102,63 @@
                     if (foundDistinctVars.size() == 0) {
                         scanOp = null; // GroupByOp or DistinctOp doesn't contain any attributes of the dataset
                     }
+                } else if (tag == LogicalOperatorTag.GROUP) { // GroupByOp found through looping (not as direct inputs of a JoinOp)
+                    List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> nestedGrpVarsList =
+                            ((GroupByOperator) currentOp).getGroupByList();
+                    // look for any DistinctOp/GroupByOp variables are replaceable with a nested GroupByOp Variable-expression
+                    for (int i = 0; i < nestedGrpVarsList.size(); i++) {
+                        LogicalVariable prevVar = nestedGrpVarsList.get(i).first;
+                        int idx = distinctVars.indexOf(prevVar);
+                        if (idx != -1 && distinctVars.size() > 0) {
+                            ILogicalExpression expr = nestedGrpVarsList.get(i).second.getValue();
+                            if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                                LogicalVariable newVar = ((VariableReferenceExpression) expr).getVariableReference();
+                                distinctVars.remove(idx);
+                                distinctVars.add(newVar);
+
+                                // replace corresponding functions' variables
+                                for (AbstractFunctionCallExpression funcExpr : distinctFunctions) {
+                                    replaceVariableInFunction(funcExpr, prevVar, newVar);
+                                }
+                            }
+                        }
+                    }
+                } else if (tag == LogicalOperatorTag.INNERJOIN || tag == LogicalOperatorTag.LEFTOUTERJOIN) {
+                    for (int i = 0; i < currentOp.getInputs().size(); i++) {
+                        ILogicalOperator nextOp = currentOp.getInputs().get(i).getValue();
+                        createDistinctOpsForJoinNodes(nextOp, distinctVarsFuncPair, context, scanAndDistinctOps);
+                    }
+                    break; // next operators are already handled in the recursion, so exit looping
                 }
+                // TODO(mehnaz): handle DISTINCT and UNNEST operators (if appears in sub-queries)
+
+                // proceed to the next operator
                 currentOp = currentOp.getInputs().get(0).getValue();
                 tag = currentOp.getOperatorTag();
             }
 
             if (scanOp != null) {
-                ILogicalOperator inputOp = (assignOp != null) ? assignOp : ((selOp != null) ? selOp : scanOp);
+                ILogicalOperator inputOp = (selOp != null) ? selOp : ((assignOp != null) ? assignOp : scanOp);
                 SourceLocation sourceLocation = inputOp.getSourceLocation();
                 DistinctOperator distinctOp =
-                        createDistinctOp(foundDistinctVars, inputOp, sourceLocation, distinctPair.second, context);
+                        createDistinctOp(foundDistinctVars, inputOp, sourceLocation, distinctFunctions, context);
                 if (distinctOp != null) {
                     scanAndDistinctOps.put(scanOp, distinctOp);
                 }
             }
-        } else if (tag == LogicalOperatorTag.INNERJOIN || tag == LogicalOperatorTag.LEFTOUTERJOIN) {
-            for (int i = 0; i < currentOp.getInputs().size(); i++) {
-                ILogicalOperator nextOp = currentOp.getInputs().get(i).getValue();
-                createDistinctOpsForJoinNodes(nextOp, grpByDistinctOp, context, scanAndDistinctOps);
+        }
+    }
+
+    private static void replaceVariableInFunction(AbstractFunctionCallExpression funcExpr, LogicalVariable prevVar,
+            LogicalVariable newVar) {
+        List<Mutable<ILogicalExpression>> argList = funcExpr.getArguments();
+        for (Mutable<ILogicalExpression> arg : argList) {
+            if (arg.getValue().getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                if (prevVar.equals(((VariableReferenceExpression) arg.getValue()).getVariableReference())) {
+                    arg.getValue().substituteVar(prevVar, newVar);
+                }
+            } else if (arg.getValue().getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                replaceVariableInFunction((AbstractFunctionCallExpression) arg.getValue(), prevVar, newVar);
             }
         }
     }
@@ -129,9 +176,8 @@
         return variables;
     }
 
-    private static Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> getGroupByDistinctVarFuncPair(
+    public static Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> getGroupByDistinctVarFuncPair(
             ILogicalOperator grpByDistinctOp) {
-
         Pair<List<LogicalVariable>, List<AbstractFunctionCallExpression>> distinctVarsFunctions =
                 new Pair<>(new ArrayList<>(), new ArrayList<>());
         List<LogicalVariable> distinctVars = distinctVarsFunctions.getFirst();
@@ -144,22 +190,37 @@
         ILogicalExpression varRef;
         ILogicalOperator nextOp;
         if (grpByDistinctOp.getOperatorTag() == LogicalOperatorTag.DISTINCT) {
-            nextOp = grpByDistinctOp.getInputs().get(0).getValue();
-            if (nextOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
-                ILogicalExpression assignExpr = ((AssignOperator) nextOp).getExpressions().get(0).getValue();
-                if (assignExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { // FId: open-object-constructor
-                    List<Mutable<ILogicalExpression>> argList =
-                            ((AbstractFunctionCallExpression) assignExpr).getArguments();
-                    for (int i = 0; i < argList.size(); i += 2) {
-                        // Only odd position arguments are field value expressions.
-                        varRef = argList.get(i + 1).getValue();
-                        if (varRef.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
-                            distinctVars.add(((VariableReferenceExpression) varRef).getVariableReference());
-                        } else if (varRef.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                            distinctVars.addAll(getFunctionVariables((AbstractFunctionCallExpression) varRef));
-                            distinctFunctions.add((AbstractFunctionCallExpression) varRef);
+            ILogicalExpression distinctExpr = ((DistinctOperator) grpByDistinctOp).getExpressions().get(0).getValue();
+            if (distinctExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) { // this Distinct expression should be a variable
+                distinctVars.add(((VariableReferenceExpression) distinctExpr).getVariableReference()); // initial Variable-expression
+                nextOp = grpByDistinctOp.getInputs().get(0).getValue();
+
+                // loop through as long as AssignOp variable does not match with DistinctOp Variable-expression
+                while (nextOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
+                    LogicalVariable assignVar = ((AssignOperator) nextOp).getVariables().get(0);
+                    if (assignVar.equals(((VariableReferenceExpression) distinctExpr).getVariableReference())) {
+                        assert distinctVars.size() == 1;
+                        distinctVars.remove(0); // remove initial Variable-expression of DistinctOp
+
+                        ILogicalExpression assignExpr = ((AssignOperator) nextOp).getExpressions().get(0).getValue();
+                        if (assignExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) { // FId: open-object-constructor
+                            List<Mutable<ILogicalExpression>> argList =
+                                    ((AbstractFunctionCallExpression) assignExpr).getArguments();
+
+                            // add all variables and the corresponding functions from AssignOp arguments
+                            for (int i = 0; i < argList.size(); i += 2) {
+                                varRef = argList.get(i + 1).getValue();
+                                if (varRef.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                                    distinctVars.add(((VariableReferenceExpression) varRef).getVariableReference());
+                                } else if (varRef.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                                    distinctVars.addAll(getFunctionVariables((AbstractFunctionCallExpression) varRef));
+                                    distinctFunctions.add((AbstractFunctionCallExpression) varRef.cloneExpression());
+                                }
+                            }
                         }
+                        break;
                     }
+                    nextOp = nextOp.getInputs().get(0).getValue();
                 }
             }
         } else if (grpByDistinctOp.getOperatorTag() == LogicalOperatorTag.GROUP) {
@@ -178,7 +239,7 @@
                         if (idx != -1 && fVars.size() > 0) {
                             distinctVars.remove(idx);
                             distinctVars.addAll(fVars);
-                            distinctFunctions.add((AbstractFunctionCallExpression) assignExpr);
+                            distinctFunctions.add((AbstractFunctionCallExpression) assignExpr.cloneExpression());
                         }
                     }
                 }
@@ -197,14 +258,12 @@
         List<Mutable<ILogicalExpression>> openRecConsArgs = new ArrayList<>();
         for (AbstractFunctionCallExpression expr : funcExpr) {
             List<LogicalVariable> funcVars = getFunctionVariables(expr);
-            if (new HashSet<>(distinctVars).containsAll(funcVars)) {
-                // all variables in the function are of the current dataset
+            if (new HashSet<>(distinctVars).containsAll(funcVars)) { // all variables in the function are of the current dataset
                 openRecConsArgs.add(new MutableObject<>(
                         new ConstantExpression(new AsterixConstantValue(new AString(String.valueOf(counter))))));
                 openRecConsArgs.add(new MutableObject<>(expr));
                 counter++;
-                // DistinctOp variables are found in the function, so remove
-                notFoundDistinctVars.removeAll(funcVars);
+                notFoundDistinctVars.removeAll(funcVars); // DistinctOp variables are found in the function, so remove
             }
         }
         if (openRecConsArgs.size() > 0) { // at least one Function expression is available/applicable
@@ -243,8 +302,7 @@
             for (LogicalVariable var : distinctVars) {
                 VariableReferenceExpression varExpr = new VariableReferenceExpression(var);
                 varExpr.setSourceLocation(sourceLocation);
-                Mutable<ILogicalExpression> vRef = new MutableObject<>(varExpr);
-                distinctExpr.add(vRef);
+                distinctExpr.add(new MutableObject<>(varExpr));
             }
         } else {
             VariableReferenceExpression varExpr = new VariableReferenceExpression(assignOp.getVariables().get(0));
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 0023874..5d46f2e 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
@@ -907,4 +907,4 @@
         }
         return null; // no SelectOp in the query tree
     }
-}
+}
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q7.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q7.plan
index 91cd909..3a82fb7 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q7.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q7.plan
@@ -3,12 +3,12 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$su_nationkey(ASC), $#1(ASC), $#2(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$326, $$327, $$328]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$328, $$329, $$330]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$326, $$327, $$328]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$328, $$329, $$330]  |PARTITIONED|
               -- SORT_GROUP_BY[$$277, $$273, $$274]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
@@ -25,8 +25,8 @@
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                   -- STREAM_PROJECT  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                      -- HYBRID_HASH_JOIN [$$324][$$300]  |PARTITIONED|
-                                        -- HASH_PARTITION_EXCHANGE [$$324]  |PARTITIONED|
+                                      -- HYBRID_HASH_JOIN [$$326][$$300]  |PARTITIONED|
+                                        -- HASH_PARTITION_EXCHANGE [$$326]  |PARTITIONED|
                                           -- STREAM_PROJECT  |PARTITIONED|
                                             -- ASSIGN  |PARTITIONED|
                                               -- STREAM_PROJECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q8.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q8.plan
index 16343e7..cad6a92 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q8.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q8.plan
@@ -3,12 +3,12 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$#1(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$350]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$351]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$350]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$351]  |PARTITIONED|
               -- SORT_GROUP_BY[$$294]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
@@ -23,8 +23,8 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- HYBRID_HASH_JOIN [$$347][$$341]  |PARTITIONED|
-                                    -- HASH_PARTITION_EXCHANGE [$$347]  |PARTITIONED|
+                                  -- HYBRID_HASH_JOIN [$$348][$$341]  |PARTITIONED|
+                                    -- HASH_PARTITION_EXCHANGE [$$348]  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ASSIGN  |PARTITIONED|
                                           -- STREAM_PROJECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q9.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q9.plan
index 7283aa0..12f531e 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q9.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results_cbo/ch2/ch2_q9.plan
@@ -3,12 +3,12 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$n_name(ASC), $#1(DESC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$230, $$231]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$231, $$232]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$230, $$231]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$231, $$232]  |PARTITIONED|
               -- SORT_GROUP_BY[$$225, $$198]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
@@ -23,8 +23,8 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- HYBRID_HASH_JOIN [$$228][$$224]  |PARTITIONED|
-                                    -- HASH_PARTITION_EXCHANGE [$$228]  |PARTITIONED|
+                                  -- HYBRID_HASH_JOIN [$$229][$$224]  |PARTITIONED|
+                                    -- HASH_PARTITION_EXCHANGE [$$229]  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ASSIGN  |PARTITIONED|
                                           -- STREAM_PROJECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_cbo/cardinality-estimation/join-queries/join-queries.3.plan b/asterixdb/asterix-app/src/test/resources/runtimets/results_cbo/cardinality-estimation/join-queries/join-queries.3.plan
index 25d39e0..cd3f2d4 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results_cbo/cardinality-estimation/join-queries/join-queries.3.plan
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_cbo/cardinality-estimation/join-queries/join-queries.3.plan
@@ -1,8 +1,8 @@
-distribute result [$$35] [cardinality: 924.07, op-cost: 0.0, total-cost: 136471.34]
+distribute result [$$35] [cardinality: 960.71, op-cost: 0.0, total-cost: 136471.34]
 -- DISTRIBUTE_RESULT  |PARTITIONED|
-  exchange [cardinality: 924.07, op-cost: 0.0, total-cost: 136471.34]
+  exchange [cardinality: 960.71, op-cost: 0.0, total-cost: 136471.34]
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-    distinct ([$$35]) [cardinality: 924.07, op-cost: 58474.7, total-cost: 136471.34]
+    distinct ([$$35]) [cardinality: 960.71, op-cost: 58474.7, total-cost: 136471.34]
     -- PRE_SORTED_DISTINCT_BY  |PARTITIONED|
       exchange [cardinality: 4783.64, op-cost: 0.0, total-cost: 77996.64]
       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SetAlgebricksPhysicalOperatorsRule.java b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SetAlgebricksPhysicalOperatorsRule.java
index 457597f..f945994 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SetAlgebricksPhysicalOperatorsRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/SetAlgebricksPhysicalOperatorsRule.java
@@ -648,4 +648,4 @@
             }
         }
     }
-}
+}
\ No newline at end of file