Luo Chen has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/3024

Change subject: [ASTERIXDB-2468] Extend CountVarToCountOneRule to support no 
group-by
......................................................................

[ASTERIXDB-2468] Extend CountVarToCountOneRule to support no group-by

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

Details:
- Extend CountVarToCountOneRule to support the no group-by case so
that count queries without group-by can be handled as well.

Change-Id: Ie0808912cf45d3a914ebf7f69ec42f73b33c071f
---
M 
asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/CountVarToCountOneRule.java
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.1.ddl.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.2.update.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.3.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
M asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
6 files changed, 166 insertions(+), 35 deletions(-)


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

diff --git 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/CountVarToCountOneRule.java
 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/CountVarToCountOneRule.java
index 5205672..e139850 100644
--- 
a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/CountVarToCountOneRule.java
+++ 
b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/CountVarToCountOneRule.java
@@ -45,47 +45,70 @@
         return false;
     }
 
-    // It is only for a group-by having just one aggregate which is a count.
+    // It is only for a group-by having just one count or a single count 
without group-by
     @Override
     public boolean rewritePost(Mutable<ILogicalOperator> opRef, 
IOptimizationContext context)
             throws AlgebricksException {
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) 
opRef.getValue();
-        if (op1.getOperatorTag() != LogicalOperatorTag.GROUP) {
+        if (op1.getOperatorTag() == LogicalOperatorTag.GROUP) {
+            GroupByOperator g = (GroupByOperator) op1;
+            if (g.getNestedPlans().size() != 1) {
+                return false;
+            }
+            ILogicalPlan p = g.getNestedPlans().get(0);
+            if (p.getRoots().size() != 1) {
+                return false;
+            }
+            AbstractLogicalOperator op2 = (AbstractLogicalOperator) 
p.getRoots().get(0).getValue();
+            if (op2.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
+                return false;
+            }
+            AggregateOperator agg = (AggregateOperator) op2;
+            if (agg.getExpressions().size() != 1) {
+                return false;
+            }
+            ILogicalExpression exp2 = agg.getExpressions().get(0).getValue();
+            if (exp2.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) 
{
+                return false;
+            }
+            AbstractFunctionCallExpression fun = 
(AbstractFunctionCallExpression) exp2;
+            if (fun.getFunctionIdentifier() != BuiltinFunctions.COUNT
+                    && fun.getFunctionIdentifier() != 
BuiltinFunctions.SQL_COUNT) {
+                return false;
+            }
+            ILogicalExpression exp3 = fun.getArguments().get(0).getValue();
+            if (exp3.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
+                return false;
+            }
+            if (agg.getInputs().get(0).getValue().getOperatorTag() != 
LogicalOperatorTag.NESTEDTUPLESOURCE) {
+                return false;
+            }
+            fun.getArguments().get(0).setValue(new ConstantExpression(new 
AsterixConstantValue(new AInt64(1L))));
+            return true;
+        } else if (op1.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
+            AggregateOperator agg = (AggregateOperator) op1;
+            if (agg.getExpressions().size() != 1) {
+                return false;
+            }
+            ILogicalExpression exp2 = agg.getExpressions().get(0).getValue();
+            if (exp2.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) 
{
+                return false;
+            }
+            AbstractFunctionCallExpression fun = 
(AbstractFunctionCallExpression) exp2;
+            if (fun.getFunctionIdentifier() != BuiltinFunctions.COUNT
+                    && fun.getFunctionIdentifier() != 
BuiltinFunctions.SQL_COUNT) {
+                return false;
+            }
+            ILogicalExpression exp3 = fun.getArguments().get(0).getValue();
+            if (exp3.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
+                return false;
+            }
+            fun.getArguments().get(0).setValue(new ConstantExpression(new 
AsterixConstantValue(new AInt64(1L))));
+            return true;
+        } else {
             return false;
         }
-        GroupByOperator g = (GroupByOperator) op1;
-        if (g.getNestedPlans().size() != 1) {
-            return false;
-        }
-        ILogicalPlan p = g.getNestedPlans().get(0);
-        if (p.getRoots().size() != 1) {
-            return false;
-        }
-        AbstractLogicalOperator op2 = (AbstractLogicalOperator) 
p.getRoots().get(0).getValue();
-        if (op2.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
-            return false;
-        }
-        AggregateOperator agg = (AggregateOperator) op2;
-        if (agg.getExpressions().size() != 1) {
-            return false;
-        }
-        ILogicalExpression exp2 = agg.getExpressions().get(0).getValue();
-        if (exp2.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
-            return false;
-        }
-        AbstractFunctionCallExpression fun = (AbstractFunctionCallExpression) 
exp2;
-        if (fun.getFunctionIdentifier() != BuiltinFunctions.COUNT) {
-            return false;
-        }
-        ILogicalExpression exp3 = fun.getArguments().get(0).getValue();
-        if (exp3.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
-            return false;
-        }
-        if (agg.getInputs().get(0).getValue().getOperatorTag() != 
LogicalOperatorTag.NESTEDTUPLESOURCE) {
-            return false;
-        }
-        fun.getArguments().get(0).setValue(new ConstantExpression(new 
AsterixConstantValue(new AInt64(1L))));
-        return true;
+
     }
 
 }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.1.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.1.ddl.sqlpp
