Dmitry Lychagin has submitted this change and it was merged.

Change subject: [NO ISSUE][FUN] Implement array functions p2
......................................................................


[NO ISSUE][FUN] Implement array functions p2

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

details:
This is part of implementing array functions. This patch
includes array_distinct(), array_sort(). It includes
reorganization of array_reverse()

Change-Id: I5e9c4ff6400b7fe93ca2aab0234750dc78f50659
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2756
Sonar-Qube: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lycha...@couchbase.com>
---
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.4.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.5.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.1.ddl.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.2.update.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.3.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.4.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.5.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.6.ddl.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.4.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.5.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.4.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.5.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.4.query.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.5.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.1.ddl.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.2.update.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.3.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.4.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.5.query.sqlpp
A 
asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.6.ddl.sqlpp
M 
asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_append/array_append.3.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_distinct/array_distinct.3.adm
A 
asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_sort/array_sort.3.adm
M asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
A 
asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/ArrayListFactory.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListFirstTypeComputer.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListTypeComputer.java
M asterixdb/asterix-runtime/pom.xml
M 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/common/ListAccessor.java
A 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/AbstractArrayProcessEval.java
A 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayDistinctDescriptor.java
M 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayReverseDescriptor.java
A 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArraySortDescriptor.java
M 
asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
35 files changed, 955 insertions(+), 60 deletions(-)

Approvals:
  Jenkins: Verified; No violations found; ; Verified
  Dmitry Lychagin: Looks good to me, approved

Objections:
  Anon. E. Moose #1000171: Violations found



diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.4.query.sqlpp
index 0cdd2e4..d5f9bb3 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.4.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.4.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [2,3]
+*/
+
 use TinySocial;
 
 select array_contains([5,1,9], [2,3]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.5.query.sqlpp
index ee9d988..afecfb6 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.5.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_contains/array_contains.5.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is {"id": 
5}
+*/
+
 use TinySocial;
 
 select array_contains([5,{"id": 5},9], {"id": 5});
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.1.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.1.ddl.sqlpp
new file mode 100755
index 0000000..257c4dd
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.1.ddl.sqlpp
@@ -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`);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.2.update.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.2.update.sqlpp
new file mode 100755
index 0000000..4a0e7ed
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.2.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 TinySocial;
+
+load  dataset TweetMessages using localfs 
((`path`=`asterix_nc1://data/tinysocial/twm.adm`),(`format`=`adm`));
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.3.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.3.query.sqlpp
new file mode 100755
index 0000000..ee5070a
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.3.query.sqlpp
@@ -0,0 +1,33 @@
+/*
+ * 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_distinct(array_append(t.`referred-topics`, "verizon", 
"platform")) from TweetMessages t order by t.tweetid),
+  "t2": (array_distinct([19, 5, 7, 7, 5, 2])),
+  "t3": (array_distinct([19, 5, 7, 7, 5.1, 5.0, 2, 7])),
+  "t4": (array_distinct([19, 5, "a", 7.5, "A", "a", "John", "a"])),
+  "t5": (array_distinct([19, missing, 7, null, 5, null])),
+  "t6": (array_distinct([3])),
+  "t7": (array_distinct("non_array")),
+  "t8": (array_distinct([])),
+  "t9": (array_distinct(missing)),
+  "t10": (array_distinct(null))
+};
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.4.query.sqlpp
new file mode 100755
index 0000000..4773079
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.4.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * 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  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [5]
+*/
+
+use TinySocial;
+
+select array_distinct([19, 3, [5]]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.5.query.sqlpp
new file mode 100755
index 0000000..0a8bdd0
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.5.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * 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  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is {"id": 
5}
+*/
+
+use TinySocial;
+
+select array_distinct([19, 3, {"id": 5}]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.6.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.6.ddl.sqlpp
new file mode 100755
index 0000000..3f8c8ec
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_distinct/array_distinct.6.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 TinySocial;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.4.query.sqlpp
index 82ecbf0..429747e 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.4.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.4.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [2,3]
+*/
+
 use TinySocial;
 
 select array_position([5,1,9], [2,3]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.5.query.sqlpp
index 3f9e4fc..4a657a5 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.5.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_position/array_position.5.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is {"id": 
5}
+*/
+
 use TinySocial;
 
 select array_position([5,{"id": 5},9], {"id": 5});
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.4.query.sqlpp
index a462600..8eee258 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.4.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.4.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is coming 
from a sub-query
+*/
+
 use TinySocial;
 
 select array_put([3, "John"], (select value v.compType from d1 v));
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.5.query.sqlpp
index 0d27117..61c7d47 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.5.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_put/array_put.5.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [9]
+*/
+
 use TinySocial;
 
 select array_put([3], 3, [9], 4, "sth");
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.4.query.sqlpp
index 2dfa514..c684b1b 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.4.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.4.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is coming 
from a sub-query
+*/
+
 use TinySocial;
 
 select array_remove([3, "John"], (select value v.compType from d1 v));
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.5.query.sqlpp
index bfa878f..e79c163 100755
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.5.query.sqlpp
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_remove/array_remove.5.query.sqlpp
@@ -17,6 +17,11 @@
  * under the License.
  */
 
+/*
+* Description  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [9]
+*/
+
 use TinySocial;
 
 select array_remove([3], 3, [9], 4, "sth");
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.1.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.1.ddl.sqlpp
new file mode 100755
index 0000000..257c4dd
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.1.ddl.sqlpp
@@ -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`);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.2.update.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.2.update.sqlpp
new file mode 100755
index 0000000..4a0e7ed
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.2.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 TinySocial;
+
+load  dataset TweetMessages using localfs 
((`path`=`asterix_nc1://data/tinysocial/twm.adm`),(`format`=`adm`));
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.3.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.3.query.sqlpp
new file mode 100755
index 0000000..3047c11
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.3.query.sqlpp
@@ -0,0 +1,33 @@
+/*
+ * 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_sort(t.`referred-topics`) from TweetMessages t order by 
t.tweetid),
+  "t2": (array_sort([19, 5, 7, 7, 5, 2])),
+  "t3": (array_sort([19, 5, 7, 7, 5.1, 5.0, 2])),
+  "t4": (array_sort([19, 5, "a", 7.5, "A", "John", "a"])),
+  "t5": (array_sort([19, missing, 7, null, 5, null])),
+  "t6": (array_sort([3])),
+  "t7": (array_sort("non_array")),
+  "t8": (array_sort([])),
+  "t9": (array_sort(missing)),
+  "t10": (array_sort(null))
+};
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.4.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.4.query.sqlpp
new file mode 100755
index 0000000..65b0521
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.4.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * 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  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is [5]
+*/
+
+use TinySocial;
+
+select array_sort([19, 3, [5]]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.5.query.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.5.query.sqlpp
new file mode 100755
index 0000000..cc1745e
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.5.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * 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  : Testing an array function that needs to compare elements
+* Expected Res : Error due to comparing non-primitive values, which is {"id": 
5}
+*/
+
+use TinySocial;
+
+select array_sort([19, 3, {"id": 5}]);
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.6.ddl.sqlpp
 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.6.ddl.sqlpp
new file mode 100755
index 0000000..3f8c8ec
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array_fun/array_sort/array_sort.6.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 TinySocial;
\ No newline at end of file
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_append/array_append.3.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_append/array_append.3.adm
index 52c4546..33f9a09 100644
--- 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_append/array_append.3.adm
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_append/array_append.3.adm
@@ -1 +1 @@
-{ "t1": [ { "$1": {{ "t-mobile", "customization", "sth", 5 }} }, { "$1": {{ 
"verizon", "voice-clarity", "sth", 5 }} }, { "$1": {{ "iphone", "platform", 
"sth", 5 }} }, { "$1": {{ "samsung", "voice-command", "sth", 5 }} }, { "$1": {{ 
"verizon", "shortcut-menu", "sth", 5 }} }, { "$1": {{ "motorola", "speed", 
"sth", 5 }} }, { "$1": {{ "sprint", "voice-command", "sth", 5 }} }, { "$1": {{ 
"motorola", "speed", "sth", 5 }} }, { "$1": {{ "iphone", "voice-clarity", 
"sth", 5 }} }, { "$1": {{ "samsung", "platform", "sth", 5 }} }, { "$1": {{ 
"t-mobile", "shortcut-menu", "sth", 5 }} }, { "$1": {{ "verizon", 
"voicemail-service", "sth", 5 }} } ], "t2": [ { "$2": [ 3, "John", [ { "sth": 
33 }, { "sth": 44 } ] ] } ], "t4": null, "t5": null, "t9": [ 3, 3, [ 9 ], null, 
"sth" ], "t7": [ 5, 10, 12.0, "sth" ] }
+{ "t1": [ { "$1": {{ "t-mobile", "customization", "sth", 5 }} }, { "$1": {{ 
"verizon", "voice-clarity", "sth", 5 }} }, { "$1": {{ "iphone", "platform", 
"sth", 5 }} }, { "$1": {{ "samsung", "voice-command", "sth", 5 }} }, { "$1": {{ 
"verizon", "shortcut-menu", "sth", 5 }} }, { "$1": {{ "motorola", "speed", 
"sth", 5 }} }, { "$1": {{ "sprint", "voice-command", "sth", 5 }} }, { "$1": {{ 
"motorola", "speed", "sth", 5 }} }, { "$1": {{ "iphone", "voice-clarity", 
"sth", 5 }} }, { "$1": {{ "samsung", "platform", "sth", 5 }} }, { "$1": {{ 
"t-mobile", "shortcut-menu", "sth", 5 }} }, { "$1": {{ "verizon", 
"voicemail-service", "sth", 5 }} } ], "t2": [ { "$2": [ 3, "John", [ { "sth": 
33 }, { "sth": 44 } ] ] } ], "t4": null, "t5": null, "t7": [ 5, 10, 12.0, "sth" 
], "t9": [ 3, 3, [ 9 ], null, "sth" ] }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_distinct/array_distinct.3.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_distinct/array_distinct.3.adm
new file mode 100644
index 0000000..0482ae3
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_distinct/array_distinct.3.adm
@@ -0,0 +1 @@
+{ "t1": [ { "$1": {{ "t-mobile", "customization", "verizon", "platform" }} }, 
{ "$1": {{ "verizon", "voice-clarity", "platform" }} }, { "$1": {{ "iphone", 
"platform", "verizon" }} }, { "$1": {{ "samsung", "voice-command", "verizon", 
"platform" }} }, { "$1": {{ "verizon", "shortcut-menu", "platform" }} }, { 
"$1": {{ "motorola", "speed", "verizon", "platform" }} }, { "$1": {{ "sprint", 
"voice-command", "verizon", "platform" }} }, { "$1": {{ "motorola", "speed", 
"verizon", "platform" }} }, { "$1": {{ "iphone", "voice-clarity", "verizon", 
"platform" }} }, { "$1": {{ "samsung", "platform", "verizon" }} }, { "$1": {{ 
"t-mobile", "shortcut-menu", "verizon", "platform" }} }, { "$1": {{ "verizon", 
"voicemail-service", "platform" }} } ], "t2": [ 19, 5, 7, 2 ], "t3": [ 19, 5, 
7, 5.1, 2 ], "t4": [ 19, 5, "a", 7.5, "A", "John" ], "t5": [ 19, null, 7, 5 ], 
"t6": [ 3 ], "t7": null, "t8": [  ], "t10": null }
diff --git 
a/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_sort/array_sort.3.adm
 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_sort/array_sort.3.adm
new file mode 100644
index 0000000..cc5be40
--- /dev/null
+++ 
b/asterixdb/asterix-app/src/test/resources/runtimets/results/array_fun/array_sort/array_sort.3.adm
@@ -0,0 +1 @@
+{ "t1": [ { "$1": {{ "customization", "t-mobile" }} }, { "$1": {{ "verizon", 
"voice-clarity" }} }, { "$1": {{ "iphone", "platform" }} }, { "$1": {{ 
"samsung", "voice-command" }} }, { "$1": {{ "shortcut-menu", "verizon" }} }, { 
"$1": {{ "motorola", "speed" }} }, { "$1": {{ "sprint", "voice-command" }} }, { 
"$1": {{ "motorola", "speed" }} }, { "$1": {{ "iphone", "voice-clarity" }} }, { 
"$1": {{ "platform", "samsung" }} }, { "$1": {{ "shortcut-menu", "t-mobile" }} 
}, { "$1": {{ "verizon", "voicemail-service" }} } ], "t2": [ 2, 5, 5, 7, 7, 19 
], "t3": [ 2, 5.0, 5, 5.1, 7, 7, 19 ], "t4": [ 5, 7.5, 19, "A", "John", "a", 
"a" ], "t5": [ null, null, null, 5, 7, 19 ], "t6": [ 3 ], "t7": null, "t8": [  
], "t10": null }
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 3616268..a28d247 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1001,8 +1001,8 @@
     <test-case FilePath="array_fun">
       <compilation-unit name="array_position">
         <output-dir compare="Text">array_position</output-dir>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="array_fun">
@@ -1018,8 +1018,8 @@
     <test-case FilePath="array_fun">
       <compilation-unit name="array_contains">
         <output-dir compare="Text">array_contains</output-dir>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="array_fun">
@@ -1030,15 +1030,29 @@
     <test-case FilePath="array_fun">
       <compilation-unit name="array_put">
         <output-dir compare="Text">array_put</output-dir>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="array_fun">
       <compilation-unit name="array_remove">
         <output-dir compare="Text">array_remove</output-dir>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
-        <expected-error>Cannot compare non-primitive values (in line 22, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="array_fun">
+      <compilation-unit name="array_distinct">
+        <output-dir compare="Text">array_distinct</output-dir>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="array_fun">
+      <compilation-unit name="array_sort">
+        <output-dir compare="Text">array_sort</output-dir>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
+        <expected-error>Cannot compare non-primitive values (in line 27, at 
column 8)</expected-error>
       </compilation-unit>
     </test-case>
   </test-group>
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/ArrayListFactory.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/ArrayListFactory.java
new file mode 100644
index 0000000..c8ca766
--- /dev/null
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/builders/ArrayListFactory.java
@@ -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.
+ */
+
+package org.apache.asterix.builders;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.util.container.IObjectFactory;
+
+public class ArrayListFactory<T> implements IObjectFactory<List<T>, ATypeTag> {
+
+    @Override
+    public List<T> create(ATypeTag arg) {
+        return new ArrayList<>();
+    }
+}
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
index 8a968df..a373c3b 100644
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/functions/BuiltinFunctions.java
@@ -192,6 +192,8 @@
             new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-prepend", FunctionIdentifier.VARARGS);
     public static final FunctionIdentifier ARRAY_APPEND =
             new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-append", FunctionIdentifier.VARARGS);
+    public static final FunctionIdentifier ARRAY_INSERT =
+            new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-insert", FunctionIdentifier.VARARGS);
     public static final FunctionIdentifier ARRAY_POSITION =
             new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-position", 2);
     public static final FunctionIdentifier ARRAY_REPEAT =
@@ -200,8 +202,10 @@
             new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-reverse", 1);
     public static final FunctionIdentifier ARRAY_CONTAINS =
             new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-contains", 2);
-    public static final FunctionIdentifier ARRAY_INSERT =
-            new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-insert", FunctionIdentifier.VARARGS);
+    public static final FunctionIdentifier ARRAY_DISTINCT =
+            new FunctionIdentifier(FunctionConstants.ASTERIX_NS, 
"array-distinct", 1);
+    public static final FunctionIdentifier ARRAY_SORT =
+            new FunctionIdentifier(FunctionConstants.ASTERIX_NS, "array-sort", 
1);
 
     // objects
     public static final FunctionIdentifier RECORD_MERGE =
@@ -1487,6 +1491,8 @@
         addFunction(ARRAY_REPEAT, ArrayRepeatTypeComputer.INSTANCE, true);
         addFunction(ARRAY_REVERSE, AListFirstTypeComputer.INSTANCE, true);
         addFunction(ARRAY_CONTAINS, ABooleanTypeComputer.INSTANCE, true);
+        addFunction(ARRAY_SORT, AListFirstTypeComputer.INSTANCE, true);
+        addFunction(ARRAY_DISTINCT, AListFirstTypeComputer.INSTANCE, true);
 
         // objects
         addFunction(RECORD_MERGE, RecordMergeTypeComputer.INSTANCE, true);
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListFirstTypeComputer.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListFirstTypeComputer.java
index 5ba7ba4..dfeaba6 100644
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListFirstTypeComputer.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListFirstTypeComputer.java
@@ -20,7 +20,6 @@
 package org.apache.asterix.om.typecomputer.impl;
 
 import org.apache.asterix.om.typecomputer.base.AbstractResultTypeComputer;
-import org.apache.asterix.om.types.AUnionType;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -42,7 +41,7 @@
         switch (argType.getTypeTag()) {
             case ARRAY:
             case MULTISET:
-                return AUnionType.createNullableType(argType);
+                return argType;
             default:
                 return BuiltinType.ANY;
         }
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListTypeComputer.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListTypeComputer.java
index 5b2513c..c0be131 100755
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListTypeComputer.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/AListTypeComputer.java
@@ -37,8 +37,8 @@
 public class AListTypeComputer extends AbstractResultTypeComputer {
     public static final AListTypeComputer INSTANCE_REMOVE = new 
AListTypeComputer(2, false, false, true);
     public static final AListTypeComputer INSTANCE_PUT = new 
AListTypeComputer(2, false, true, true);
-    public static final AListTypeComputer INSTANCE_PREPEND = new 
AListTypeComputer(2, false, true, false);
-    public static final AListTypeComputer INSTANCE_APPEND = new 
AListTypeComputer(2, true, true, false);
+    public static final AListTypeComputer INSTANCE_PREPEND = new 
AListTypeComputer(2, true, true, false);
+    public static final AListTypeComputer INSTANCE_APPEND = new 
AListTypeComputer(2, false, true, false);
     public static final AListTypeComputer INSTANCE_INSERT = new 
AListTypeComputer(3, false, true, false);
 
     private final int minNumArgs;
diff --git a/asterixdb/asterix-runtime/pom.xml 
b/asterixdb/asterix-runtime/pom.xml
index 4cb4aad..2b28f50 100644
--- a/asterixdb/asterix-runtime/pom.xml
+++ b/asterixdb/asterix-runtime/pom.xml
@@ -192,5 +192,10 @@
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>it.unimi.dsi</groupId>
+      <artifactId>fastutil</artifactId>
+      <version>8.1.1</version>
+    </dependency>
   </dependencies>
 </project>
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/common/ListAccessor.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/common/ListAccessor.java
index 3c97fc9..e834522 100644
--- 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/common/ListAccessor.java
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/common/ListAccessor.java
@@ -29,6 +29,8 @@
 import org.apache.asterix.om.utils.NonTaggedFormatUtil;
 import org.apache.asterix.runtime.exceptions.TypeMismatchException;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
 
 /**
  * Utility class for accessing serialized unordered and ordered lists.
@@ -107,6 +109,30 @@
         dos.write(listBytes, itemOffset, itemLength);
     }
 
+    /**
+     * @param itemIndex the index of the item requested
+     * @param pointable a pointable that will be set to point to the item 
requested
+     * @param storage if list is strongly typed, the item tag will be written 
followed by the item value to this storage
+     * @return true when the item requested has been written to the storage. 
false when a pointer to the item was set
+     * @throws IOException
+     */
+    public boolean getOrWriteItem(int itemIndex, IPointable pointable, 
ArrayBackedValueStorage storage)
+            throws IOException {
+        int itemOffset = getItemOffset(itemIndex);
+        int itemLength = getItemLength(itemOffset);
+        if (itemsAreSelfDescribing()) {
+            // +1 to account for the already included tag
+            pointable.set(listBytes, itemOffset, itemLength + 1);
+            return false;
+        } else {
+            storage.reset();
+            storage.getDataOutput().writeByte(itemType.serialize());
+            storage.getDataOutput().write(listBytes, itemOffset, itemLength);
+            pointable.set(storage);
+            return true;
+        }
+    }
+
     public byte[] getByteArray() {
         return listBytes;
     }
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/AbstractArrayProcessEval.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/AbstractArrayProcessEval.java
new file mode 100755
index 0000000..9c4f671
--- /dev/null
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/AbstractArrayProcessEval.java
@@ -0,0 +1,128 @@
+/*
+ * 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.
+ */
+package org.apache.asterix.runtime.evaluators.functions;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.asterix.builders.AbvsBuilderFactory;
+import org.apache.asterix.builders.ArrayListFactory;
+import org.apache.asterix.builders.IAsterixListBuilder;
+import org.apache.asterix.builders.OrderedListBuilder;
+import org.apache.asterix.builders.UnorderedListBuilder;
+import org.apache.asterix.om.pointables.PointableAllocator;
+import org.apache.asterix.om.types.AOrderedListType;
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.AUnorderedListType;
+import org.apache.asterix.om.types.AbstractCollectionType;
+import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.types.TypeTagUtil;
+import org.apache.asterix.om.util.container.IObjectPool;
+import org.apache.asterix.om.util.container.ListObjectPool;
+import org.apache.asterix.runtime.evaluators.common.ListAccessor;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.data.std.api.IMutableValueStorage;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.primitive.VoidPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
+
+public abstract class AbstractArrayProcessEval implements IScalarEvaluator {
+    private final ArrayBackedValueStorage storage;
+    private final IScalarEvaluator listArgEval;
+    private final ListAccessor listAccessor;
+    private final IPointable listArg;
+    private IAType inputListType;
+    private IAsterixListBuilder orderedListBuilder;
+    private IAsterixListBuilder unorderedListBuilder;
+
+    protected final PointableAllocator pointableAllocator;
+    protected final IObjectPool<IMutableValueStorage, ATypeTag> 
storageAllocator;
+    protected final IObjectPool<List<IPointable>, ATypeTag> arrayListAllocator;
+
+    public AbstractArrayProcessEval(IScalarEvaluatorFactory[] args, 
IHyracksTaskContext ctx, IAType inputListType)
+            throws HyracksDataException {
+        orderedListBuilder = null;
+        unorderedListBuilder = null;
+        storage = new ArrayBackedValueStorage();
+        listArg = new VoidPointable();
+        pointableAllocator = new PointableAllocator();
+        storageAllocator = new ListObjectPool<>(new AbvsBuilderFactory());
+        arrayListAllocator = new ListObjectPool<>(new ArrayListFactory<>());
+        listArgEval = args[0].createScalarEvaluator(ctx);
+        listAccessor = new ListAccessor();
+        this.inputListType = inputListType;
+    }
+
+    @Override
+    public void evaluate(IFrameTupleReference tuple, IPointable result) throws 
HyracksDataException {
+        // get the list argument and make sure it's a list
+        listArgEval.evaluate(tuple, listArg);
+        byte listArgType = listArg.getByteArray()[listArg.getStartOffset()];
+
+        // create the new list with the same type as the input list
+        IAsterixListBuilder listBuilder;
+        if (listArgType == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
+            if (orderedListBuilder == null) {
+                orderedListBuilder = new OrderedListBuilder();
+            }
+            listBuilder = orderedListBuilder;
+        } else if (listArgType == ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
+            if (unorderedListBuilder == null) {
+                unorderedListBuilder = new UnorderedListBuilder();
+            }
+            listBuilder = unorderedListBuilder;
+        } else {
+            PointableHelper.setNull(result);
+            return;
+        }
+
+        listAccessor.reset(listArg.getByteArray(), listArg.getStartOffset());
+        AbstractCollectionType outputListType;
+        if (!inputListType.getTypeTag().isListType()) {
+            ATypeTag itemType = listAccessor.getItemType();
+            if (listAccessor.getListType() == ATypeTag.ARRAY) {
+                outputListType = new 
AOrderedListType(TypeTagUtil.getBuiltinTypeByTag(itemType), null);
+            } else {
+                outputListType = new 
AUnorderedListType(TypeTagUtil.getBuiltinTypeByTag(itemType), null);
+            }
+        } else {
+            outputListType = (AbstractCollectionType) inputListType;
+        }
+
+        listBuilder.reset(outputListType);
+        try {
+            processList(listAccessor, listBuilder);
+            storage.reset();
+            listBuilder.write(storage.getDataOutput(), true);
+            result.set(storage);
+        } catch (IOException e) {
+            throw HyracksDataException.create(e);
+        } finally {
+            pointableAllocator.reset();
+            storageAllocator.reset();
+            arrayListAllocator.reset();
+        }
+    }
+
+    protected abstract void processList(ListAccessor listAccessor, 
IAsterixListBuilder listBuilder) throws IOException;
+}
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayDistinctDescriptor.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayDistinctDescriptor.java
new file mode 100755
index 0000000..52db331
--- /dev/null
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayDistinctDescriptor.java
@@ -0,0 +1,181 @@
+/*
+ * 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.
+ */
+package org.apache.asterix.runtime.evaluators.functions;
+
+import static 
org.apache.asterix.om.types.EnumDeserializer.ATYPETAGDESERIALIZER;
+
+import java.io.IOException;
+import java.util.List;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.asterix.builders.IAsterixListBuilder;
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import 
org.apache.asterix.dataflow.data.nontagged.comparators.AObjectAscBinaryComparatorFactory;
+import org.apache.asterix.formats.nontagged.BinaryHashFunctionFactoryProvider;
+import org.apache.asterix.om.functions.BuiltinFunctions;
+import org.apache.asterix.om.functions.IFunctionDescriptor;
+import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
+import org.apache.asterix.om.functions.IFunctionTypeInferer;
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.IAType;
+import 
org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
+import org.apache.asterix.runtime.evaluators.common.ListAccessor;
+import org.apache.asterix.runtime.functions.FunctionTypeInferers;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
+import org.apache.hyracks.api.dataflow.value.IBinaryComparator;
+import org.apache.hyracks.api.dataflow.value.IBinaryHashFunction;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.exceptions.SourceLocation;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+
+public class ArrayDistinctDescriptor extends 
AbstractScalarFunctionDynamicDescriptor {
+    private static final long serialVersionUID = 1L;
+    private IAType inputListType;
+
+    public static final IFunctionDescriptorFactory FACTORY = new 
IFunctionDescriptorFactory() {
+        @Override
+        public IFunctionDescriptor createFunctionDescriptor() {
+            return new ArrayDistinctDescriptor();
+        }
+
+        @Override
+        public IFunctionTypeInferer createFunctionTypeInferer() {
+            // the type of the input list is needed in order to use the same 
type for the new returned list
+            return FunctionTypeInferers.SET_ARGUMENT_TYPE;
+        }
+    };
+
+    @Override
+    public FunctionIdentifier getIdentifier() {
+        return BuiltinFunctions.ARRAY_DISTINCT;
+    }
+
+    @Override
+    public void setImmutableStates(Object... states) {
+        inputListType = (IAType) states[0];
+    }
+
+    @Override
+    public IScalarEvaluatorFactory createEvaluatorFactory(final 
IScalarEvaluatorFactory[] args)
+            throws AlgebricksException {
+        return new IScalarEvaluatorFactory() {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public IScalarEvaluator createScalarEvaluator(final 
IHyracksTaskContext ctx) throws HyracksDataException {
+                return new ArrayDistinctFunction(args, ctx, sourceLoc);
+            }
+        };
+    }
+
+    public class ArrayDistinctFunction extends AbstractArrayProcessEval {
+        private final IBinaryComparator comp;
+        private final SourceLocation sourceLoc;
+        private final IBinaryHashFunction binaryHashFunction;
+        private final Int2ObjectMap<List<IPointable>> hashes;
+        private IPointable item;
+        private ArrayBackedValueStorage storage;
+
+        public ArrayDistinctFunction(IScalarEvaluatorFactory[] args, 
IHyracksTaskContext ctx, SourceLocation sourceLoc)
+                throws HyracksDataException {
+            super(args, ctx, inputListType);
+            this.sourceLoc = sourceLoc;
+            hashes = new Int2ObjectOpenHashMap<>();
+            item = pointableAllocator.allocateEmpty();
+            storage = (ArrayBackedValueStorage) 
storageAllocator.allocate(null);
+            comp = 
AObjectAscBinaryComparatorFactory.INSTANCE.createBinaryComparator();
+            binaryHashFunction = 
BinaryHashFunctionFactoryProvider.INSTANCE.getBinaryHashFunctionFactory(null)
+                    .createBinaryHashFunction();
+        }
+
+        @Override
+        protected void processList(ListAccessor listAccessor, 
IAsterixListBuilder listBuilder) throws IOException {
+            int hash;
+            boolean itemInStorage;
+            boolean nullMissingWasAdded = false;
+            List<IPointable> sameHashes;
+            hashes.clear();
+            for (int i = 0; i < listAccessor.size(); i++) {
+                // get the item and compute its hash
+                itemInStorage = listAccessor.getOrWriteItem(i, item, storage);
+                if 
(ATYPETAGDESERIALIZER.deserialize(item.getByteArray()[item.getStartOffset()]).isDerivedType())
 {
+                    throw new 
RuntimeDataException(ErrorCode.CANNOT_COMPARE_COMPLEX, sourceLoc);
+                }
+                if (isNullOrMissing(item)) {
+                    if (!nullMissingWasAdded) {
+                        listBuilder.addItem(item);
+                        nullMissingWasAdded = true;
+                    }
+                } else {
+                    // look up if it already exists
+                    hash = binaryHashFunction.hash(item.getByteArray(), 
item.getStartOffset(), item.getLength());
+                    hashes.get(hash);
+                    sameHashes = hashes.get(hash);
+                    if (sameHashes == null) {
+                        // new item
+                        sameHashes = arrayListAllocator.allocate(null);
+                        sameHashes.clear();
+                        addItem(item, listBuilder, itemInStorage, sameHashes);
+                        hashes.put(hash, sameHashes);
+                        item = pointableAllocator.allocateEmpty();
+                    } else {
+                        // check if it happens that two hashes are the same 
but they are for different items
+                        if (isNewItem(item, sameHashes)) {
+                            addItem(item, listBuilder, itemInStorage, 
sameHashes);
+                            item = pointableAllocator.allocateEmpty();
+                        }
+                    }
+                }
+            }
+        }
+
+        private boolean isNullOrMissing(IPointable item) {
+            byte tag = item.getByteArray()[item.getStartOffset()];
+            return tag == ATypeTag.SERIALIZED_NULL_TYPE_TAG || tag == 
ATypeTag.SERIALIZED_MISSING_TYPE_TAG;
+        }
+
+        private void addItem(IPointable item, IAsterixListBuilder listBuilder, 
boolean itemInStorage,
+                List<IPointable> sameHashes) throws HyracksDataException {
+            sameHashes.add(item);
+            listBuilder.addItem(item);
+            if (itemInStorage) {
+                // create new storage since the added item is using it now
+                storage = (ArrayBackedValueStorage) 
storageAllocator.allocate(null);
+            }
+        }
+
+        private boolean isNewItem(IPointable item, List<IPointable> 
sameHashes) throws HyracksDataException {
+            for (int j = 0; j < sameHashes.size(); j++) {
+                if (comp.compare(item.getByteArray(), item.getStartOffset(), 
item.getLength(),
+                        sameHashes.get(j).getByteArray(), 
sameHashes.get(j).getStartOffset(),
+                        sameHashes.get(j).getLength()) == 0) {
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
+}
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayReverseDescriptor.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayReverseDescriptor.java
index eed50ca..fa8bec3f 100755
--- 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayReverseDescriptor.java
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArrayReverseDescriptor.java
@@ -21,14 +21,10 @@
 import java.io.IOException;
 
 import org.apache.asterix.builders.IAsterixListBuilder;
-import org.apache.asterix.builders.OrderedListBuilder;
-import org.apache.asterix.builders.UnorderedListBuilder;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.functions.IFunctionDescriptor;
 import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
 import org.apache.asterix.om.functions.IFunctionTypeInferer;
-import org.apache.asterix.om.types.ATypeTag;
-import org.apache.asterix.om.types.AbstractCollectionType;
 import org.apache.asterix.om.types.IAType;
 import 
org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
 import org.apache.asterix.runtime.evaluators.common.ListAccessor;
@@ -39,10 +35,9 @@
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.context.IHyracksTaskContext;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
-import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.api.AbstractPointable;
 import org.apache.hyracks.data.std.primitive.VoidPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
-import org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference;
 
 /**
  * array_reverse(list) returns a new list with the entries of the original 
input list in reverse order. If the input is
@@ -63,7 +58,6 @@
             // the type of the input list is needed in order to use the same 
type for the new returned list
             return FunctionTypeInferers.SET_ARGUMENT_TYPE;
         }
-
     };
 
     @Override
@@ -89,51 +83,23 @@
         };
     }
 
-    public class ArrayReverseFunction implements IScalarEvaluator {
+    public class ArrayReverseFunction extends AbstractArrayProcessEval {
         private final ArrayBackedValueStorage storage;
-        private final IScalarEvaluator listArgEval;
-        private final ListAccessor listAccessor;
-        private final IPointable listArg;
+        private final AbstractPointable item;
 
         public ArrayReverseFunction(IScalarEvaluatorFactory[] args, 
IHyracksTaskContext ctx)
                 throws HyracksDataException {
+            super(args, ctx, inputListType);
             storage = new ArrayBackedValueStorage();
-            listArg = new VoidPointable();
-            listArgEval = args[0].createScalarEvaluator(ctx);
-            listAccessor = new ListAccessor();
+            item = new VoidPointable();
         }
 
         @Override
-        public void evaluate(IFrameTupleReference tuple, IPointable result) 
throws HyracksDataException {
-            // get the list argument and make sure it's a list
-            listArgEval.evaluate(tuple, listArg);
-            byte listArgType = 
listArg.getByteArray()[listArg.getStartOffset()];
-
-            // create the new list with the same type as the input list
-            IAsterixListBuilder listBuilder;
-            if (listArgType == ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
-                listBuilder = new OrderedListBuilder();
-            } else if (listArgType == 
ATypeTag.SERIALIZED_UNORDEREDLIST_TYPE_TAG) {
-                listBuilder = new UnorderedListBuilder();
-            } else {
-                PointableHelper.setNull(result);
-                return;
-            }
-
-            listBuilder.reset((AbstractCollectionType) inputListType);
-            listAccessor.reset(listArg.getByteArray(), 
listArg.getStartOffset());
-            try {
-                // get the list items in reverse and append to the new list
-                for (int i = listAccessor.size() - 1; i >= 0; i--) {
-                    storage.reset();
-                    listAccessor.writeItem(i, storage.getDataOutput());
-                    listBuilder.addItem(storage);
-                }
-                storage.reset();
-                listBuilder.write(storage.getDataOutput(), true);
-                result.set(storage);
-            } catch (IOException e) {
-                throw HyracksDataException.create(e);
+        protected void processList(ListAccessor listAccessor, 
IAsterixListBuilder listBuilder) throws IOException {
+            // get the list items in reverse and append to the new list
+            for (int i = listAccessor.size() - 1; i >= 0; i--) {
+                listAccessor.getOrWriteItem(i, item, storage);
+                listBuilder.addItem(item);
             }
         }
     }
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArraySortDescriptor.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArraySortDescriptor.java
new file mode 100755
index 0000000..f99fc4c
--- /dev/null
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/ArraySortDescriptor.java
@@ -0,0 +1,140 @@
+/*
+ * 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.
+ */
+package org.apache.asterix.runtime.evaluators.functions;
+
+import static 
org.apache.asterix.om.types.EnumDeserializer.ATYPETAGDESERIALIZER;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.PriorityQueue;
+
+import org.apache.asterix.builders.IAsterixListBuilder;
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
+import 
org.apache.asterix.dataflow.data.nontagged.comparators.AObjectAscBinaryComparatorFactory;
+import org.apache.asterix.om.functions.BuiltinFunctions;
+import org.apache.asterix.om.functions.IFunctionDescriptor;
+import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
+import org.apache.asterix.om.functions.IFunctionTypeInferer;
+import org.apache.asterix.om.types.IAType;
+import 
org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
+import org.apache.asterix.runtime.evaluators.common.ListAccessor;
+import org.apache.asterix.runtime.functions.FunctionTypeInferers;
+import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
+import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
+import org.apache.hyracks.api.dataflow.value.IBinaryComparator;
+import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.exceptions.SourceLocation;
+import org.apache.hyracks.data.std.api.IPointable;
+import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
+
+public class ArraySortDescriptor extends 
AbstractScalarFunctionDynamicDescriptor {
+    private static final long serialVersionUID = 1L;
+    private static final ArraySortComparator COMP = new ArraySortComparator();
+    private IAType inputListType;
+
+    public static final IFunctionDescriptorFactory FACTORY = new 
IFunctionDescriptorFactory() {
+        @Override
+        public IFunctionDescriptor createFunctionDescriptor() {
+            return new ArraySortDescriptor();
+        }
+
+        @Override
+        public IFunctionTypeInferer createFunctionTypeInferer() {
+            // the type of the input list is needed in order to use the same 
type for the new returned list
+            return FunctionTypeInferers.SET_ARGUMENT_TYPE;
+        }
+    };
+
+    @Override
+    public FunctionIdentifier getIdentifier() {
+        return BuiltinFunctions.ARRAY_SORT;
+    }
+
+    @Override
+    public void setImmutableStates(Object... states) {
+        inputListType = (IAType) states[0];
+    }
+
+    @Override
+    public IScalarEvaluatorFactory createEvaluatorFactory(final 
IScalarEvaluatorFactory[] args)
+            throws AlgebricksException {
+        return new IScalarEvaluatorFactory() {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public IScalarEvaluator createScalarEvaluator(final 
IHyracksTaskContext ctx) throws HyracksDataException {
+                return new ArraySortFunction(args, ctx, sourceLoc);
+            }
+        };
+    }
+
+    private static class ArraySortComparator implements Comparator<IPointable> 
{
+        private final IBinaryComparator comp = 
AObjectAscBinaryComparatorFactory.INSTANCE.createBinaryComparator();
+
+        @Override
+        public int compare(IPointable val1, IPointable val2) {
+            try {
+                return comp.compare(val1.getByteArray(), 
val1.getStartOffset(), val1.getLength(), val2.getByteArray(),
+                        val2.getStartOffset(), val2.getLength());
+            } catch (HyracksDataException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
+
+    public class ArraySortFunction extends AbstractArrayProcessEval {
+        private final SourceLocation sourceLoc;
+        private final PriorityQueue<IPointable> sortedList;
+        private IPointable item;
+        private ArrayBackedValueStorage storage;
+
+        public ArraySortFunction(IScalarEvaluatorFactory[] args, 
IHyracksTaskContext ctx, SourceLocation sourceLoc)
+                throws HyracksDataException {
+            super(args, ctx, inputListType);
+            this.sourceLoc = sourceLoc;
+            item = pointableAllocator.allocateEmpty();
+            storage = (ArrayBackedValueStorage) 
storageAllocator.allocate(null);
+            sortedList = new PriorityQueue<>(COMP);
+        }
+
+        @Override
+        protected void processList(ListAccessor listAccessor, 
IAsterixListBuilder listBuilder) throws IOException {
+            sortedList.clear();
+            boolean itemInStorage;
+            for (int i = 0; i < listAccessor.size(); i++) {
+                itemInStorage = listAccessor.getOrWriteItem(i, item, storage);
+                if 
(ATYPETAGDESERIALIZER.deserialize(item.getByteArray()[item.getStartOffset()]).isDerivedType())
 {
+                    throw new 
RuntimeDataException(ErrorCode.CANNOT_COMPARE_COMPLEX, sourceLoc);
+                }
+                sortedList.add(item);
+                if (itemInStorage) {
+                    storage = (ArrayBackedValueStorage) 
storageAllocator.allocate(null);
+                }
+                item = pointableAllocator.allocateEmpty();
+            }
+            while (!sortedList.isEmpty()) {
+                listBuilder.addItem(sortedList.poll());
+            }
+        }
+    }
+}
diff --git 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
index 519cb86..099d563 100644
--- 
a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
+++ 
b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/functions/FunctionCollection.java
@@ -145,6 +145,7 @@
 import 
org.apache.asterix.runtime.evaluators.functions.AnyCollectionMemberDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayAppendDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayContainsDescriptor;
+import org.apache.asterix.runtime.evaluators.functions.ArrayDistinctDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayInsertDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayPositionDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayPrependDescriptor;
@@ -152,6 +153,7 @@
 import org.apache.asterix.runtime.evaluators.functions.ArrayRemoveDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayRepeatDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.ArrayReverseDescriptor;
+import org.apache.asterix.runtime.evaluators.functions.ArraySortDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.CastTypeDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.CastTypeLaxDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.CheckUnknownDescriptor;
@@ -387,8 +389,10 @@
         fc.add(ArrayInsertDescriptor.FACTORY);
         fc.addGenerated(ArrayPositionDescriptor.FACTORY);
         fc.addGenerated(ArrayRepeatDescriptor.FACTORY);
-        fc.addGenerated(ArrayReverseDescriptor.FACTORY);
         fc.addGenerated(ArrayContainsDescriptor.FACTORY);
+        fc.addGenerated(ArrayReverseDescriptor.FACTORY);
+        fc.addGenerated(ArraySortDescriptor.FACTORY);
+        fc.addGenerated(ArrayDistinctDescriptor.FACTORY);
 
         // unnesting functions
         fc.add(TidRunningAggregateDescriptor.FACTORY);

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/2756
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I5e9c4ff6400b7fe93ca2aab0234750dc78f50659
Gerrit-PatchSet: 16
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Ali Alsuliman <ali.al.solai...@gmail.com>
Gerrit-Reviewer: Ali Alsuliman <ali.al.solai...@gmail.com>
Gerrit-Reviewer: Anon. E. Moose #1000171
Gerrit-Reviewer: Dmitry Lychagin <dmitry.lycha...@couchbase.com>
Gerrit-Reviewer: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Gerrit-Reviewer: Till Westmann <ti...@apache.org>
Gerrit-Reviewer: abdullah alamoudi <bamou...@gmail.com>

Reply via email to