[ASTERIXDB-3125][COMP] Select operator has zero cost and cardinality

[ASTERIXDB-3126][COMP] CBO: hint productivity NPE if missing cardinality
estimate on collection

- user model changes: no
- storage format changes: no
- interface changes: no


Change-Id: I7cbff25b14346ca8eca5baa7e0fe8fb1da72f270
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17408
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Tested-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Ali Alsuliman <ali.al.solaiman@gmail.com>
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 183eb46..cee555e 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
@@ -434,8 +434,9 @@
             HashMap<EmptyTupleSourceOperator, ILogicalOperator> joinLeafInputsHashMap) {
         ILogicalOperator leftInput = joinLeafInputsHashMap.get(plan.getEmptyTupleSourceOp());
         skipAllIndexes(plan, leftInput);
-        if (leftInput.getOperatorTag() == LogicalOperatorTag.SELECT) {
-            addCardCostAnnotations(leftInput, plan);
+        ILogicalOperator selOp = findSelectOrDataScan(leftInput);
+        if (selOp != null) {
+            addCardCostAnnotations(selOp, plan);
         }
         addCardCostAnnotations(findDataSourceScanOperator(leftInput), plan);
     }
@@ -493,8 +494,9 @@
             // leaf
             ILogicalOperator leftInput = joinLeafInputsHashMap.get(leftPlan.getEmptyTupleSourceOp());
             skipAllIndexes(leftPlan, leftInput);
-            if (leftInput.getOperatorTag() == LogicalOperatorTag.SELECT) {
-                addCardCostAnnotations(leftInput, leftPlan);
+            ILogicalOperator selOp = findSelectOrDataScan(leftInput);
+            if (selOp != null) {
+                addCardCostAnnotations(selOp, leftPlan);
             }
             joinOp.getInputs().get(0).setValue(leftInput);
             addCardCostAnnotations(findDataSourceScanOperator(leftInput), leftPlan);
@@ -510,8 +512,9 @@
             // leaf
             ILogicalOperator rightInput = joinLeafInputsHashMap.get(rightPlan.getEmptyTupleSourceOp());
             skipAllIndexes(rightPlan, rightInput);
-            if (rightInput.getOperatorTag() == LogicalOperatorTag.SELECT) {
-                addCardCostAnnotations(rightInput, rightPlan);
+            ILogicalOperator selOp = findSelectOrDataScan(rightInput);
+            if (selOp != null) {
+                addCardCostAnnotations(selOp, rightPlan);
             }
             joinOp.getInputs().get(1).setValue(rightInput);
             addCardCostAnnotations(findDataSourceScanOperator(rightInput), rightPlan);
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index e03a3e3..98fc560 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -690,21 +690,25 @@
       try {
         switch (hintToken.hint) {
           case SINGLE_DATASET_PREDICATE_SELECTIVITY_HINT:
-            selectivity = 1.0; // uninitialized
-            if (hintToken.hintParams != null) {
-            Matcher mat = lessThanOnePat.matcher(hintToken.hintParams);
-            if (mat.find()) {
-                selectivity = Double.parseDouble (mat.group());
-                }
+            if (hintToken.hintParams == null) {
+                throw new SqlppParseException(getSourceLocation(hintToken), "Expected selectivity value");
             }
-
-            return new PredicateCardinalityAnnotation(selectivity);
+            else {
+                selectivity = 1.0; // uninitialized
+                Matcher mat = lessThanOnePat.matcher(hintToken.hintParams);
+                if (mat.find()) {
+                    selectivity = Double.parseDouble (mat.group());
+                }
+                return new PredicateCardinalityAnnotation(selectivity);
+            }
           case JOIN_PREDICATE_PRODUCTIVITY_HINT:
-            productivity = 1.0; // uninitialized
-            String leftSideDataSet = null;
-            if (hintToken.hintParams != null) {
+            if (hintToken.hintParams == null) {
+                throw new SqlppParseException(getSourceLocation(hintToken), "Expected productivity value");
+            }
+            else {
+                productivity = 1.0; // uninitialized
+                String leftSideDataSet = null;
                 Matcher StringNum = stringNumber.matcher(hintToken.hintParams);
-
                 if (StringNum.find()) {
                     String matchedGroup = StringNum.group();
                     Pattern var = Pattern.compile("[a-zA-Z]\\w*"); // any word character [a-zA-Z_0-9]
@@ -715,9 +719,9 @@
                     if (numMat.find())
                         productivity = Double.parseDouble (numMat.group());
                 }
+                // attach hint to global scope
+                return new JoinProductivityAnnotation (productivity, leftSideDataSet);
             }
-            // attach hint to global scope
-            return new JoinProductivityAnnotation (productivity, leftSideDataSet);
           case HASH_BROADCAST_JOIN_HINT:
             if (hintToken.hintParams == null) {
               return new BroadcastExpressionAnnotation(BroadcastExpressionAnnotation.BroadcastSide.RIGHT);