This is an automated email from the ASF dual-hosted git repository.

mblow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git

commit 0ce5f5afe70b192a6bed851f55289a238f8e960a
Author: Dmitry Lychagin <[email protected]>
AuthorDate: Thu Apr 9 11:02:56 2020 -0700

    [NO ISSUE][COMP] Prohibit anonymous type use by CREATE DATASET
    
    - user model changes: no
    - storage format changes: no
    - interface changes: no
    
    Details:
    - CREATE DATASET should not be able to use anonymous types
    
    Change-Id: I0dcdd2684180b054ff48f0f0a8ccd6041e8bd00c
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/5664
    Integration-Tests: Jenkins <[email protected]>
    Tested-by: Jenkins <[email protected]>
    Reviewed-by: Hussain Towaileb <[email protected]>
---
 .../asterix/app/translator/QueryTranslator.java    | 17 ++++++++--
 .../create-dataset-inline-type-2.1.ddl.sqlpp       | 36 ++++++++++++++++++++++
 .../create-dataset-inline-type-2.2.query.sqlpp     | 25 +++++++++++++++
 .../create-dataset-inline-type-2.3.ddl.sqlpp       | 28 +++++++++++++++++
 .../create-dataset-inline-type-2.4.ddl.sqlpp       | 31 +++++++++++++++++++
 .../create-dataset-inline-type-2.2.adm             |  1 +
 .../test/resources/runtimets/testsuite_sqlpp.xml   |  7 +++++
 .../metadata/declared/MetadataManagerUtil.java     | 12 ++++++--
 .../metadata/declared/MetadataProvider.java        |  5 +++
 9 files changed, 158 insertions(+), 4 deletions(-)

diff --git 
a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
 
