You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by bu...@apache.org on 2017/08/09 01:56:54 UTC

asterixdb git commit: [ASTERIXDB-1897][COMP][RT] Fix complex group-by.

Repository: asterixdb
Updated Branches:
  refs/heads/master 07075667c -> bf1aea2ac


[ASTERIXDB-1897][COMP][RT] Fix complex group-by.

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

Details:
- Fix type computer for numeric aggregations;
- Fix error reporting for SubplanRuntimeFactory;
- Add a negative test query.

Change-Id: Iebb393820a8edd0c54d80248b2a33c77d4f6fd7b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1923
Sonar-Qube: Jenkins <je...@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <ti...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/asterixdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/asterixdb/commit/bf1aea2a
Tree: http://git-wip-us.apache.org/repos/asf/asterixdb/tree/bf1aea2a
Diff: http://git-wip-us.apache.org/repos/asf/asterixdb/diff/bf1aea2a

Branch: refs/heads/master
Commit: bf1aea2acdc094aa34626e15679eaacf7b5a28de
Parents: 0707566
Author: Yingyi Bu <yi...@couchbase.com>
Authored: Tue Aug 8 11:48:53 2017 -0700
Committer: Yingyi Bu <bu...@gmail.com>
Committed: Tue Aug 8 18:55:46 2017 -0700

----------------------------------------------------------------------
 .../group-by/policy-05/policy-05.1.ddl.sqlpp    |  29 ++++
 .../group-by/policy-05/policy-05.2.update.sqlpp | 137 +++++++++++++++++++
 .../group-by/policy-05/policy-05.3.query.sqlpp  |  36 +++++
 .../resources/runtimets/testsuite_sqlpp.xml     |   6 +
 .../impl/NumericAggTypeComputer.java            |  11 +-
 .../om/typecomputer/impl/TypeComputeUtils.java  |  31 ++---
 .../operators/meta/SubplanRuntimeFactory.java   |   8 +-
 7 files changed, 233 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.1.ddl.sqlpp