new file mode 100644
index 0000000..a048870
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.1.ddl.sqlpp
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+drop  dataverse Twitter if exists;
+create  dataverse Twitter;
+
+use Twitter;
+
+
+create type TweetType as{
+  id : int,
+  sid : int,
+  message : string
+};
+
+create  dataset Tweet(TweetType) primary key id;
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.2.update.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.2.update.sqlpp
new file mode 100644
index 0000000..bd244d0
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.2.update.sqlpp
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.3.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.3.query.sqlpp
new file mode 100644
index 0000000..e65a7e8
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/count_dataset/count_dataset.3.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+use Twitter;
+
+explain select count(tmp) from (select * from Tweet where sid>=1 AND sid<=10 
order by id) tmp;
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
new file mode 100644
index 0000000..ef3da94
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
@@ -0,0 +1,30 @@
+distribute result [$$45]
+-- DISTRIBUTE_RESULT  |UNPARTITIONED|
+  exchange
+  -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+    project ([$$45])
+    -- STREAM_PROJECT  |UNPARTITIONED|
+      assign [$$45] <- [{"$1": $$48}]
+      -- ASSIGN  |UNPARTITIONED|
+        aggregate [$$48] <- [agg-sql-sum($$50)]
+        -- AGGREGATE  |UNPARTITIONED|
+          aggregate [$$50] <- [agg-sql-count(1)]
+          -- AGGREGATE  |PARTITIONED|
+            exchange
+            -- SORT_MERGE_EXCHANGE [$$47(ASC) ]  |PARTITIONED|
+              project ([$$47])
+              -- STREAM_PROJECT  |PARTITIONED|
+                select (and(ge($$46, 1), le($$46, 10)))
+                -- STREAM_SELECT  |PARTITIONED|
+                  project ([$$47, $$46])
+                  -- STREAM_PROJECT  |PARTITIONED|
+                    assign [$$46] <- [$$Tweet.getField(1)]
+                    -- ASSIGN  |PARTITIONED|
+                      exchange
+                      -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                        data-scan []<-[$$47, $$Tweet] <- Twitter.Tweet
+                        -- DATASOURCE_SCAN  |PARTITIONED|
+                          exchange
+                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                            empty-tuple-source
+                            -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml 
b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index e52449c..8631b84 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1301,6 +1301,11 @@
         <output-dir compare="Text">count_01</output-dir>
       </compilation-unit>
     </test-case>
+       <test-case FilePath="aggregate">
+      <compilation-unit name="count_dataset">
+        <output-dir compare="Text">count_dataset</output-dir>
+      </compilation-unit>
+    </test-case>
     <test-case FilePath="aggregate-sql">
       <compilation-unit name="count_empty_01">
         <output-dir compare="Text">count_empty_01</output-dir>

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/3024
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie0808912cf45d3a914ebf7f69ec42f73b33c071f
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Luo Chen <[email protected]>

Reply via email to