Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cpp/src/arrow/compute/exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,14 @@ class ScalarExecutor : public KernelExecutorImpl<ScalarKernel> {
Status Execute(const ExecBatch& batch, ExecListener* listener) override {
RETURN_NOT_OK(span_iterator_.Init(batch, exec_context()->exec_chunksize()));

if (batch.length == 0) {
// A dictionary-to-dictionary cast must run to preserve unreferenced dictionary
// values even when there are no indices to process.
const bool changes_dictionary_type =
batch.num_values() == 1 && batch.values[0].type() != nullptr &&
is_dictionary(batch.values[0].type()->id()) &&
is_dictionary(output_type_.type->id()) &&
!batch.values[0].type()->Equals(*output_type_.type);
Comment on lines +788 to +792

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks... weird. Why the type unequality condition?

Also, this will trigger on every scalar compute function, not just the cast function.

if (batch.length == 0 && !changes_dictionary_type) {
// For zero-length batches, we do nothing except return a zero-length
// array of the correct output type
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Array> result,
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_cast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4573,6 +4573,19 @@ TEST(Cast, DictTypeToAnotherDict) {
Cast(arr, dictionary(int8(), int8()), CastOptions::Safe()));
}

TEST(Cast, EmptyDictionaryToAnotherDictionary) {
auto dictionary_values = ArrayFromJSON(utf8(), R"(["foo", "bar"])");

ASSERT_OK_AND_ASSIGN(auto input, DictionaryArray::FromArrays(
dictionary(int32(), utf8()),
ArrayFromJSON(int32(), "[]"), dictionary_values));
ASSERT_OK_AND_ASSIGN(auto result, Cast(*input, dictionary(int64(), utf8())));
auto dictionary_result = checked_pointer_cast<DictionaryArray>(result);

ASSERT_TRUE(dictionary_result->indices()->type()->Equals(int64()));
AssertArraysEqual(*dictionary_values, *dictionary_result->dictionary());
}

TEST(Cast, NoOutBitmapIfInIsAllValid) {
auto a = ArrayFromJSON(int8(), "[1]");
CastOptions options;
Expand Down
Loading