Skip to content
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

Handle empty dictionary in to_arrow_device interop #18121

Draft
wants to merge 1 commit into
base: branch-25.04
Choose a base branch
from
Draft
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
14 changes: 11 additions & 3 deletions cpp/src/interop/to_arrow_device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "arrow_utilities.hpp"

#include <cudf/column/column.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/detail/device_scalar.hpp>
#include <cudf/detail/interop.hpp>
Expand Down Expand Up @@ -266,21 +267,28 @@ int dispatch_to_arrow_device::operator()<cudf::dictionary32>(cudf::column&& colu
ArrowArray* out)
{
nanoarrow::UniqueArray tmp;

cudf::dictionary_column_view const dview{column.view()};

NANOARROW_RETURN_NOT_OK(initialize_array(
tmp.get(),
id_to_arrow_type(column.child(cudf::dictionary_column_view::indices_column_index).type().id()),
dview.is_empty() ? NANOARROW_TYPE_INT32 : id_to_arrow_type(dview.indices().type().id()),
column));
NANOARROW_RETURN_NOT_OK(ArrowArrayAllocateDictionary(tmp.get()));

auto contents = column.release();
NANOARROW_RETURN_NOT_OK(set_null_mask(contents, tmp.get()));

auto indices_contents =
contents.children[cudf::dictionary_column_view::indices_column_index]->release();
dview.is_empty()
? cudf::make_empty_column(cudf::type_id::INT32)->release()
: contents.children[cudf::dictionary_column_view::indices_column_index]->release();
NANOARROW_RETURN_NOT_OK(
set_buffer(std::move(indices_contents.data), fixed_width_data_buffer_idx, tmp.get()));

auto& keys = contents.children[cudf::dictionary_column_view::keys_column_index];
auto keys = dview.is_empty()
? cudf::make_empty_column(cudf::type_id::INT64)
: std::move(contents.children[cudf::dictionary_column_view::keys_column_index]);
NANOARROW_RETURN_NOT_OK(cudf::type_dispatcher(
keys->type(), dispatch_to_arrow_device{}, std::move(*keys), stream, mr, tmp->dictionary));

Expand Down
4 changes: 3 additions & 1 deletion cpp/src/interop/to_arrow_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ int dispatch_to_arrow_type::operator()<cudf::dictionary32>(column_view input,
{
cudf::dictionary_column_view const dview{input};

NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(out, id_to_arrow_type(dview.indices().type().id())));
NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(
out, dview.is_empty() ? NANOARROW_TYPE_INT32 : id_to_arrow_type(dview.indices().type().id())));
NANOARROW_RETURN_NOT_OK(ArrowSchemaAllocateDictionary(out));
ArrowSchemaInit(out->dictionary);
if (dview.is_empty()) { return ArrowSchemaSetType(out->dictionary, NANOARROW_TYPE_INT64); }

auto dict_keys = dview.keys();
return cudf::type_dispatcher(
Expand Down
12 changes: 12 additions & 0 deletions cpp/tests/interop/to_arrow_device_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ TEST_F(ToArrowDeviceTest, EmptyTable)
compare_arrays(schema.get(), arr.get(), &got_arrow_device->array);
}

TEST_F(ToArrowDeviceTest, EmptyDictionary)
{
auto empty = cudf::make_empty_column(cudf::type_id::DICTIONARY32);
auto meta = std::vector<cudf::column_metadata>({cudf::column_metadata{"d"}});

auto arrow_schema = cudf::to_arrow_schema(cudf::table_view({empty->view()}), meta);
ASSERT_EQ(arrow_schema->n_children, 1);
auto dictionary = arrow_schema->children[0]->dictionary;
ASSERT_NE(dictionary, nullptr);
EXPECT_EQ(dictionary->n_children, 0);
}

TEST_F(ToArrowDeviceTest, DateTimeTable)
{
auto data = {1, 2, 3, 4, 5, 6};
Expand Down
Loading