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

Fix construction of nested structs with EMPTY child #10761

Merged
Merged
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
5 changes: 5 additions & 0 deletions cpp/src/structs/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ void superimpose_parent_nulls(bitmask_type const* parent_null_mask,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (child.type().id() == cudf::type_id::EMPTY) {
isVoid marked this conversation as resolved.
Show resolved Hide resolved
// EMPTY columns should not have a null mask,
// so don't superimpose null mask on empty columns.
return;
}
if (!child.nullable()) {
// Child currently has no null mask. Copy parent's null mask.
child.set_null_mask(cudf::detail::copy_bitmask(parent_null_mask, 0, child.size(), stream, mr));
Expand Down
19 changes: 18 additions & 1 deletion cpp/tests/structs/structs_column_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -623,4 +623,21 @@ TYPED_TEST(TypedStructColumnWrapperTest, CopyColumnFromView)
cudf::column(list_of_structs_column->view()));
}

TEST_F(StructColumnWrapperTest, TestStructsColumnWithEmptyChild)
{
// structs_column_views should not superimpose their null mask onto any EMPTY children,
// because EMPTY columns cannot have a null mask. This test ensures that
// we can construct a structs column with a parent null mask and an EMPTY
// child and then view it.
auto empty_col =
std::make_unique<cudf::column>(cudf::data_type(cudf::type_id::EMPTY), 3, rmm::device_buffer{});
shwina marked this conversation as resolved.
Show resolved Hide resolved
int num_rows{empty_col->size()};
vector_of_columns cols;
cols.push_back(std::move(empty_col));
auto mask_vec = std::vector<bool>{true, false, false};
auto mask = cudf::test::detail::make_null_mask(mask_vec.begin(), mask_vec.end());
auto structs_col = cudf::make_structs_column(num_rows, std::move(cols), 2, std::move(mask));
EXPECT_NO_THROW(structs_col->view());
}

CUDF_TEST_PROGRAM_MAIN()
4 changes: 4 additions & 0 deletions python/cudf/cudf/_lib/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ SUPPORTED_NUMPY_TO_LIBCUDF_TYPES = {
}

LIBCUDF_TO_SUPPORTED_NUMPY_TYPES = {
# There's no equivalent to EMPTY in cudf. We translate EMPTY
# columns from libcudf to ``int8`` columns of all nulls in Python.
# ``int8`` is chosen because it uses the least amount of memory.
TypeId.EMPTY: np.dtype("int8"),
TypeId.INT8: np.dtype("int8"),
TypeId.INT16: np.dtype("int16"),
TypeId.INT32: np.dtype("int32"),
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,13 @@ def test_struct_int_values():
assert isinstance(actual_series[0]["b"], int)
assert isinstance(actual_series[1]["b"], type(None))
assert isinstance(actual_series[2]["b"], int)


def test_nested_struct_from_pandas_empty():
# tests constructing nested structs columns that would result in
# libcudf EMPTY type child columns inheriting their parent's null
# mask. See GH PR: #10761
pdf = pd.Series([[{"c": {"x": None}}], [{"c": None}]])
shwina marked this conversation as resolved.
Show resolved Hide resolved
gdf = cudf.from_pandas(pdf)

assert_eq(pdf, gdf)