Skip to content

Commit

Permalink
Fixes sliced list and struct column bug in JSON chunked writer (#13108)
Browse files Browse the repository at this point in the history
Fixes the OOM access error while using chunked JSON writer on list columns.
The issue is present in struct columns also, which is fixed in this change.
Fixes #13030

Authors:
  - Karthikeyan (https://github.com/karthikeyann)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Nghia Truong (https://github.com/ttnghia)
  - David Wendt (https://github.com/davidwendt)

URL: #13108
  • Loading branch information
karthikeyann authored Apr 11, 2023
1 parent e9e86f4 commit 5638d44
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
26 changes: 16 additions & 10 deletions cpp/src/io/json/write_json.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <io/csv/durations.hpp>
#include <lists/utilities.hpp>

#include <cudf/column/column_device_view.cuh>
#include <cudf/column/column_factories.hpp>
Expand Down Expand Up @@ -437,15 +438,16 @@ struct column_to_strings_fn {
};
auto child_string = cudf::strings::detail::replace_nulls(
child_string_with_null()->view(), narep, stream_, rmm::mr::get_current_device_resource());
auto const list_child_string =
column_view(column.type(),
column.size(),
column.head(),
column.null_mask(),
column.null_count(),
column.offset(),
{lists_column_view(column).offsets(), child_string->view()});
return strings::detail::join_list_elements(lists_column_view(list_child_string),
auto new_offsets = cudf::lists::detail::get_normalized_offsets(
lists_column_view(column), stream_, rmm::mr::get_current_device_resource());
auto const list_child_string = make_lists_column(
column.size(),
std::move(new_offsets),
std::move(child_string),
column.null_count(),
cudf::detail::copy_bitmask(column, stream_, rmm::mr::get_current_device_resource()),
stream_);
return strings::detail::join_list_elements(lists_column_view(*list_child_string),
string_scalar{","},
narep,
strings::separator_on_nulls::YES,
Expand All @@ -470,7 +472,11 @@ struct column_to_strings_fn {
std::enable_if_t<std::is_same_v<column_type, cudf::struct_view>, std::unique_ptr<column>>
operator()(column_view const& column, host_span<column_name_info const> children_names) const
{
auto col_string = operator()(column.child_begin(), column.child_end(), children_names);
auto const child_it = cudf::detail::make_counting_transform_iterator(
0, [structs_view = structs_column_view{column}](auto const child_idx) {
return structs_view.get_sliced_child(child_idx);
});
auto col_string = operator()(child_it, child_it + column.num_children(), children_names);
col_string->set_null_mask(cudf::detail::copy_bitmask(column, stream_, mr_),
column.null_count());
return col_string;
Expand Down
45 changes: 45 additions & 0 deletions cpp/tests/io/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,49 @@ TEST_F(JsonWriterTest, NullList)
EXPECT_EQ(expected, std::string(out_buffer.data(), out_buffer.size()));
}

TEST_F(JsonWriterTest, ChunkedNested)
{
std::string const data = R"(
{"a": 1, "b": -2, "c": { }, "e": [{"f": 1}]}
{"a": 2, "b": -2, "c": { }, "e": null}
{"a": 3, "b": -2, "c": {"d": 9}, "e": [{"f": 2}, null]}
{"a": 4, "b": -2, "c": {"d": 16}, "e": [{"f": 3}, {"f": 4}, {"f": 5}]}
{"a": 5, "b": -2, "c": { }, "e": []}
{"a": 6, "b": -2, "c": {"d": 36}, "e": [{"f": 6}]}
{"a": 7, "b": -2, "c": {"d": 49}, "e": [{"f": 7}]}
{"a": 8, "b": -2, "c": {"d": 64}, "e": [{"f": 8}]}
{"a": 9, "b": -2, "c": {"d": 81}, "e": [{"f": 9}]}
)";
cudf::io::json_reader_options in_options =
cudf::io::json_reader_options::builder(cudf::io::source_info{data.data(), data.size()})
.lines(true);

cudf::io::table_with_metadata result = cudf::io::read_json(in_options);
cudf::table_view tbl_view = result.tbl->view();
cudf::io::table_metadata mt{result.metadata};

std::vector<char> out_buffer;
auto destination = cudf::io::sink_info(&out_buffer);
auto options_builder = cudf::io::json_writer_options_builder(destination, tbl_view)
.include_nulls(false)
.metadata(mt)
.lines(true)
.na_rep("null")
.rows_per_chunk(8);

cudf::io::write_json(options_builder.build(), rmm::mr::get_current_device_resource());
std::string const expected =
R"({"a":1,"b":-2,"c":{},"e":[{"f":1}]}
{"a":2,"b":-2,"c":{}}
{"a":3,"b":-2,"c":{"d":9},"e":[{"f":2},null]}
{"a":4,"b":-2,"c":{"d":16},"e":[{"f":3},{"f":4},{"f":5}]}
{"a":5,"b":-2,"c":{},"e":[]}
{"a":6,"b":-2,"c":{"d":36},"e":[{"f":6}]}
{"a":7,"b":-2,"c":{"d":49},"e":[{"f":7}]}
{"a":8,"b":-2,"c":{"d":64},"e":[{"f":8}]}
{"a":9,"b":-2,"c":{"d":81},"e":[{"f":9}]}
)";
EXPECT_EQ(expected, std::string(out_buffer.data(), out_buffer.size()));
}

CUDF_TEST_PROGRAM_MAIN()

0 comments on commit 5638d44

Please sign in to comment.