-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-35786: [C++] Add pairwise_diff function #35787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c77075e
Add pairwise diff function
js8544 789bc9e
make linter happy
js8544 885e78c
fix doc
js8544 1f8d8c5
fix warning
js8544 4a61a8f
refactor to two different functions for better extending to other ops
js8544 6741ba2
fix doc
js8544 fabd43a
use subtract kernel exec
js8544 c757748
lint
js8544 dadd720
use template for resolver
js8544 3d8179a
pr feedback
js8544 6da2dc2
save exec by value
js8544 d16d034
update var name
js8544 a6ad857
Update docs/source/cpp/compute.rst
js8544 fe641f0
Update cpp/src/arrow/compute/api_vector.h
js8544 17464af
update doc
js8544 d335071
Update docs/source/cpp/compute.rst
js8544 c51f659
lint
js8544 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // 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. | ||
|
|
||
| // Vector kernels for pairwise computation | ||
|
|
||
| #include <iostream> | ||
| #include <memory> | ||
| #include "arrow/builder.h" | ||
| #include "arrow/compute/api_vector.h" | ||
| #include "arrow/compute/exec.h" | ||
| #include "arrow/compute/function.h" | ||
| #include "arrow/compute/kernel.h" | ||
| #include "arrow/compute/kernels/base_arithmetic_internal.h" | ||
| #include "arrow/compute/kernels/codegen_internal.h" | ||
| #include "arrow/compute/registry.h" | ||
| #include "arrow/compute/util.h" | ||
| #include "arrow/status.h" | ||
| #include "arrow/type.h" | ||
| #include "arrow/type_fwd.h" | ||
| #include "arrow/type_traits.h" | ||
| #include "arrow/util/bit_util.h" | ||
| #include "arrow/util/checked_cast.h" | ||
| #include "arrow/util/logging.h" | ||
| #include "arrow/visit_type_inline.h" | ||
|
|
||
| namespace arrow::compute::internal { | ||
|
|
||
| // We reuse the kernel exec function of a scalar binary function to compute pairwise | ||
| // results. For example, for pairwise_diff, we reuse subtract's kernel exec. | ||
| struct PairwiseState : KernelState { | ||
| PairwiseState(const PairwiseOptions& options, ArrayKernelExec scalar_exec) | ||
| : periods(options.periods), scalar_exec(scalar_exec) {} | ||
|
|
||
| int64_t periods; | ||
| ArrayKernelExec scalar_exec; | ||
| }; | ||
|
|
||
| /// A generic pairwise implementation that can be reused by different ops. | ||
| Status PairwiseExecImpl(KernelContext* ctx, const ArraySpan& input, | ||
| const ArrayKernelExec& scalar_exec, int64_t periods, | ||
| ArrayData* result) { | ||
| // We only compute values in the region where the input-with-offset overlaps | ||
| // the original input. The margin where these do not overlap gets filled with null. | ||
| auto margin_length = std::min(abs(periods), input.length); | ||
| auto computed_length = input.length - margin_length; | ||
| auto margin_start = periods > 0 ? 0 : computed_length; | ||
| auto computed_start = periods > 0 ? margin_length : 0; | ||
| auto left_start = computed_start; | ||
| auto right_start = margin_length - computed_start; | ||
| // prepare bitmap | ||
| bit_util::ClearBitmap(result->buffers[0]->mutable_data(), margin_start, margin_length); | ||
| for (int64_t i = computed_start; i < computed_start + computed_length; i++) { | ||
| if (input.IsValid(i) && input.IsValid(i - periods)) { | ||
| bit_util::SetBit(result->buffers[0]->mutable_data(), i); | ||
| } else { | ||
| bit_util::ClearBit(result->buffers[0]->mutable_data(), i); | ||
| } | ||
| } | ||
| // prepare input span | ||
| ArraySpan left(input); | ||
| left.SetSlice(left_start, computed_length); | ||
| ArraySpan right(input); | ||
| right.SetSlice(right_start, computed_length); | ||
| // prepare output span | ||
| ArraySpan output_span; | ||
| output_span.SetMembers(*result); | ||
| output_span.offset = computed_start; | ||
| output_span.length = computed_length; | ||
| ExecResult output{output_span}; | ||
| // execute scalar function | ||
| RETURN_NOT_OK(scalar_exec(ctx, ExecSpan({left, right}, computed_length), &output)); | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status PairwiseExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { | ||
| const auto& state = checked_cast<const PairwiseState&>(*ctx->state()); | ||
| auto input = batch[0].array; | ||
| RETURN_NOT_OK(PairwiseExecImpl(ctx, batch[0].array, state.scalar_exec, state.periods, | ||
| out->array_data_mutable())); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| const FunctionDoc pairwise_diff_doc( | ||
| "Compute first order difference of an array", | ||
| ("Computes the first order difference of an array, It internally calls \n" | ||
| "the scalar function \"subtract\" to compute \n differences, so its \n" | ||
| "behavior and supported types are the same as \n" | ||
| "\"subtract\". The period can be specified in :struct:`PairwiseOptions`.\n" | ||
| "\n" | ||
| "Results will wrap around on integer overflow. Use function \n" | ||
| "\"pairwise_diff_checked\" if you want overflow to return an error."), | ||
| {"input"}, "PairwiseOptions"); | ||
|
|
||
| const FunctionDoc pairwise_diff_checked_doc( | ||
| "Compute first order difference of an array", | ||
| ("Computes the first order difference of an array, It internally calls \n" | ||
| "the scalar function \"subtract_checked\" (or the checked variant) to compute \n" | ||
| "differences, so its behavior and supported types are the same as \n" | ||
| "\"subtract_checked\". The period can be specified in :struct:`PairwiseOptions`.\n" | ||
| "\n" | ||
| "This function returns an error on overflow. For a variant that doesn't \n" | ||
| "fail on overflow, use function \"pairwise_diff\"."), | ||
| {"input"}, "PairwiseOptions"); | ||
|
|
||
| const PairwiseOptions* GetDefaultPairwiseOptions() { | ||
| static const auto kDefaultPairwiseOptions = PairwiseOptions::Defaults(); | ||
| return &kDefaultPairwiseOptions; | ||
| } | ||
|
|
||
| struct PairwiseKernelData { | ||
| InputType input; | ||
| OutputType output; | ||
| ArrayKernelExec exec; | ||
| }; | ||
|
|
||
| void RegisterPairwiseDiffKernels(std::string_view func_name, | ||
| std::string_view base_func_name, const FunctionDoc& doc, | ||
| FunctionRegistry* registry) { | ||
| VectorKernel kernel; | ||
| kernel.can_execute_chunkwise = false; | ||
| kernel.null_handling = NullHandling::COMPUTED_PREALLOCATE; | ||
| kernel.mem_allocation = MemAllocation::PREALLOCATE; | ||
| kernel.init = OptionsWrapper<PairwiseOptions>::Init; | ||
| auto func = std::make_shared<VectorFunction>(std::string(func_name), Arity::Unary(), | ||
| doc, GetDefaultPairwiseOptions()); | ||
|
|
||
| auto base_func_result = registry->GetFunction(std::string(base_func_name)); | ||
| DCHECK_OK(base_func_result.status()); | ||
| const auto& base_func = checked_cast<const ScalarFunction&>(**base_func_result); | ||
| DCHECK_EQ(base_func.arity().num_args, 2); | ||
|
|
||
| for (const auto& base_func_kernel : base_func.kernels()) { | ||
| const auto& base_func_kernel_sig = base_func_kernel->signature; | ||
| if (!base_func_kernel_sig->in_types()[0].Equals( | ||
| base_func_kernel_sig->in_types()[1])) { | ||
| continue; | ||
| } | ||
| OutputType out_type(base_func_kernel_sig->out_type()); | ||
| // Need to wrap base output resolver | ||
| if (out_type.kind() == OutputType::COMPUTED) { | ||
| out_type = | ||
| OutputType([base_resolver = base_func_kernel_sig->out_type().resolver()]( | ||
| KernelContext* ctx, const std::vector<TypeHolder>& input_types) { | ||
| return base_resolver(ctx, {input_types[0], input_types[0]}); | ||
| }); | ||
| } | ||
|
|
||
| kernel.signature = | ||
| KernelSignature::Make({base_func_kernel_sig->in_types()[0]}, out_type); | ||
| kernel.exec = PairwiseExec; | ||
| kernel.init = [scalar_exec = base_func_kernel->exec](KernelContext* ctx, | ||
| const KernelInitArgs& args) { | ||
| return std::make_unique<PairwiseState>( | ||
| checked_cast<const PairwiseOptions&>(*args.options), scalar_exec); | ||
| }; | ||
| DCHECK_OK(func->AddKernel(kernel)); | ||
| } | ||
|
|
||
| DCHECK_OK(registry->AddFunction(std::move(func))); | ||
| } | ||
|
|
||
| void RegisterVectorPairwise(FunctionRegistry* registry) { | ||
| RegisterPairwiseDiffKernels("pairwise_diff", "subtract", pairwise_diff_doc, registry); | ||
| RegisterPairwiseDiffKernels("pairwise_diff_checked", "subtract_checked", | ||
| pairwise_diff_checked_doc, registry); | ||
| } | ||
|
|
||
| } // namespace arrow::compute::internal |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.