new file mode 100644
index 0000000..0242f1c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.1.ddl.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * 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 gby IF EXISTS;
+CREATE DATAVERSE gby;
+
+USE gby;
+
+CREATE TYPE PolicyType AS {
+    id: UUID
+}
+
+CREATE DATASET policies(PolicyType) PRIMARY KEY id AUTOGENERATED;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.2.update.sqlpp
new file mode 100644
index 0000000..3b865bb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.2.update.sqlpp
@@ -0,0 +1,137 @@
+/*
+ * 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 gby;
+
+INSERT INTO policies
+(
+  [ {
+    "policyno": "C123",
+    "state": "CA",
+    "zipcode": "96008",
+    "make": "Honda",
+    "accidents": [
+          {
+            "year": "2015",
+            "cost": 5000
+          },
+          {
+            "year": "2016",
+            "cost": 8000
+          },
+          {
+            "year": "2016",
+            "cost": 6000
+          }
+    ]
+  },
+  {
+    "policyno": "C124",
+    "state": "CA",
+    "zipcode": "96853",
+    "make": "Ford",
+    "accidents": [
+          {
+            "year": "2015",
+            "cost": 5000
+          },
+          {
+            "year": "2015",
+            "cost": 8000
+          },
+          {
+            "year": "2016",
+            "cost": 6000
+          }
+    ]
+  },
+  {
+    "policyno": "A123",
+    "state": "AZ",
+    "zipcode": "86008",
+    "make": "Honda",
+    "accidents": [
+        {
+          "year": "2015",
+          "cost": 5000
+        },
+        {
+          "year": "2016",
+          "cost": 8000
+        },
+        {
+          "year": "2016",
+          "cost": 6000
+        }
+    ]
+  },
+  {
+    "policyno": "A124",
+    "state": "AZ",
+    "zipcode": "86853",
+    "make": "Ford",
+    "accidents": [
+        {
+          "year": "2015",
+          "cost": 5000
+        },
+        {
+          "year": "2016",
+          "cost": 8000
+        },
+        {
+          "year": "2016",
+          "cost": 6000
+        }
+    ]
+  },
+  {
+    "policyno": "U123",
+    "state": "UT",
+    "zipcode": "66008",
+    "make": "Honda",
+    "accidents": [
+        {
+          "year": "2015",
+          "cost": 5000
+        },
+        {
+          "year": "2016",
+          "cost": 8000
+        },
+        {
+          "year": "2016",
+          "cost": 6000
+        }
+    ]
+  },
+  {
+    "policyno": "U124",
+    "state": "UT",
+    "zipcode": "66853",
+    "make": "Ford",
+    "accidents": [ ]
+  },
+  {
+    "policyno": "U125",
+    "state": "UT",
+    "zipcode": "66853",
+    "make": "Ford"
+  } ]
+);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
new file mode 100644
index 0000000..9a03c50
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
@@ -0,0 +1,36 @@
+/*
+ * 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 gby;
+
+FROM policies p
+GROUP BY state GROUP AS g
+SELECT state,
+       (
+         FROM g
+         SELECT VALUE SUM(
+                  (
+                    FROM g.p.accidents a
+                    WHERE a.year = "2016"
+                    SELECT COUNT(*)
+                   )[0]
+                )
+       )[0]  / (COUNT(*) * 1.0 ) AS risk
+ORDER BY risk DESC
+LIMIT 5;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
----------------------------------------------------------------------
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 47c0529..70b3486 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -2881,6 +2881,12 @@
         <output-dir compare="Text">policy-04</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="group-by">
+      <compilation-unit name="policy-05">
+        <output-dir compare="Text">none</output-dir>
+        <expected-error>Invalid item type: function agg-sum cannot process item type object in an input array (or multiset)</expected-error>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="index-join">
     <test-case FilePath="index-join">

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/NumericAggTypeComputer.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/NumericAggTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/NumericAggTypeComputer.java
index f2ae015..ec9c59f 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/NumericAggTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/NumericAggTypeComputer.java
@@ -22,6 +22,7 @@ import org.apache.asterix.om.exceptions.UnsupportedTypeException;
 import org.apache.asterix.om.typecomputer.base.AbstractResultTypeComputer;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.AUnionType;
+import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
@@ -52,7 +53,6 @@ public class NumericAggTypeComputer extends AbstractResultTypeComputer {
     @Override
     protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
         ATypeTag tag = strippedInputTypes[0].getTypeTag();
-        IAType type = null;
         switch (tag) {
             case DOUBLE:
             case FLOAT:
@@ -61,11 +61,12 @@ public class NumericAggTypeComputer extends AbstractResultTypeComputer {
             case SMALLINT:
             case TINYINT:
             case ANY:
-                type = strippedInputTypes[0];
-                break;
+                IAType type = strippedInputTypes[0];
+                return AUnionType.createNullableType(type, "AggResult");
             default:
-                break;
+                // All other possible cases.
+                return BuiltinType.ANULL;
         }
-        return AUnionType.createNullableType(type, "AggResult");
+
     }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/TypeComputeUtils.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/TypeComputeUtils.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/TypeComputeUtils.java
index a1bbb86..d6825e3 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/TypeComputeUtils.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/TypeComputeUtils.java
@@ -22,7 +22,6 @@ import org.apache.asterix.om.types.AOrderedListType;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.AUnionType;
-import org.apache.asterix.om.types.AUnorderedListType;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
 import org.apache.asterix.om.utils.RecordUtil;
@@ -102,6 +101,21 @@ public class TypeComputeUtils {
             if (category == NULL) {
                 return BuiltinType.ANULL;
             }
+            boolean metNull = false;
+            for (IAType knownInputType : knownInputTypes) {
+                ATypeTag typeTag = knownInputType.getTypeTag();
+                // Returns missing if there is one missing.
+                if (typeTag == ATypeTag.MISSING) {
+                    return knownInputType;
+                }
+                if (typeTag == ATypeTag.NULL) {
+                    metNull = true;
+                }
+            }
+            // Returns null if there is one null but there is no missing.
+            if (metNull) {
+                return BuiltinType.ANULL;
+            }
             return TypeComputeUtils.getResultType(resultTypeGenerator.getResultType(expr, knownInputTypes), category);
         } else {
             return resultTypeGenerator.getResultType(expr, knownInputTypes);
@@ -218,19 +232,4 @@ public class TypeComputeUtils {
         return null;
     }
 
-    public static AUnorderedListType extractUnorderedListType(IAType t) {
-        if (t.getTypeTag() == ATypeTag.MULTISET) {
-            return (AUnorderedListType) t;
-        }
-        if (t.getTypeTag() == ATypeTag.UNION) {
-            AUnionType unionType = (AUnionType) t;
-            IAType innerType = unionType.getActualType();
-            if (innerType.getTypeTag() == ATypeTag.MULTISET) {
-                return (AUnorderedListType) innerType;
-            }
-        }
-
-        return null;
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/bf1aea2a/hyracks-fullstack/algebricks/algebricks-runtime/src/main/java/org/apache/hyracks/algebricks/runtime/operators/meta/SubplanRuntimeFactory.java
----------------------------------------------------------------------
diff --git a/hyracks-fullstack/algebricks/algebricks-runtime/src/main/java/org/apache/hyracks/algebricks/runtime/operators/meta/SubplanRuntimeFactory.java b/hyracks-fullstack/algebricks/algebricks-runtime/src/main/java/org/apache/hyracks/algebricks/runtime/operators/meta/SubplanRuntimeFactory.java
index 8b13e09..f6a349f 100644
--- a/hyracks-fullstack/algebricks/algebricks-runtime/src/main/java/org/apache/hyracks/algebricks/runtime/operators/meta/SubplanRuntimeFactory.java
+++ b/hyracks-fullstack/algebricks/algebricks-runtime/src/main/java/org/apache/hyracks/algebricks/runtime/operators/meta/SubplanRuntimeFactory.java
@@ -91,7 +91,8 @@ public class SubplanRuntimeFactory extends AbstractOneInputOneOutputRuntimeFacto
                 private boolean smthWasWritten = false;
                 private FrameTupleAccessor ta = new FrameTupleAccessor(
                         pipeline.getRecordDescriptors()[pipeline.getRecordDescriptors().length - 1]);
-                private ArrayTupleBuilder tb = new ArrayTupleBuilder(nullWriters.length);
+                private ArrayTupleBuilder tb = new ArrayTupleBuilder(
+                        nullWriters.length + SubplanRuntimeFactory.this.inputRecordDesc.getFieldCount());
 
                 @Override
                 public void open() throws HyracksDataException {
@@ -110,17 +111,16 @@ public class SubplanRuntimeFactory extends AbstractOneInputOneOutputRuntimeFacto
 
                 @Override
                 public void close() throws HyracksDataException {
-                    if (!smthWasWritten) {
+                    if (!smthWasWritten && !failed) {
                         // the case when we need to write nulls
                         appendNullsToTuple();
                         appendToFrameFromTupleBuilder(tb);
                     }
-
                 }
 
                 @Override
                 public void fail() throws HyracksDataException {
-                    writer.fail();
+                    // writer.fail() is called by the outer class' writer.fail().
                 }
 
                 private void appendNullsToTuple() throws HyracksDataException {