Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be NaN?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use NaN, std::numeric_limits<double>::signaling_NaN() https://en.cppreference.com/w/cpp/types/numeric_limits/signaling_NaN will be better.
But I don't think this is required. Because we indicate whether error is occurred by error argument. Users should check error instead of checking return value for error check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If @shiro615 changes this before I merge, I'll accept the change. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users can check error argument. I want to use 0.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

}
}


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