[ASTERIXDB-3302][COMP] Compilation error when creating index
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
Allow index creation on a field having a closed-type when there
type is provided in the index creation statement as long as
the type provided is same as their closed-type.
Change-Id: Ie662ce1276fa60493cf0a7623306cb19448a2133
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17930
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Peeyush Gupta <peeyush.gupta@couchbase.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
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 f0cc75b..c2be64f 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
@@ -1409,18 +1409,30 @@
fieldTypeMissable = projectTypeMissable;
} else {
// the type of the indexed field is explicitly specified in the DDL
+ Map<TypeSignature, IAType> typeMap = TypeTranslator.computeTypes(databaseName, dataverseName,
+ indexName, projectTypeExpr.getType(), databaseName, dataverseName, mdTxnCtx);
+ TypeSignature typeSignature = new TypeSignature(databaseName, dataverseName, indexName);
+ fieldTypePrime = typeMap.get(typeSignature);
+ // BACK-COMPAT: keep prime type only if we're overriding field types
+ fieldTypeNullable = fieldTypeMissable = false;
+ overridesFieldTypes = true;
+
if (stmtCreateIndex.isEnforced()) {
if (!projectTypeExpr.isUnknownable()) {
throw new CompilationException(ErrorCode.INDEX_ILLEGAL_ENFORCED_NON_OPTIONAL,
indexedElement.getSourceLocation(),
LogRedactionUtil.userData(String.valueOf(projectPath)));
}
- // don't allow creating an enforced index on a closed-type field, fields that
- // are part of schema get the field type, if it's not null, then the field is closed-type
+ // don't allow creating an enforced index on a closed-type field having field type different
+ // from the closed-type
if (isFieldFromSchema) {
- throw new CompilationException(ErrorCode.INDEX_ILLEGAL_ENFORCED_ON_CLOSED_FIELD,
- indexedElement.getSourceLocation(),
- LogRedactionUtil.userData(String.valueOf(projectPath)));
+ if (fieldTypePrime == null) {
+ throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE,
+ indexedElement.getSourceLocation(), "cannot find type of field");
+ } else if (!projectTypePrime.deepEqual(fieldTypePrime)) {
+ throw new CompilationException(ErrorCode.TYPE_MISMATCH_GENERIC, sourceLoc,
+ projectTypePrime.getTypeTag(), fieldTypePrime.getTypeTag());
+ }
}
} else {
if (indexType != IndexType.BTREE && indexType != IndexType.ARRAY) {
@@ -1430,23 +1442,16 @@
if (isFieldFromSchema) {
// allow overriding the type of the closed-field only if CAST modifier is used
if (!stmtCreateIndex.hasCastDefaultNull()) {
- throw new CompilationException(ErrorCode.COMPILATION_ERROR,
- indexedElement.getSourceLocation(),
- "Typed index on '"
- + LogRedactionUtil
- .userData(RecordUtil.toFullyQualifiedName(projectPath))
- + "' field could be created only for open datatype");
+ if (fieldTypePrime == null) {
+ throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE,
+ indexedElement.getSourceLocation(), "cannot find type of field");
+ } else if (!projectTypePrime.deepEqual(fieldTypePrime)) {
+ throw new CompilationException(ErrorCode.TYPE_MISMATCH_GENERIC, sourceLoc,
+ projectTypePrime.getTypeTag(), fieldTypePrime.getTypeTag());
+ }
}
}
}
-
- Map<TypeSignature, IAType> typeMap = TypeTranslator.computeTypes(databaseName, dataverseName,
- indexName, projectTypeExpr.getType(), databaseName, dataverseName, mdTxnCtx);
- TypeSignature typeSignature = new TypeSignature(databaseName, dataverseName, indexName);
- fieldTypePrime = typeMap.get(typeSignature);
- // BACK-COMPAT: keep prime type only if we're overriding field types
- fieldTypeNullable = fieldTypeMissable = false;
- overridesFieldTypes = true;
}
if (fieldTypePrime == null) {
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.000.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.000.ddl.sqlpp
new file mode 100644
index 0000000..524db48
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.000.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.
+ */
+
+CREATE DATAVERSE test;
+USE test;
+CREATE TYPE t1 AS { id: int, name: string, phone:string };
+
+CREATE DATASET ds1(t1) primary key id;
+CREATE DATASET ds2 primary key (id:int, name:string);
+
+CREATE INDEX idx0 ON ds1(name);
+CREATE INDEX idx1 ON ds1(name: string);
+CREATE INDEX idx2 ON ds1(name: string, id:int);
+CREATE INDEX idx3 ON ds2(name: string);
+CREATE INDEX idx4 ON ds2(phone: string, id:int);
+CREATE INDEX idx5 ON ds2(name);
+CREATE INDEX idx6 ON ds2(phone:string, id);
+CREATE INDEX idx7 ON ds2(name:string?) ENFORCED;
+CREATE INDEX idx8 ON ds1(name:int) CAST (DEFAULT NULL);
+CREATE INDEX idx9 ON ds2(name:int) CAST (DEFAULT NULL);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.001.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.001.update.sqlpp
new file mode 100644
index 0000000..504477e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.001.update.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 test;
+INSERT INTO ds1 [{"id": 1, "name": "John", "phone":"982819"}, {"id": 2, "name": "Jason", "phone":"982819"}];
+INSERT INTO ds2 [{"id": 1, "name": "John", "phone":982819}, {"id": 2, "name": "Jason", "phone":"982819"}];
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.002.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.002.ddl.sqlpp
new file mode 100644
index 0000000..67d4208
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.002.ddl.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+/*
+ * Description : Test secondary indexes bulk load where some tuples have malformed values in indexed fields
+ * Expected Res : Said tuples are not indexed
+ */
+
+USE test;
+
+CREATE INDEX idxf1 ON ds1(name: int);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.003.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.003.ddl.sqlpp
new file mode 100644
index 0000000..cc0ae73
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.003.ddl.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+/*
+ * Description : Test secondary indexes bulk load where some tuples have malformed values in indexed fields
+ * Expected Res : Said tuples are not indexed
+ */
+
+USE test;
+
+CREATE INDEX idxf2 ON ds2(name: int);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.004.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.004.ddl.sqlpp
new file mode 100644
index 0000000..508ec76
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.004.ddl.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+/*
+ * Description : Test secondary indexes bulk load where some tuples have malformed values in indexed fields
+ * Expected Res : Said tuples are not indexed
+ */
+
+USE test;
+
+CREATE INDEX idxf2 ON ds1(name: int?) ENFORCED;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.005.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.005.query.sqlpp
new file mode 100644
index 0000000..c1a403f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.005.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.
+ */
+
+USE test;
+
+SET `import-private-functions` `true`;
+FROM DUMP_INDEX("test", "ds2", "idx4") AS v
+SELECT VALUE v
+ORDER BY v.values;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.006.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.006.query.sqlpp
new file mode 100644
index 0000000..aad3a3e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.006.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 test;
+
+SELECT (FROM ds1 SELECT VALUE ds1 ORDER BY id) AS r1, (FROM ds2 SELECT VALUE ds2 ORDER BY id) AS r2;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.007.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.007.query.sqlpp
new file mode 100644
index 0000000..14f2ebd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.007.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.
+ */
+
+USE test;
+
+SET `import-private-functions` `true`;
+FROM DUMP_INDEX("test", "ds1", "idx1") AS v
+SELECT VALUE v
+ORDER BY v.values;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.999.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.999.ddl.sqlpp
new file mode 100644
index 0000000..86a1b59
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/ddl/index-closed-type/index-closed-type.999.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 test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.005.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.005.adm
new file mode 100644
index 0000000..cc4f892
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.005.adm
@@ -0,0 +1,2 @@
+{ "values": [ 1, 1, "John" ] }
+{ "values": [ "982819", 2, 2, "Jason" ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.006.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.006.adm
new file mode 100644
index 0000000..9a50304
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.006.adm
@@ -0,0 +1 @@
+{ "r1": [ { "id": 1, "name": "John", "phone": "982819" }, { "id": 2, "name": "Jason", "phone": "982819" } ], "r2": [ { "id": 1, "name": "John", "phone": 982819 }, { "id": 2, "name": "Jason", "phone": "982819" } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.007.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.007.adm
new file mode 100644
index 0000000..45fbd09
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/ddl/index-closed-type/index-closed-type.007.adm
@@ -0,0 +1,2 @@
+{ "values": [ "Jason", 2 ] }
+{ "values": [ "John", 1 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
index 66286b0..c18075d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
@@ -4402,7 +4402,6 @@
<expected-error>CAST modifier is only allowed for B-Tree indexes</expected-error>
<expected-error>CAST modifier cannot be specified together with ENFORCED</expected-error>
<expected-error>CAST modifier is used without specifying the type of the indexed field</expected-error>
- <expected-error>Typed index on 'typed_f2' field could be created only for open datatype</expected-error>
<expected-error>Parameter invalid_date cannot be set</expected-error>
</compilation-unit>
</test-case>
@@ -4411,6 +4410,14 @@
<output-dir compare="Text">drop-anonymous-nested-types</output-dir>
</compilation-unit>
</test-case>
+ <test-case FilePath="ddl">
+ <compilation-unit name="index-closed-type">
+ <output-dir compare="Text">index-closed-type</output-dir>
+ <expected-error>ASX0037: Type mismatch: expected value of type string, but got the value of type bigint</expected-error>
+ <expected-error>ASX0037: Type mismatch: expected value of type string, but got the value of type bigint</expected-error>
+ <expected-error>ASX0037: Type mismatch: expected value of type string, but got the value of type bigint</expected-error>
+ </compilation-unit>
+ </test-case>
</test-group>
<test-group name="dml">
<test-case FilePath="dml">
@@ -7410,13 +7417,12 @@
<test-case FilePath="open-index-enforced/error-checking">
<compilation-unit name="enforced-field-name-collision">
<output-dir compare="Text">enforced-field-name-collision</output-dir>
- <expected-error>Cannot create enforced index on '[value]' field. The field is closed type.</expected-error>
</compilation-unit>
</test-case>
<test-case FilePath="open-index-enforced/error-checking">
<compilation-unit name="enforced-field-type-collision">
<output-dir compare="Text">enforced-field-type-collision</output-dir>
- <expected-error>Cannot create enforced index on '[value]' field. The field is closed type.</expected-error>
+ <expected-error>ASX0037: Type mismatch: expected value of type string, but got the value of type integer</expected-error>
</compilation-unit>
</test-case>
<test-case FilePath="open-index-enforced/error-checking">
@@ -7452,7 +7458,7 @@
<test-case FilePath="open-index-enforced/error-checking">
<compilation-unit name="object-type-collision">
<output-dir compare="Text">object-type-collision</output-dir>
- <expected-error>ASX1051: Cannot create enforced index on '[value]' field. The field is closed type.</expected-error>
+ <expected-error>ASX0037: Type mismatch: expected value of type string, but got the value of type integer</expected-error>
</compilation-unit>
</test-case>
</test-group>