[ASTERIXDB-3091][MTD] Make DROP TYPE derive correct names

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

Details:

DROP TYPE tries to derive the dependent nested anonymous types
to drop by name. However, when we insert these, for missable
fields and nested collections, the base type is used rather than
the quantified or outer type to create the name. This happens to
work most of the time hence this was not noticed for a very long
time.
This patch just fixes the two cases by using the unquantified type
for missable fields of records, and the item type rather than the
collection type for collections.

Change-Id: I67f3ea5bbd9dd4f5f4b836e765f42ccc1d7b13ca
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17296
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ian Maxon <imaxon@uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.000.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.000.ddl.sqlpp
new file mode 100644
index 0000000..5a225a9
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.000.ddl.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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 hanging IF EXISTS;
+CREATE DATAVERSE hanging;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.query.sqlpp
new file mode 100644
index 0000000..678e47d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.query.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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 * FROM Metadata.Datatype d
+WHERE d.DataverseName="hanging";
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.002.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.002.ddl.sqlpp
new file mode 100644
index 0000000..89fd9f0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.002.ddl.sqlpp
@@ -0,0 +1,37 @@
+/*
+ * 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 hanging;
+CREATE TYPE UserType AS {
+_oid: uuid,
+user_id: string,
+email: string,
+name:
+{ first: string?, last: string?, nesty: [ string ]?, nestier: { bar: bigint } }
+,
+address:
+{ street: string?, city: string?, state: string?, zip: int? }
+?,
+categories: [ string ],
+phone: [
+{ number: string?, kind: string?, nesty: [ string ]?, nestier: { bar: bigint } }
+],
+joined_date: string,
+is_buyer: boolean,
+is_seller: boolean
+};
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.query.sqlpp
new file mode 100644
index 0000000..678e47d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.query.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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 * FROM Metadata.Datatype d
+WHERE d.DataverseName="hanging";
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.004.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.004.ddl.sqlpp
new file mode 100644
index 0000000..de21a0d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.004.ddl.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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 hanging;
+DROP TYPE UserType;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.query.sqlpp
new file mode 100644
index 0000000..d82d9dc
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.query.sqlpp
@@ -0,0 +1,21 @@
+/*
+ * 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 hanging;
+SELECT * FROM Metadata.Datatype d
+WHERE d.DataverseName="hanging";
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.adm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.001.adm
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.adm
new file mode 100644
index 0000000..32a1d99
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.003.adm
@@ -0,0 +1,10 @@
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType", "Derived": { "Tag": "RECORD", "IsAnonymous": false, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "_oid", "FieldType": "uuid", "IsNullable": false, "IsMissable": false }, { "FieldName": "user_id", "FieldType": "string", "IsNullable": false, "IsMissable": false }, { "FieldName": "email", "FieldType": "string", "IsNullable": false, "IsMissable": false }, { "FieldName": "name", "FieldType": "UserType_name", "IsNullable": false, "IsMissable": false }, { "FieldName": "address", "FieldType": "UserType_address", "IsNullable": true, "IsMissable": true }, { "FieldName": "categories", "FieldType": "UserType_categories", "IsNullable": false, "IsMissable": false }, { "FieldName": "phone", "FieldType": "UserType_phone", "IsNullable": false, "IsMissable": false }, { "FieldName": "joined_date", "FieldType": "string", "IsNullable": false, "IsMissable": false }, { "FieldName": "is_buyer", "FieldType": "boolean", "IsNullable": false, "IsMissable": false }, { "FieldName": "is_seller", "FieldType": "boolean", "IsNullable": false, "IsMissable": false } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_address", "Derived": { "Tag": "RECORD", "IsAnonymous": true, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "street", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "city", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "state", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "zip", "FieldType": "int64", "IsNullable": true, "IsMissable": true } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_categories", "Derived": { "Tag": "ORDEREDLIST", "IsAnonymous": true, "OrderedList": "string" }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_name", "Derived": { "Tag": "RECORD", "IsAnonymous": true, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "first", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "last", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "nesty", "FieldType": "UserType_name_nesty", "IsNullable": true, "IsMissable": true }, { "FieldName": "nestier", "FieldType": "UserType_name_nestier", "IsNullable": false, "IsMissable": false } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_name_nestier", "Derived": { "Tag": "RECORD", "IsAnonymous": true, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "bar", "FieldType": "int64", "IsNullable": false, "IsMissable": false } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_name_nesty", "Derived": { "Tag": "ORDEREDLIST", "IsAnonymous": true, "OrderedList": "string" }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_phone", "Derived": { "Tag": "ORDEREDLIST", "IsAnonymous": true, "OrderedList": "UserType_phone_Item" }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_phone_Item", "Derived": { "Tag": "RECORD", "IsAnonymous": true, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "number", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "kind", "FieldType": "string", "IsNullable": true, "IsMissable": true }, { "FieldName": "nesty", "FieldType": "UserType_phone_Item_nesty", "IsNullable": true, "IsMissable": true }, { "FieldName": "nestier", "FieldType": "UserType_phone_Item_nestier", "IsNullable": false, "IsMissable": false } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_phone_Item_nestier", "Derived": { "Tag": "RECORD", "IsAnonymous": true, "Record": { "IsOpen": true, "Fields": [ { "FieldName": "bar", "FieldType": "int64", "IsNullable": false, "IsMissable": false } ] } }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
+{ "d": { "DataverseName": "hanging", "DatatypeName": "UserType_phone_Item_nesty", "Derived": { "Tag": "ORDEREDLIST", "IsAnonymous": true, "OrderedList": "string" }, "Timestamp": "Tue Mar 21 11:21:48 PDT 2023" } }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.adm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/drop-anonymous-nested-types/drop-anonymous-nested-types.005.adm
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 98a11ce..241e65e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -4369,6 +4369,11 @@
         <expected-error>Parameter invalid_date cannot be set</expected-error>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="ddl">
+      <compilation-unit name="drop-anonymous-nested-types">
+        <output-dir compare="Text">drop-anonymous-nested-types</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="dml">
     <test-case FilePath="dml">
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
index 87f5129..2195f88 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
@@ -106,9 +106,11 @@
 import org.apache.asterix.om.base.AInt32;
 import org.apache.asterix.om.base.AMutableString;
 import org.apache.asterix.om.base.AString;
+import org.apache.asterix.om.typecomputer.impl.TypeComputeUtils;
 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.AbstractCollectionType;
 import org.apache.asterix.om.types.AbstractComplexType;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
@@ -1483,15 +1485,20 @@
             ARecordType recType = (ARecordType) parentType.getDatatype();
             subTypes = Arrays.asList(recType.getFieldTypes());
         } else if (parentType.getDatatype().getTypeTag() == ATypeTag.UNION) {
-            AUnionType recType = (AUnionType) parentType.getDatatype();
-            subTypes = recType.getUnionList();
+            AUnionType unionType = (AUnionType) parentType.getDatatype();
+            subTypes = unionType.getUnionList();
+        } else if (parentType.getDatatype().getTypeTag() == ATypeTag.ARRAY
+                || parentType.getDatatype().getTypeTag() == ATypeTag.MULTISET) {
+            AbstractCollectionType collType = (AbstractCollectionType) parentType.getDatatype();
+            subTypes = List.of(collType.getItemType());
         }
 
         List<String> nestedTypes = new ArrayList<>();
         if (subTypes != null) {
             for (IAType subType : subTypes) {
-                if (!(subType instanceof BuiltinType)) {
-                    nestedTypes.add(subType.getTypeName());
+                IAType actualType = TypeComputeUtils.getActualType(subType);
+                if (!(actualType instanceof BuiltinType)) {
+                    nestedTypes.add(actualType.getTypeName());
                 }
             }
         }