-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GH-41810: [C++] Support cast kernel from (dense or sparse) union to (large) string #41827
Open
llama90
wants to merge
9
commits into
apache:main
Choose a base branch
from
llama90:ARROW-41810
base: main
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1966c31
support cast from union to string
llama90 42af932
apply lint
llama90 a59133f
add test for large string (large_utf8())
llama90 bbc5b0a
fix offset for dense and sparse union scalar
llama90 337ffca
update unit test to remove duplicate
llama90 597439e
update stringstream to string because there is no complex manipulation
llama90 6afefaf
add test for large string (large_utf8())
llama90 007dafb
apply lint
llama90 939f3e2
update test
llama90 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 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 |
---|---|---|
|
@@ -510,6 +510,60 @@ void AddBinaryToFixedSizeBinaryCast(CastFunction* func) { | |
AddBinaryToFixedSizeBinaryCast<FixedSizeBinaryType>(func); | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Union to String | ||
|
||
template <typename O> | ||
struct UnionToStringCastFunctor { | ||
using BuilderType = typename TypeTraits<O>::BuilderType; | ||
|
||
static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { | ||
const ArraySpan& input = batch[0].array; | ||
const auto& union_type = checked_cast<const UnionType&>(*input.type); | ||
const auto type_ids = input.GetValues<int8_t>(1); | ||
const auto& offsets = input.GetValues<int32_t>(2); | ||
|
||
BuilderType builder(input.type->GetSharedPtr(), ctx->memory_pool()); | ||
RETURN_NOT_OK(builder.Reserve(input.length)); | ||
|
||
for (int64_t i = 0; i < input.length; ++i) { | ||
if (input.IsNull(i)) { | ||
RETURN_NOT_OK(builder.AppendNull()); | ||
continue; | ||
} | ||
|
||
const int8_t type_id = type_ids[i]; | ||
const auto& field = union_type.field(union_type.child_ids()[type_id]); | ||
const ArraySpan& child_span = input.child_data[union_type.child_ids()[type_id]]; | ||
|
||
std::shared_ptr<Scalar> child_scalar; | ||
auto child_index = union_type.mode() == UnionMode::DENSE ? offsets[i] : i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a template parameter (a |
||
RETURN_NOT_OK(child_span.ToArray()->GetScalar(child_index).Value(&child_scalar)); | ||
|
||
std::string str = "union{" + field->name() + ": " + field->type()->ToString() + | ||
" = " + child_scalar->ToString() + "}"; | ||
RETURN_NOT_OK(builder.Append(str)); | ||
} | ||
|
||
std::shared_ptr<Array> output_array; | ||
RETURN_NOT_OK(builder.Finish(&output_array)); | ||
out->value = output_array->data(); | ||
return Status::OK(); | ||
} | ||
}; | ||
|
||
template <typename OutType> | ||
void AddUnionToStringCast(CastFunction* func) { | ||
auto out_ty = TypeTraits<OutType>::type_singleton(); | ||
|
||
DCHECK_OK(func->AddKernel(Type::DENSE_UNION, {InputType(Type::DENSE_UNION)}, out_ty, | ||
UnionToStringCastFunctor<OutType>::Exec, | ||
NullHandling::COMPUTED_NO_PREALLOCATE)); | ||
DCHECK_OK(func->AddKernel(Type::SPARSE_UNION, {InputType(Type::SPARSE_UNION)}, out_ty, | ||
UnionToStringCastFunctor<OutType>::Exec, | ||
NullHandling::COMPUTED_NO_PREALLOCATE)); | ||
} | ||
|
||
} // namespace | ||
|
||
std::vector<std::shared_ptr<CastFunction>> GetBinaryLikeCasts() { | ||
|
@@ -528,6 +582,7 @@ std::vector<std::shared_ptr<CastFunction>> GetBinaryLikeCasts() { | |
AddDecimalToStringCasts<StringType>(cast_string.get()); | ||
AddTemporalToStringCasts<StringType>(cast_string.get()); | ||
AddBinaryToBinaryCast<StringType>(cast_string.get()); | ||
AddUnionToStringCast<StringType>(cast_string.get()); | ||
|
||
auto cast_large_string = | ||
std::make_shared<CastFunction>("cast_large_string", Type::LARGE_STRING); | ||
|
@@ -536,6 +591,7 @@ std::vector<std::shared_ptr<CastFunction>> GetBinaryLikeCasts() { | |
AddDecimalToStringCasts<LargeStringType>(cast_large_string.get()); | ||
AddTemporalToStringCasts<LargeStringType>(cast_large_string.get()); | ||
AddBinaryToBinaryCast<LargeStringType>(cast_large_string.get()); | ||
AddUnionToStringCast<LargeStringType>(cast_large_string.get()); | ||
|
||
auto cast_fsb = | ||
std::make_shared<CastFunction>("cast_fixed_size_binary", Type::FIXED_SIZE_BINARY); | ||
|
This file contains 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
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.
Maybe it's a better way to implement this by expand
StringFormatter
(include other nested types)? @felipecrvIn this way, we can unify it with other type's implementations and shield the logic of converting strings in the current file.
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.
Thank you for the review and the insightful suggestions.
That makes sense, and I'll consider applying that.
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.
Also, this issue is related: #41831.
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.
True, but that can prevent optimizations in the future. The approach of taking a scalar function and turning it into an array function by mapping —
array::map(scalar_function: scalar -> scalar) -> array
— is appealing but prevents vectorization techniques.UPDATE: that's what we will do here because the set of unions and their parametrizations is infinite, but
StringFormatter<MonthIntervalType>
is not the way to go because it would have toswitch
on the type for every invocation of the formatter.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.
A string formatter for unions should allocate a vector of string formatters that can do virtual dispatching (and deal with nesting themselves as well).
StringFormatter<T>
performs static dispatch which allows loop-specialization for the non-nested types. But for nested types we will need to setup a vector ofVirtualStringFormatter
(which is actually a tree) so that all the "switching on the type" happens at construction time (beginning of the loop) and invocations inside the loop are following the same function pointers from the vtables.This hierarchy would be similar to the builder class hierarchy.