This is an automated email from the ASF dual-hosted git repository. kou pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push: new dcee4ad ARROW-4859: [GLib] Add garrow_numeric_array_mean() dcee4ad is described below commit dcee4ad1c5f62df0265e26f59ec58ed85597ffa2 Author: Yosuke Shiro <yosuke.shiro...@gmail.com> AuthorDate: Fri Mar 15 05:36:09 2019 +0900 ARROW-4859: [GLib] Add garrow_numeric_array_mean() Author: Yosuke Shiro <yosuke.shiro...@gmail.com> Closes #3889 from shiro615/glib-mean and squashes the following commits: 1d2abbf5 <Yosuke Shiro> Use assert_in_delta for floating point number e4be5213 <Yosuke Shiro> Use 0.0 for double dfff46be <Yosuke Shiro> Add garrow_numeric_array_mean() --- c_glib/arrow-glib/basic-array.cpp | 27 +++++++++++++++++++++++++++ c_glib/arrow-glib/basic-array.h | 4 ++++ c_glib/test/test-numeric-array.rb | 26 ++++++++++++++++++++++++++ cpp/src/arrow/compute/api.h | 1 + 4 files changed, 58 insertions(+) diff --git a/c_glib/arrow-glib/basic-array.cpp b/c_glib/arrow-glib/basic-array.cpp index 7409945..c201f9d 100644 --- a/c_glib/arrow-glib/basic-array.cpp +++ b/c_glib/arrow-glib/basic-array.cpp @@ -939,6 +939,33 @@ garrow_numeric_array_class_init(GArrowNumericArrayClass *klass) { } +/** + * garrow_numeric_array_mean: + * @array: A #GArrowNumericArray. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: The value of the computed mean. + * + * Since: 0.13.0 + */ +gdouble +garrow_numeric_array_mean(GArrowNumericArray *array, + GError **error) +{ + auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array)); + auto memory_pool = arrow::default_memory_pool(); + arrow::compute::FunctionContext context(memory_pool); + arrow::compute::Datum mean_datum; + auto status = arrow::compute::Mean(&context, arrow_array, &mean_datum); + if (garrow_error_check(error, status, "[numeric-array][mean]")) { + using ScalarType = typename arrow::TypeTraits<arrow::DoubleType>::ScalarType; + auto arrow_numeric_scalar = std::dynamic_pointer_cast<ScalarType>(mean_datum.scalar()); + return arrow_numeric_scalar->value; + } else { + return 0.0; + } +} + G_DEFINE_TYPE(GArrowInt8Array, garrow_int8_array, diff --git a/c_glib/arrow-glib/basic-array.h b/c_glib/arrow-glib/basic-array.h index 592699d..1dde2f2 100644 --- a/c_glib/arrow-glib/basic-array.h +++ b/c_glib/arrow-glib/basic-array.h @@ -212,6 +212,10 @@ struct _GArrowNumericArrayClass GArrowPrimitiveArrayClass parent_class; }; +GARROW_AVAILABLE_IN_0_13 +gdouble garrow_numeric_array_mean(GArrowNumericArray *array, + GError **error); + #define GARROW_TYPE_INT8_ARRAY (garrow_int8_array_get_type()) G_DECLARE_DERIVABLE_TYPE(GArrowInt8Array, garrow_int8_array, diff --git a/c_glib/test/test-numeric-array.rb b/c_glib/test/test-numeric-array.rb new file mode 100644 index 0000000..d919d59 --- /dev/null +++ b/c_glib/test/test-numeric-array.rb @@ -0,0 +1,26 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class TestNumericArray < Test::Unit::TestCase + include Helper::Buildable + + def test_mean + array = build_double_array([1.1, 2.2, nil]) + assert_in_delta(array.values.sum / 2, + array.mean) + end +end diff --git a/cpp/src/arrow/compute/api.h b/cpp/src/arrow/compute/api.h index cd5f11e..42839ce 100644 --- a/cpp/src/arrow/compute/api.h +++ b/cpp/src/arrow/compute/api.h @@ -24,5 +24,6 @@ #include "arrow/compute/kernels/boolean.h" // IWYU pragma: export #include "arrow/compute/kernels/cast.h" // IWYU pragma: export #include "arrow/compute/kernels/hash.h" // IWYU pragma: export +#include "arrow/compute/kernels/mean.h" // IWYU pragma: export #endif // ARROW_COMPUTE_API_H