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 missing trailing comma in json writer #12688

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 11 additions & 12 deletions cpp/src/io/json/write_json.cu
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,13 @@ std::unique_ptr<column> make_column_names_column(host_span<column_name_info cons

void write_chunked(data_sink* out_sink,
strings_column_view const& str_column_view,
std::string const& line_terminator,
string_scalar const& d_line_terminator,
json_writer_options const& options,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(str_column_view.size() > 0, "Unexpected empty strings column.");

string_scalar d_line_terminator{line_terminator};
Copy link
Contributor

Choose a reason for hiding this comment

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

So this was unused before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was used before too. Instead of passing std::string as argument, now string_scalar is passed.

auto p_str_col_w_nl = cudf::strings::detail::join_strings(str_column_view,
d_line_terminator,
string_scalar("", false),
Expand All @@ -609,15 +608,6 @@ void write_chunked(data_sink* out_sink,

out_sink->host_write(h_bytes.data(), total_num_bytes);
}

// Needs newline at the end, to separate from next chunk
if (options.is_enabled_lines()) {
if (out_sink->is_device_write_preferred(d_line_terminator.size())) {
out_sink->device_write(d_line_terminator.data(), d_line_terminator.size(), stream);
} else {
out_sink->host_write(line_terminator.data(), line_terminator.size());
}
}
}

void write_json(data_sink* out_sink,
Expand Down Expand Up @@ -697,7 +687,16 @@ void write_json(data_sink* out_sink,
// struct converter for the table
auto str_concat_col = converter(sub_view.begin(), sub_view.end(), user_column_names);

write_chunked(out_sink, str_concat_col->view(), line_terminator, options, stream, mr);
write_chunked(out_sink, str_concat_col->view(), d_line_terminator, options, stream, mr);

// Needs line_terminator at the end, to separate from next chunk
if (&sub_view != &vector_views.back() or options.is_enabled_lines()) {
if (out_sink->is_device_write_preferred(d_line_terminator.size())) {
out_sink->device_write(d_line_terminator.data(), d_line_terminator.size(), stream);
} else {
out_sink->host_write(line_terminator.data(), line_terminator.size());
}
}
}
} else {
if (options.is_enabled_lines()) {
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/json.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def write_json(
bool include_nulls=True,
bool lines=False,
bool index=False,
int rows_per_chunk=8,
int rows_per_chunk=1024*256, # 256K rows
ttnghia marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Cython function to call into libcudf API, see `write_json`.
Expand Down
15 changes: 12 additions & 3 deletions python/cudf/cudf/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,23 @@ def test_json_writer(tmpdir, pdf, gdf):
assert_eq(pdf_string, gdf_string)


def test_cudf_json_writer(pdf):
@pytest.mark.parametrize(
"lines", [True, False], ids=["lines=True", "lines=False"]
)
def test_cudf_json_writer(pdf, lines):
# removing datetime column because pandas doesn't support it
for col_name in pdf.columns:
if "datetime" in col_name:
pdf.drop(col_name, axis=1, inplace=True)
gdf = cudf.DataFrame.from_pandas(pdf)
pdf_string = pdf.to_json(orient="records", lines=True)
gdf_string = gdf.to_json(orient="records", lines=True, engine="cudf")
pdf_string = pdf.to_json(orient="records", lines=lines)
gdf_string = gdf.to_json(orient="records", lines=lines, engine="cudf")

assert_eq(pdf_string, gdf_string)

gdf_string = gdf.to_json(
orient="records", lines=lines, engine="cudf", rows_per_chunk=8
)

assert_eq(pdf_string, gdf_string)

Expand Down