From 1f3471e6f0a34532a80d76afa63cde03e838c476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9B=9E=E5=BD=92?= Date: Fri, 2 Feb 2024 16:56:33 +0800 Subject: [PATCH 1/3] close GH-39860 --- cpp/src/arrow/compute/expression.cc | 22 +++++++++++++++------- cpp/src/arrow/compute/expression_test.cc | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/cpp/src/arrow/compute/expression.cc b/cpp/src/arrow/compute/expression.cc index b47e0a35525c..2c044472c2d2 100644 --- a/cpp/src/arrow/compute/expression.cc +++ b/cpp/src/arrow/compute/expression.cc @@ -750,15 +750,23 @@ Result ExecuteScalarExpression(const Expression& expr, const ExecBatch& i auto call = CallNotNull(expr); - std::vector arguments(call->arguments.size()); - + size_t num_args = call->arguments.size(); + std::vector arguments(num_args); bool all_scalar = true; - for (size_t i = 0; i < arguments.size(); ++i) { - ARROW_ASSIGN_OR_RAISE( - arguments[i], ExecuteScalarExpression(call->arguments[i], input, exec_context)); - if (arguments[i].is_array()) { - all_scalar = false; + + if (num_args) { + for (size_t i = 0; i < num_args; ++i) { + ARROW_ASSIGN_OR_RAISE( + arguments[i], ExecuteScalarExpression(call->arguments[i], input, exec_context)); + if (arguments[i].is_array()) { + all_scalar = false; + } } + } else { + // See ARROW-39860. When num_args == 0, there is no problem in continuing to execute + // subsequent logic, because we have already found the kernel that the current + // expression can execute during Bind. + all_scalar = false; } auto executor = compute::detail::KernelExecutor::MakeScalar(); diff --git a/cpp/src/arrow/compute/expression_test.cc b/cpp/src/arrow/compute/expression_test.cc index 44159e76600f..45353c74f267 100644 --- a/cpp/src/arrow/compute/expression_test.cc +++ b/cpp/src/arrow/compute/expression_test.cc @@ -863,6 +863,23 @@ TEST(Expression, ExecuteCall) { ])")); } +TEST(Expression, ExecuteCallWithNoArguments) { + const int kCount = 10; + auto random_options = RandomOptions::FromSeed(/*seed=*/0); + ExecBatch input({}, kCount); + + Expression random_expr = call("random", {}, random_options); + ASSERT_OK_AND_ASSIGN(random_expr, random_expr.Bind(float64())); + + ASSERT_OK_AND_ASSIGN(Datum actual, ExecuteScalarExpression(random_expr, input)); + compute::ExecContext* exec_context = default_exec_context(); + ASSERT_OK_AND_ASSIGN(auto function, + exec_context->func_registry()->GetFunction("random")); + ASSERT_OK_AND_ASSIGN(Datum expected, + function->Execute(input, &random_options, exec_context)); + AssertDatumsEqual(actual, expected, /*verbose=*/true); +} + TEST(Expression, ExecuteDictionaryTransparent) { ExpectExecute( equal(field_ref("a"), field_ref("b")), From e00e5bf0628bb778a783a710f9198b8da3e80e81 Mon Sep 17 00:00:00 2001 From: "hugo.zhang" Date: Sat, 3 Feb 2024 11:05:44 +0800 Subject: [PATCH 2/3] fix comments --- cpp/src/arrow/compute/expression.cc | 33 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/cpp/src/arrow/compute/expression.cc b/cpp/src/arrow/compute/expression.cc index 2c044472c2d2..8c59ad1df86f 100644 --- a/cpp/src/arrow/compute/expression.cc +++ b/cpp/src/arrow/compute/expression.cc @@ -750,23 +750,24 @@ Result ExecuteScalarExpression(const Expression& expr, const ExecBatch& i auto call = CallNotNull(expr); - size_t num_args = call->arguments.size(); - std::vector arguments(num_args); - bool all_scalar = true; + std::vector arguments(call->arguments.size()); - if (num_args) { - for (size_t i = 0; i < num_args; ++i) { - ARROW_ASSIGN_OR_RAISE( - arguments[i], ExecuteScalarExpression(call->arguments[i], input, exec_context)); - if (arguments[i].is_array()) { - all_scalar = false; - } + bool all_scalar = true; + for (size_t i = 0; i < arguments.size(); ++i) { + ARROW_ASSIGN_OR_RAISE( + arguments[i], ExecuteScalarExpression(call->arguments[i], input, exec_context)); + if (arguments[i].is_array()) { + all_scalar = false; } + } + + int64_t input_length; + if (!arguments.empty() && all_scalar) { + // all inputs are scalar, so use a 1-long batch to avoid + // computing input.length equivalent outputs + input_length = 1; } else { - // See ARROW-39860. When num_args == 0, there is no problem in continuing to execute - // subsequent logic, because we have already found the kernel that the current - // expression can execute during Bind. - all_scalar = false; + input_length = input.length; } auto executor = compute::detail::KernelExecutor::MakeScalar(); @@ -780,8 +781,8 @@ Result ExecuteScalarExpression(const Expression& expr, const ExecBatch& i RETURN_NOT_OK(executor->Init(&kernel_context, {kernel, types, options})); compute::detail::DatumAccumulator listener; - RETURN_NOT_OK(executor->Execute( - ExecBatch(std::move(arguments), all_scalar ? 1 : input.length), &listener)); + RETURN_NOT_OK( + executor->Execute(ExecBatch(std::move(arguments), input_length), &listener)); const auto out = executor->WrapResults(arguments, listener.values()); #ifndef NDEBUG DCHECK_OK(executor->CheckResultType(out, call->function_name.c_str())); From b79cbfde4da04f4e8305ae5fadeb1879521d2853 Mon Sep 17 00:00:00 2001 From: "hugo.zhang" Date: Sat, 3 Feb 2024 19:37:01 +0800 Subject: [PATCH 3/3] fix --- cpp/src/arrow/compute/expression_test.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/src/arrow/compute/expression_test.cc b/cpp/src/arrow/compute/expression_test.cc index 45353c74f267..d33c348cd77d 100644 --- a/cpp/src/arrow/compute/expression_test.cc +++ b/cpp/src/arrow/compute/expression_test.cc @@ -878,6 +878,8 @@ TEST(Expression, ExecuteCallWithNoArguments) { ASSERT_OK_AND_ASSIGN(Datum expected, function->Execute(input, &random_options, exec_context)); AssertDatumsEqual(actual, expected, /*verbose=*/true); + + EXPECT_EQ(actual.length(), kCount); } TEST(Expression, ExecuteDictionaryTransparent) {