Skip to content

Commit

Permalink
[NO ISSUE][FUN] Implement array_position function
Browse files Browse the repository at this point in the history
- user model changes: no
- storage format changes: no
- interface changes: no

details:
This is part of implementing array functions.
The array_position() takes an input list and a value
and returns the index of the value in the array or
-1 if the value is not found.
array_position(list, val). An error is thrown if
val is object or list.

Change-Id: I4604d347a22f98071a68abee43693fca9096b361
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2714
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
  • Loading branch information
AliSolaiman authored and mhubail committed Jun 24, 2018
1 parent d21c5eb commit f6b2ade
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 5 deletions.
Expand Up @@ -41,18 +41,21 @@

public class NullMissingTest {

private static final String arrayAppend = "ArrayAppendDescriptor";

@Test
public void test() throws Exception {
List<IFunctionDescriptorFactory> functions =
FunctionCollection.createDefaultFunctionCollection().getFunctionDescriptorFactories();
int testedFunctions = 0;
String[] splits;
for (IFunctionDescriptorFactory func : functions) {
String className = func.getClass().getName();
// We test all generated functions except
// record and cast functions, which requires type settings (we test them in runtime tests).
String[] splits = className.split("\\.");
splits = className.split("\\.");
if (className.contains("Gen") && !className.contains("record") && !className.contains("Cast")
&& !splits[splits.length - 1].startsWith("Array")) {
&& !splits[splits.length - 1].startsWith(arrayAppend)) {
testFunction(func);
++testedFunctions;
}
Expand Down
@@ -0,0 +1,46 @@
/*
* 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 TinySocial if exists;
create dataverse TinySocial;

use TinySocial;


create type TinySocial.TwitterUserType as
{
`screen-name` : string,
lang : string,
friends_count : bigint,
statuses_count : bigint,
name : string,
followers_count : bigint
};

create type TinySocial.TweetMessageType as
closed {
tweetid : string,
user : TwitterUserType,
`sender-location` : point?,
`send-time` : datetime,
`referred-topics` : {{string}},
`message-text` : string
};

create dataset TweetMessages(TweetMessageType) primary key tweetid hints (`CARDINALITY`=`100`);
@@ -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 TinySocial;

load dataset TweetMessages using localfs ((`path`=`asterix_nc1://data/tinysocial/twm.adm`),(`format`=`adm`));
@@ -0,0 +1,34 @@
/*
* 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 TinySocial;

{
"t1": (select array_position(t.`referred-topics`, "speed") from TweetMessages t order by t.tweetid),
"t2": (select array_position([3,8,98,40], 8)),
"t3": (select array_position([3,8,98,40], 40.0)),
"t4": (select array_position([3,8,98,40], -3)),
"t5": (select array_position([3,"sth",98,40], 98)),
"t6": (select array_position([3,8,98,40], null)),
"t7": (select array_position([3,8,98,40], missing)),
"t8": (select array_position(missing, 6)),
"t9": (select array_position(null, 6)),
"t10": (select array_position(5, "sth")),
"t11": (select array_position([5, {"id":77}, "sth"], "sth"))
};
@@ -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 TinySocial;

select array_position([5,1,9], [2,3]);
@@ -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 TinySocial;

select array_position([5,{"id": 5},9], {"id": 5});
@@ -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 TinySocial;
@@ -0,0 +1 @@
{ "t1": [ { "$1": -1 }, { "$1": -1 }, { "$1": -1 }, { "$1": -1 }, { "$1": -1 }, { "$1": 1 }, { "$1": -1 }, { "$1": 1 }, { "$1": -1 }, { "$1": -1 }, { "$1": -1 }, { "$1": -1 } ], "t2": [ { "$2": 1 } ], "t3": [ { "$3": 3 } ], "t4": [ { "$4": -1 } ], "t5": [ { "$5": 2 } ], "t6": [ { "$6": null } ], "t7": [ { } ], "t8": [ { } ], "t9": [ { "$9": null } ], "t10": [ { "$10": null } ], "t11": [ { "$11": 2 } ] }
Expand Up @@ -988,6 +988,13 @@
<output-dir compare="Text">array_append</output-dir>
</compilation-unit>
</test-case>
<test-case FilePath="array_fun">
<compilation-unit name="array_position">
<output-dir compare="Text">array_position</output-dir>
<expected-error>HYR0115: Cannot compare non-primitive values (in line 22, at column 8)</expected-error>
<expected-error>HYR0115: Cannot compare non-primitive values (in line 22, at column 8)</expected-error>
</compilation-unit>
</test-case>
</test-group>
<test-group name="boolean">
<test-case FilePath="boolean">
Expand Down
Expand Up @@ -185,6 +185,8 @@ public enum SpatialFilterKind {
// array functions
public static final FunctionIdentifier ARRAY_APPEND =
new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "array-append", FunctionIdentifier.VARARGS);
public static final FunctionIdentifier ARRAY_POSITION =
new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "array-position", 2);

// objects
public static final FunctionIdentifier RECORD_MERGE =
Expand Down Expand Up @@ -1461,6 +1463,7 @@ public static FunctionInfo lookupFunction(FunctionIdentifier fid) {

// array functions
addFunction(ARRAY_APPEND, ArrayAppendTypeComputer.INSTANCE, true);
addFunction(ARRAY_POSITION, AInt32TypeComputer.INSTANCE, true);

// objects
addFunction(RECORD_MERGE, RecordMergeTypeComputer.INSTANCE, true);
Expand Down
Expand Up @@ -135,6 +135,10 @@ public boolean isDerivedType() {
return this == ATypeTag.OBJECT || this == ATypeTag.ARRAY || this == ATypeTag.MULTISET || this == ATypeTag.UNION;
}

public final boolean isListType() {
return this == ATypeTag.ARRAY || this == ATypeTag.MULTISET;
}

@Override
public String toString() {
return this.name().toLowerCase();
Expand Down
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.asterix.runtime.evaluators.functions;

import static org.apache.asterix.om.types.EnumDeserializer.ATYPETAGDESERIALIZER;

import java.io.IOException;
import java.util.Arrays;

Expand All @@ -29,7 +31,6 @@
import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
import org.apache.asterix.om.functions.IFunctionTypeInferer;
import org.apache.asterix.om.pointables.base.DefaultOpenFieldType;
import org.apache.asterix.om.types.AOrderedListType;
import org.apache.asterix.om.types.ATypeTag;
import org.apache.asterix.om.types.AbstractCollectionType;
import org.apache.asterix.om.types.IAType;
Expand Down Expand Up @@ -144,8 +145,7 @@ public void evaluate(IFrameTupleReference tuple, IPointable result) throws Hyrac
}
}

if (listArgType != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG
&& listArgType != ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
if (!ATYPETAGDESERIALIZER.deserialize(listArgType).isListType()) {
PointableHelper.setNull(result);
return;
}
Expand Down

0 comments on commit f6b2ade

Please sign in to comment.