-
Notifications
You must be signed in to change notification settings - Fork 160
feat[gpu]: widen decimals for Arrow device export #8155
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
Open
0ax1
wants to merge
1
commit into
develop
Choose a base branch
from
ad/arrow-device-decimal
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−33
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include "config.cuh" | ||
| #include "types.cuh" | ||
| #include <stdint.h> | ||
| #include <type_traits> | ||
|
|
||
| // Arrow decimal schemas fix the physical values buffer width: | ||
| // - Decimal128: 16 bytes per value. | ||
| // - Decimal256: 32 bytes per value. | ||
| // | ||
| // Vortex may use narrower decimal storage, so Arrow Device export widens values | ||
| // to match the schema-implied physical layout consumed by cuDF and other Arrow | ||
| // readers. | ||
| // Converts a decimal storage value to Arrow's 128-bit decimal physical representation. | ||
| template <typename Input> | ||
| __device__ __forceinline__ int128_t decimal_to_i128(Input value) { | ||
| if constexpr (std::is_same_v<Input, int128_t>) { | ||
| return value; | ||
| } else if constexpr (std::is_same_v<Input, int256_t>) { | ||
| return int128_t {value.parts[0], value.parts[1]}; | ||
| } else { | ||
| const int64_t lo = static_cast<int64_t>(value); | ||
| const int64_t hi = value < 0 ? -1 : 0; | ||
| return int128_t {lo, hi}; | ||
| } | ||
| } | ||
|
|
||
| // Converts one decimal value to the requested Arrow decimal physical representation. | ||
| template <typename Output, typename Input> | ||
| __device__ __forceinline__ Output decimal_cast_value(Input value) { | ||
| if constexpr (std::is_same_v<Output, int256_t> && std::is_same_v<Input, int256_t>) { | ||
| return value; | ||
| } else if constexpr (std::is_same_v<Output, int128_t>) { | ||
| return decimal_to_i128(value); | ||
| } else { | ||
| static_assert(std::is_same_v<Output, int256_t>); | ||
| const int128_t value128 = decimal_to_i128(value); | ||
| const int64_t sign = value128.hi < 0 ? -1 : 0; | ||
| return int256_t {{value128.lo, value128.hi, sign, sign}}; | ||
| } | ||
| } | ||
|
|
||
| // Widens a contiguous decimal values buffer on the device. | ||
| template <typename Input, typename Output> | ||
| __device__ void | ||
| decimal_cast_device(const Input *__restrict input, Output *__restrict output, uint64_t array_len) { | ||
| const uint64_t worker = blockIdx.x * blockDim.x + threadIdx.x; | ||
| const uint64_t startElem = start_elem(worker, array_len); | ||
| const uint64_t stopElem = stop_elem(worker, array_len); | ||
|
|
||
| if (startElem >= array_len) { | ||
| return; | ||
| } | ||
|
|
||
| for (uint64_t idx = startElem; idx < stopElem; idx++) { | ||
| output[idx] = decimal_cast_value<Output>(input[idx]); | ||
| } | ||
| } | ||
|
|
||
| // Generates Arrow Decimal128 and Decimal256 widening kernels for one input storage type. | ||
| #define GENERATE_DECIMAL_CAST_KERNELS(input_suffix, InputType) \ | ||
| extern "C" __global__ void decimal_cast_##input_suffix##_i128(const InputType *__restrict input, \ | ||
| int128_t *__restrict output, \ | ||
| uint64_t array_len) { \ | ||
| decimal_cast_device(input, output, array_len); \ | ||
| } \ | ||
| extern "C" __global__ void decimal_cast_##input_suffix##_i256(const InputType *__restrict input, \ | ||
| int256_t *__restrict output, \ | ||
| uint64_t array_len) { \ | ||
| decimal_cast_device(input, output, array_len); \ | ||
| } | ||
|
|
||
| FOR_EACH_SIGNED_INT(GENERATE_DECIMAL_CAST_KERNELS) | ||
| FOR_EACH_LARGE_DECIMAL(GENERATE_DECIMAL_CAST_KERNELS) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this end up truncating? I think it is currently possible in vortex to do this:
when exporting this array we would pick the i256 -> i128 kernel and truncate the values without checking for overflow.
probably the right fix is for vortex to reject constructing such arrays