b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
index 81dce6d..c1eea7c 100644
--- 
a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
+++ 
b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
@@ -628,7 +628,13 @@ public class QueryTranslator extends 
AbstractLangTranslator implements IStatemen
             IAType itemType;
             switch (itemTypeExpr.getTypeKind()) {
                 case TYPEREFERENCE:
-                    itemType = 
metadataProvider.findType(itemTypeDataverseName, itemTypeName);
+                    Datatype itemTypeEntity = 
metadataProvider.findTypeEntity(itemTypeDataverseName, itemTypeName);
+                    if (itemTypeEntity == null || 
itemTypeEntity.getIsAnonymous()) {
+                        // anonymous types cannot be referred from CREATE 
DATASET
+                        throw new AsterixException(ErrorCode.UNKNOWN_TYPE, 
sourceLoc,
+                                itemTypeDataverseName + "." + itemTypeName);
+                    }
+                    itemType = itemTypeEntity.getDatatype();
                     break;
                 case RECORD:
                     itemTypeDataverseName = dataverseName;
@@ -664,7 +670,14 @@ public class QueryTranslator extends 
AbstractLangTranslator implements IStatemen
                     if (metaItemTypeExpr != null) {
                         switch (metaItemTypeExpr.getTypeKind()) {
                             case TYPEREFERENCE:
-                                metaItemType = 
metadataProvider.findType(metaItemTypeDataverseName, metaItemTypeName);
+                                Datatype metaItemTypeEntity =
+                                        
metadataProvider.findTypeEntity(metaItemTypeDataverseName, metaItemTypeName);
+                                if (metaItemTypeEntity == null || 
metaItemTypeEntity.getIsAnonymous()) {
+                                    // anonymous types cannot be referred from 
CREATE DATASET
+                                    throw new 
AsterixException(ErrorCode.UNKNOWN_TYPE, sourceLoc,
+                                            metaItemTypeDataverseName + "." + 
metaItemTypeName);
+                                }
+                                metaItemType = 
metaItemTypeEntity.getDatatype();
                                 if (metaItemType.getTypeTag() != 
ATypeTag.OBJECT) {
                                     throw new 
CompilationException(ErrorCode.COMPILATION_ERROR, sourceLoc,
                                             "Dataset meta type has to be a 
record type.");
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.1.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.1.ddl.sqlpp
new file mode 100644
index 0000000..d10d096
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.1.ddl.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.
+ */
+
+DROP DATAVERSE test IF EXISTS;
+CREATE DATAVERSE test;
+
+USE test;
+
+/* Prepare: create datasets with inline type */
+
+CREATE DATASET Cust1(
+  c_custkey integer not null,
+  c_name string not null
+) PRIMARY KEY c_custkey;
+
+CREATE DATASET Cust2(
+  c_custkey integer not null,
+  c_name string not null,
+  c_phone string
+) PRIMARY KEY c_custkey;
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.query.sqlpp
new file mode 100644
index 0000000..00cad73
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+SELECT count(*) as cnt
+FROM Metadata.`Datatype` dt
+WHERE dt.DataverseName = "test"
+ AND starts_with(dt.DatatypeName, "$d$t$i$Cust")
+ AND dt.Derived.IsAnonymous
+
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.3.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.3.ddl.sqlpp
new file mode 100644
index 0000000..81572f6
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.3.ddl.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * 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.
+ */
+
+/* Create dataset that attempts to use first dataset's type
+   as its item type -> Expect error: unknown type */
+
+USE test;
+
+CREATE DATASET Cust1X(
+  `$d$t$i$Cust1`
+)
+PRIMARY KEY c_custkey;
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.4.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.4.ddl.sqlpp
new file mode 100644
index 0000000..2ad4888
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.4.ddl.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+/*
+ * Create dataset that attempts to use first dataset's type
+ * as its meta item type -> Expect error: unknown type
+ */
+
+USE test;
+
+CREATE DATASET Cust2X(
+  c_custkey integer not null,
+  c_name string not null
+)
+WITH META(`$d$t$i$Cust2`)
+PRIMARY KEY c_custkey;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.adm
new file mode 100644
index 0000000..3591912
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/create-dataset-inline-type-2/create-dataset-inline-type-2.2.adm
@@ -0,0 +1 @@
+{ "cnt": 2 }
\ No newline at end of file
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 0d17fb1..63db153 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -3865,6 +3865,13 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="ddl">
+      <compilation-unit name="create-dataset-inline-type-2">
+        <output-dir compare="Text">create-dataset-inline-type-2</output-dir>
+        <expected-error>ASX1082: Cannot find datatype with name 
test.$d$t$i$Cust1</expected-error>
+        <expected-error>ASX1082: Cannot find datatype with name 
test.$d$t$i$Cust2</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="ddl">
       <compilation-unit name="drop-primary-index">
         <output-dir compare="Text">drop-primary-index</output-dir>
         <expected-error>Cannot drop index "ds". Drop dataset "ds" to remove 
this index</expected-error>
diff --git 
a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataManagerUtil.java
 
b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataManagerUtil.java
index e819d65..6317d94 100644
--- 
a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataManagerUtil.java
+++ 
b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataManagerUtil.java
@@ -23,6 +23,8 @@ import java.util.List;
 
 import org.apache.asterix.common.cluster.IClusterStateManager;
 import org.apache.asterix.common.config.DatasetConfig.DatasetType;
+import org.apache.asterix.common.exceptions.AsterixException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.metadata.MetadataManager;
 import org.apache.asterix.metadata.MetadataTransactionContext;
 import org.apache.asterix.metadata.entities.Dataset;
@@ -48,14 +50,20 @@ public class MetadataManagerUtil {
 
     public static IAType findType(MetadataTransactionContext mdTxnCtx, String 
dataverse, String typeName)
             throws AlgebricksException {
+        Datatype type = findTypeEntity(mdTxnCtx, dataverse, typeName);
+        return type != null ? type.getDatatype() : null;
+    }
+
+    public static Datatype findTypeEntity(MetadataTransactionContext mdTxnCtx, 
String dataverse, String typeName)
+            throws AlgebricksException {
         if (dataverse == null || typeName == null) {
             return null;
         }
         Datatype type = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, 
dataverse, typeName);
         if (type == null) {
-            throw new AlgebricksException("Type name '" + typeName + "' 
unknown in dataverse '" + dataverse + "'");
+            throw new AsterixException(ErrorCode.UNKNOWN_TYPE, dataverse + "." 
+ typeName);
         }
-        return type.getDatatype();
+        return type;
     }
 
     public static ARecordType findOutputRecordType(MetadataTransactionContext 
mdTxnCtx, String dataverse,
diff --git 
a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataProvider.java
 
b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataProvider.java
index 5bdf2a7..6b6cc78 100644
--- 
a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataProvider.java
+++ 
b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/declared/MetadataProvider.java
@@ -72,6 +72,7 @@ import 
org.apache.asterix.metadata.bootstrap.MetadataBuiltinEntities;
 import 
org.apache.asterix.metadata.dataset.hints.DatasetHints.DatasetCardinalityHint;
 import org.apache.asterix.metadata.entities.Dataset;
 import org.apache.asterix.metadata.entities.DatasourceAdapter;
+import org.apache.asterix.metadata.entities.Datatype;
 import org.apache.asterix.metadata.entities.Dataverse;
 import org.apache.asterix.metadata.entities.ExternalDatasetDetails;
 import org.apache.asterix.metadata.entities.Feed;
@@ -345,6 +346,10 @@ public class MetadataProvider implements 
IMetadataProvider<DataSourceId, String>
         return MetadataManagerUtil.findNodes(mdTxnCtx, nodeGroupName);
     }
 
+    public Datatype findTypeEntity(String dataverse, String typeName) throws 
AlgebricksException {
+        return MetadataManagerUtil.findTypeEntity(mdTxnCtx, dataverse, 
typeName);
+    }
+
     public IAType findType(String dataverse, String typeName) throws 
AlgebricksException {
         return MetadataManagerUtil.findType(mdTxnCtx, dataverse, typeName);
     }

Reply via email to