Skip to content

Commit

Permalink
ARROW-4859: [GLib] Add garrow_numeric_array_mean()
Browse files Browse the repository at this point in the history
Author: Yosuke Shiro <[email protected]>

Closes apache#3889 from shiro615/glib-mean and squashes the following commits:

1d2abbf <Yosuke Shiro> Use assert_in_delta for floating point number
e4be521 <Yosuke Shiro> Use 0.0 for double
dfff46b <Yosuke Shiro> Add garrow_numeric_array_mean()
  • Loading branch information
shiro615 authored and kou committed Mar 14, 2019
1 parent 954e3f4 commit dcee4ad
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions c_glib/arrow-glib/basic-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/basic-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions c_glib/test/test-numeric-array.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions cpp/src/arrow/compute/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dcee4ad

Please sign in to comment.