[ASTERIXDB-2296][COMP] proper handling of an optional subfield type
- user-model changes: no
- storage format changes: no
- interface changes: no
Details:
- Handle an optional subfield type properly in
AbstractIntroduceAccessMethodRule.getFieldNameFromSubTree()
Change-Id: Ia80b6dbf549b77295f310b7c6f3a3f3f5a323114
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2419
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
index f4178d7..4f9b4df 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
@@ -30,6 +30,8 @@
import org.apache.asterix.common.config.DatasetConfig.DatasetType;
import org.apache.asterix.common.config.DatasetConfig.IndexType;
+import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
import org.apache.asterix.dataflow.data.common.ExpressionTypeComputer;
import org.apache.asterix.metadata.declared.MetadataProvider;
import org.apache.asterix.metadata.entities.Index;
@@ -38,6 +40,7 @@
import org.apache.asterix.om.base.AString;
import org.apache.asterix.om.constants.AsterixConstantValue;
import org.apache.asterix.om.functions.BuiltinFunctions;
+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.AbstractCollectionType;
@@ -872,9 +875,15 @@
}
if (!isByName) {
- fieldName = sourceVar.equals(metaVar)
- ? ((ARecordType) metaType.getSubFieldType(parentFieldNames)).getFieldNames()[fieldIndex]
- : ((ARecordType) recordType.getSubFieldType(parentFieldNames)).getFieldNames()[fieldIndex];
+ IAType subFieldType = sourceVar.equals(metaVar) ? metaType.getSubFieldType(parentFieldNames)
+ : recordType.getSubFieldType(parentFieldNames);
+ // Sub-field type can be AUnionType in case if it's optional. Thus, needs to get the actual type.
+ subFieldType = TypeComputeUtils.getActualType(subFieldType);
+ if (subFieldType.getTypeTag() != ATypeTag.OBJECT) {
+ throw CompilationException.create(ErrorCode.TYPE_CONVERT, subFieldType,
+ ARecordType.class.getName());
+ }
+ fieldName = ((ARecordType) subFieldType).getFieldNames()[fieldIndex];
}
if (optFuncExpr != null) {
optFuncExpr.setSourceVar(funcVarIndex, ((AssignOperator) op).getVariables().get(assignVarIndex));
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/filter/inverted-btree-search-return-optional-field.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/filter/inverted-btree-search-return-optional-field.sqlpp
new file mode 100644
index 0000000..3221cc3
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/filter/inverted-btree-search-return-optional-field.sqlpp
@@ -0,0 +1,91 @@
+/*
+ * 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 if not exists;
+use twitter;
+
+create type typeUser if not exists as open {
+ id: int64,
+ name: string,
+ screen_name : string,
+ profile_image_url : string?,
+ lang : string,
+ location: string,
+ create_at: date,
+ description: string,
+ followers_count: int32,
+ friends_count: int32,
+ statues_count: int64
+};
+
+create type typePlace if not exists as open{
+ country : string,
+ country_code : string,
+ full_name : string,
+ id : string,
+ name : string,
+ place_type : string,
+ bounding_box : rectangle
+};
+
+create type typeGeoTag if not exists as open {
+ stateID: int32,
+ stateName: string,
+ countyID: int32,
+ countyName: string,
+ cityID: int32?,
+ cityName: string?
+};
+
+create type typeTweet if not exists as open {
+ create_at : datetime,
+ id: int64,
+ text: string,
+ in_reply_to_status : int64,
+ in_reply_to_user : int64,
+ favorite_count : int64,
+ coordinate: point?,
+ retweet_count : int64,
+ lang : string,
+ is_retweet: boolean,
+ hashtags : {{ string }} ?,
+ user_mentions : {{ int64 }} ? ,
+ user : typeUser,
+ place : typePlace?,
+ geo_tag: typeGeoTag
+};
+
+create dataset ds_tweet(typeTweet) if not exists primary key id with filter on create_at with
+{"merge-policy":
+ {"name":"prefix","parameters":
+ {"max-mergable-component-size":134217728, "max-tolerance-component-count":10}
+ }
+};
+
+create index text_idx if not exists on ds_tweet(text) type fulltext;
+
+select t.`place`.`bounding_box` as `place.bounding_box`,t.`user`.`id` as `user.id`,t.`id` as `id`,
+ t.`coordinate` as `coordinate`,t.`create_at` as `create_at`
+from twitter.ds_tweet t
+where t.`create_at` >= datetime('2018-02-22T10:53:07.888Z') and t.`create_at` < datetime('2018-02-22T18:50:39.301Z')
+ and ftcontains(t.`text`, ['francisco'], {'mode':'all'}) and t.`geo_tag`.`stateID` in [ 37,51,24,11 ]
+order by t.`create_at` desc
+limit 2147483647
+offset 0;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/filter/inverted-btree-search-return-optional-field.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/filter/inverted-btree-search-return-optional-field.plan
new file mode 100644
index 0000000..629a05a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/filter/inverted-btree-search-return-optional-field.plan
@@ -0,0 +1,43 @@
+-- DISTRIBUTE_RESULT |UNPARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |UNPARTITIONED|
+ -- STREAM_LIMIT |UNPARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- SORT_MERGE_EXCHANGE [$$37(DESC) ] |PARTITIONED|
+ -- STREAM_LIMIT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STABLE_SORT [topK: 2147483647] [$$37(DESC)] |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- STREAM_SELECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- PRE_CLUSTERED_GROUP_BY[$$38] |PARTITIONED|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STABLE_SORT [$$38(ASC)] |PARTITIONED|
+ -- HASH_PARTITION_EXCHANGE [$$38] |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- HYBRID_HASH_JOIN [$$39][$#1] |PARTITIONED|
+ -- HASH_PARTITION_EXCHANGE [$$39] |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_SELECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- BTREE_SEARCH |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STABLE_SORT [$$52(ASC)] |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- SINGLE_PARTITION_INVERTED_INDEX_SEARCH |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- EMPTY_TUPLE_SOURCE |PARTITIONED|
+ -- HASH_PARTITION_EXCHANGE [$#1] |PARTITIONED|
+ -- ASSIGN |UNPARTITIONED|
+ -- UNNEST |UNPARTITIONED|
+ -- EMPTY_TUPLE_SOURCE |UNPARTITIONED|