diff --git a/c_glib/arrow-glib/arrow-glib.h b/c_glib/arrow-glib/arrow-glib.h index 272b6ba1dae1..76dfbd58f84c 100644 --- a/c_glib/arrow-glib/arrow-glib.h +++ b/c_glib/arrow-glib/arrow-glib.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/c_glib/arrow-glib/compute.cpp b/c_glib/arrow-glib/compute.cpp index 5f494f3bc7ba..770e51dc0363 100644 --- a/c_glib/arrow-glib/compute.cpp +++ b/c_glib/arrow-glib/compute.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -273,10 +274,11 @@ garrow_compute_initialize(GError **error) return garrow::check(error, status, "[compute][initialize]"); } -typedef struct GArrowExecuteContextPrivate_ +struct GArrowExecuteContextPrivate { - arrow::compute::ExecContext context; -} GArrowExecuteContextPrivate; + std::shared_ptr context; + GArrowExecutor *executor; +}; G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_OBJECT) @@ -284,19 +286,78 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowExecuteContext, garrow_execute_context, G_TYPE_ static_cast( \ garrow_execute_context_get_instance_private(GARROW_EXECUTE_CONTEXT(object))) +enum { + PROP_EXECUTOR = 1, +}; + +static void +garrow_execute_context_dispose(GObject *object) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + if (priv->executor) { + g_object_unref(priv->executor); + priv->executor = nullptr; + } + + G_OBJECT_CLASS(garrow_execute_context_parent_class)->dispose(object); +} + static void garrow_execute_context_finalize(GObject *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - priv->context.~ExecContext(); + priv->context.~shared_ptr(); G_OBJECT_CLASS(garrow_execute_context_parent_class)->finalize(object); } +static void +garrow_execute_context_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_EXECUTOR: + { + priv->executor = GARROW_EXECUTOR(g_value_dup_object(value)); + auto arrow_executor = garrow_executor_get_raw(priv->executor); + priv->context = + std::make_shared(arrow::default_memory_pool(), + arrow_executor.get()); + break; + } + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_execute_context_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_EXECUTOR: + g_value_set_object(value, priv->executor); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + static void garrow_execute_context_init(GArrowExecuteContext *object) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(object); - new (&priv->context) arrow::compute::ExecContext(arrow::default_memory_pool(), nullptr); + new (&priv->context) std::shared_ptr; } static void @@ -304,21 +365,41 @@ garrow_execute_context_class_init(GArrowExecuteContextClass *klass) { auto gobject_class = G_OBJECT_CLASS(klass); + gobject_class->dispose = garrow_execute_context_dispose; gobject_class->finalize = garrow_execute_context_finalize; + gobject_class->set_property = garrow_execute_context_set_property; + gobject_class->get_property = garrow_execute_context_get_property; + + GParamSpec *spec; + /** + * GArrowExecuteContext:executor: + * + * The executor for execution. + * + * Since: 23.0.0 + */ + spec = g_param_spec_object( + "executor", + "Executor", + "The executor for execution", + GARROW_TYPE_EXECUTOR, + static_cast(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec); } /** * garrow_execute_context_new: + * @executor: (nullable): A #GArrowExecutor or %NULL. * * Returns: A newly created #GArrowExecuteContext. * * Since: 1.0.0 */ GArrowExecuteContext * -garrow_execute_context_new(void) +garrow_execute_context_new(GArrowExecutor *executor) { - auto execute_context = g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, NULL); - return GARROW_EXECUTE_CONTEXT(execute_context); + return GARROW_EXECUTE_CONTEXT( + g_object_new(GARROW_TYPE_EXECUTE_CONTEXT, "executor", executor, nullptr)); } typedef struct GArrowFunctionOptionsPrivate_ @@ -1877,6 +1958,7 @@ garrow_execute_plan_class_init(GArrowExecutePlanClass *klass) /** * garrow_execute_plan_new: + * @context: (nullable): A #GArrowExecuteContext or %NULL. * @error: (nullable): Return location for a #GError or %NULL. * * Returns: (nullable): A newly created #GArrowExecutePlan on success, @@ -1885,9 +1967,15 @@ garrow_execute_plan_class_init(GArrowExecutePlanClass *klass) * Since: 6.0.0 */ GArrowExecutePlan * -garrow_execute_plan_new(GError **error) +garrow_execute_plan_new(GArrowExecuteContext *context, GError **error) { - auto arrow_plan_result = arrow::acero::ExecPlan::Make(); + arrow::Result> arrow_plan_result; + if (context) { + auto arrow_context = garrow_execute_context_get_raw(context); + arrow_plan_result = arrow::acero::ExecPlan::Make(*arrow_context); + } else { + arrow_plan_result = arrow::acero::ExecPlan::Make(); + } if (garrow::check(error, arrow_plan_result, "[execute-plan][new]")) { return GARROW_EXECUTE_PLAN( g_object_new(GARROW_TYPE_EXECUTE_PLAN, "plan", &(*arrow_plan_result), NULL)); @@ -6355,7 +6443,7 @@ arrow::compute::ExecContext * garrow_execute_context_get_raw(GArrowExecuteContext *context) { auto priv = GARROW_EXECUTE_CONTEXT_GET_PRIVATE(context); - return &priv->context; + return priv->context.get(); } GArrowFunctionOptions * diff --git a/c_glib/arrow-glib/compute.h b/c_glib/arrow-glib/compute.h index 0f689d147e3f..ce9515603243 100644 --- a/c_glib/arrow-glib/compute.h +++ b/c_glib/arrow-glib/compute.h @@ -21,6 +21,7 @@ #include #include +#include #include G_BEGIN_DECLS @@ -40,7 +41,7 @@ struct _GArrowExecuteContextClass GARROW_AVAILABLE_IN_1_0 GArrowExecuteContext * -garrow_execute_context_new(void); +garrow_execute_context_new(GArrowExecutor *executor); GARROW_AVAILABLE_IN_7_0 gboolean @@ -322,7 +323,7 @@ struct _GArrowExecutePlanClass GARROW_AVAILABLE_IN_6_0 GArrowExecutePlan * -garrow_execute_plan_new(GError **error); +garrow_execute_plan_new(GArrowExecuteContext *context, GError **error); GARROW_AVAILABLE_IN_6_0 GArrowExecuteNode * garrow_execute_plan_build_node(GArrowExecutePlan *plan, diff --git a/c_glib/arrow-glib/executor.cpp b/c_glib/arrow-glib/executor.cpp new file mode 100644 index 000000000000..26cb4f5fe974 --- /dev/null +++ b/c_glib/arrow-glib/executor.cpp @@ -0,0 +1,150 @@ +/* + * 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. + */ + +#include +#include + +#include + +G_BEGIN_DECLS + +/** + * SECTION: executor + * @section_id: executor-classes + * @title: Executor classes + * @include: arrow-glib/arrow-glib.h + * + * #GArrowExecutor is the base class for executor implementations. + * + * #GArrowThreadPool is a class for thread pool management. + */ + +struct GArrowExecutorPrivate +{ + std::shared_ptr executor; +}; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExecutor, garrow_executor, G_TYPE_OBJECT) + +#define GARROW_EXECUTOR_GET_PRIVATE(obj) \ + static_cast( \ + garrow_executor_get_instance_private(GARROW_EXECUTOR(obj))) + +enum { + PROP_EXECUTOR = 1, +}; + +static void +garrow_executor_init(GArrowExecutor *object) +{ + auto priv = GARROW_EXECUTOR_GET_PRIVATE(object); + new (&priv->executor) std::shared_ptr; +} + +static void +garrow_executor_finalize(GObject *object) +{ + auto priv = GARROW_EXECUTOR_GET_PRIVATE(object); + priv->executor.~shared_ptr(); + G_OBJECT_CLASS(garrow_executor_parent_class)->finalize(object); +} + +static void +garrow_executor_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_EXECUTOR_GET_PRIVATE(GARROW_EXECUTOR(object)); + + switch (prop_id) { + case PROP_EXECUTOR: + priv->executor = *static_cast *>( + g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_executor_class_init(GArrowExecutorClass *klass) +{ + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_executor_finalize; + gobject_class->set_property = garrow_executor_set_property; + + GParamSpec *spec; + spec = g_param_spec_pointer( + "executor", + "Executor", + "The raw std::shared_ptr *", + static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_EXECUTOR, spec); +} + +G_DEFINE_TYPE(GArrowThreadPool, garrow_thread_pool, GARROW_TYPE_EXECUTOR) + +static void +garrow_thread_pool_init(GArrowThreadPool *object) +{ +} + +static void +garrow_thread_pool_class_init(GArrowThreadPoolClass *klass) +{ +} + +/** + * garrow_thread_pool_new: + * @n_threads: The number of threads in the pool. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: (nullable): A newly created #GArrowThreadPool on success, + * %NULL on error. + * + * Since: 23.0.0 + */ +GArrowThreadPool * +garrow_thread_pool_new(guint n_threads, GError **error) +{ + auto arrow_thread_pool_result = arrow::internal::ThreadPool::Make(n_threads); + if (garrow::check(error, arrow_thread_pool_result, "[thread-pool][new]")) { + auto arrow_thread_pool = *arrow_thread_pool_result; + auto thread_pool = GARROW_THREAD_POOL( + g_object_new(GARROW_TYPE_THREAD_POOL, "executor", &arrow_thread_pool, nullptr)); + return thread_pool; + } else { + return nullptr; + } +} + +G_END_DECLS + +std::shared_ptr +garrow_executor_get_raw(GArrowExecutor *executor) +{ + if (!executor) + return nullptr; + + auto priv = GARROW_EXECUTOR_GET_PRIVATE(executor); + return priv->executor; +} diff --git a/c_glib/arrow-glib/executor.h b/c_glib/arrow-glib/executor.h new file mode 100644 index 000000000000..50dab0b26e5e --- /dev/null +++ b/c_glib/arrow-glib/executor.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +#pragma once + +#include + +#include + +G_BEGIN_DECLS + +#define GARROW_TYPE_EXECUTOR (garrow_executor_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE(GArrowExecutor, garrow_executor, GARROW, EXECUTOR, GObject) +struct _GArrowExecutorClass +{ + GObjectClass parent_class; +}; + +#define GARROW_TYPE_THREAD_POOL (garrow_thread_pool_get_type()) +GARROW_AVAILABLE_IN_23_0 +G_DECLARE_DERIVABLE_TYPE( + GArrowThreadPool, garrow_thread_pool, GARROW, THREAD_POOL, GArrowExecutor) +struct _GArrowThreadPoolClass +{ + GArrowExecutorClass parent_class; +}; + +GARROW_AVAILABLE_IN_23_0 +GArrowThreadPool * +garrow_thread_pool_new(guint n_threads, GError **error); + +G_END_DECLS diff --git a/c_glib/arrow-glib/executor.hpp b/c_glib/arrow-glib/executor.hpp new file mode 100644 index 000000000000..e91823186ffb --- /dev/null +++ b/c_glib/arrow-glib/executor.hpp @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#pragma once + +#include + +std::shared_ptr +garrow_executor_get_raw(GArrowExecutor *executor); diff --git a/c_glib/arrow-glib/meson.build b/c_glib/arrow-glib/meson.build index b755ffb56ac9..023b02e012e4 100644 --- a/c_glib/arrow-glib/meson.build +++ b/c_glib/arrow-glib/meson.build @@ -30,6 +30,7 @@ sources = files( 'decimal.cpp', 'decoder.cpp', 'error.cpp', + 'executor.cpp', 'expression.cpp', 'field.cpp', 'interval.cpp', @@ -87,6 +88,7 @@ c_headers = files( 'decimal.h', 'decoder.h', 'error.h', + 'executor.h', 'expression.h', 'field.h', 'interval.h', @@ -137,6 +139,7 @@ cpp_headers = files( 'decimal.hpp', 'decoder.hpp', 'error.hpp', + 'executor.hpp', 'expression.hpp', 'field.hpp', 'interval.hpp', diff --git a/c_glib/test/test-execute-context.rb b/c_glib/test/test-execute-context.rb new file mode 100644 index 000000000000..fc6873d0a3c4 --- /dev/null +++ b/c_glib/test/test-execute-context.rb @@ -0,0 +1,30 @@ +# 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 TestExecuteContext < Test::Unit::TestCase + def setup + @context = Arrow::ExecuteContext.new + end + + def test_executor + assert_nil(@context.executor) + + executor = Arrow::ThreadPool.new(1) + context = Arrow::ExecuteContext.new(executor) + assert_equal(executor, context.executor) + end +end diff --git a/c_glib/test/test-execute-plan.rb b/c_glib/test/test-execute-plan.rb index da1433db559c..33ae686ea991 100644 --- a/c_glib/test/test-execute-plan.rb +++ b/c_glib/test/test-execute-plan.rb @@ -28,8 +28,8 @@ def execute(plan) end sub_test_case("aggregate") do - def build_plan - plan = Arrow::ExecutePlan.new + def build_plan(context = nil) + plan = Arrow::ExecutePlan.new(context) record_batch = build_record_batch(number: build_int8_array([1, 2, 3, 4, 5]), @@ -63,6 +63,22 @@ def test_by_string reader.read_all) end end + + def test_hash_first_with_single_thread + thread_pool = Arrow::ThreadPool.new(1) + context = Arrow::ExecuteContext.new(thread_pool) + plan, reader = build_plan(context) do + aggregations = [ + Arrow::Aggregation.new("hash_first", nil, "number", "first(number)"), + ] + Arrow::AggregateNodeOptions.new(aggregations, ["string"]) + end + execute(plan) do + assert_equal(build_table("string" => build_string_array(["a", "b"]), + "first(number)" => build_int8_array([1, 2])), + reader.read_all) + end + end end sub_test_case("hash join") do