From 4385e22da25fc11e108628db23a5367e0ef226fe Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Fri, 1 Dec 2017 00:04:57 +0900 Subject: [PATCH] Add garrow_array_unique() --- c_glib/arrow-glib/basic-array.cpp | 33 +++++++++++++++++++++++++++++++ c_glib/arrow-glib/basic-array.h | 2 ++ c_glib/test/test-unique.rb | 31 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 c_glib/test/test-unique.rb diff --git a/c_glib/arrow-glib/basic-array.cpp b/c_glib/arrow-glib/basic-array.cpp index 36cf4603a55..a7afaca9583 100644 --- a/c_glib/arrow-glib/basic-array.cpp +++ b/c_glib/arrow-glib/basic-array.cpp @@ -563,6 +563,39 @@ garrow_array_cast(GArrowArray *array, return garrow_array_new_raw(&arrow_casted_array); } +/** + * garrow_array_unique: + * @array: A #GArrowArray. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: (nullable) (transfer full): + * A newly created unique elements array on success, %NULL on error. + * + * Since: 0.8.0 + */ +GArrowArray * +garrow_array_unique(GArrowArray *array, + GError **error) +{ + auto arrow_array = garrow_array_get_raw(array); + auto memory_pool = arrow::default_memory_pool(); + arrow::compute::FunctionContext context(memory_pool); + std::shared_ptr arrow_unique_array; + auto status = arrow::compute::Unique(&context, + arrow::compute::Datum(arrow_array), + &arrow_unique_array); + if (!status.ok()) { + std::stringstream message; + message << "[array][unique] <"; + message << arrow_array->type()->ToString(); + message << ">"; + garrow_error_check(error, status, message.str().c_str()); + return NULL; + } + + return garrow_array_new_raw(&arrow_unique_array); +} + G_DEFINE_TYPE(GArrowNullArray, \ garrow_null_array, \ diff --git a/c_glib/arrow-glib/basic-array.h b/c_glib/arrow-glib/basic-array.h index c01a04f07d8..613a59b071a 100644 --- a/c_glib/arrow-glib/basic-array.h +++ b/c_glib/arrow-glib/basic-array.h @@ -66,6 +66,8 @@ GArrowArray *garrow_array_cast (GArrowArray *array, GArrowDataType *target_data_type, GArrowCastOptions *options, GError **error); +GArrowArray *garrow_array_unique (GArrowArray *array, + GError **error); #define GARROW_TYPE_NULL_ARRAY \ (garrow_null_array_get_type()) diff --git a/c_glib/test/test-unique.rb b/c_glib/test/test-unique.rb new file mode 100644 index 00000000000..b94ff462b46 --- /dev/null +++ b/c_glib/test/test-unique.rb @@ -0,0 +1,31 @@ +# 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. + +class TestUnique < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_int32 + assert_equal(build_int32_array([1, 3, -1, -3]), + build_int32_array([1, 3, 1, -1, -3, -1]).unique) + end + + def test_string + assert_equal(build_string_array(["Ruby", "Python"]), + build_string_array(["Ruby", "Python", "Ruby"]).unique) + end +end