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 List's missing children metadata in JSON writer #13869

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
9 changes: 5 additions & 4 deletions python/cudf/cudf/_lib/json.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ cdef _set_col_children_metadata(Column col,
child_col, col_meta.children[i]
)
elif is_list_dtype(col):
_set_col_children_metadata(
col.children[0],
col_meta.children[0]
)
for i, child_col in enumerate(col.children):
col_meta.children.push_back(child_info)
_set_col_children_metadata(
child_col, col_meta.children[i]
)
else:
return
65 changes: 64 additions & 1 deletion python/cudf/cudf/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,69 @@ def test_cudf_json_writer_read(gdf_writer_types):
assert_eq(pdf2, gdf2)


@pytest.mark.parametrize(
"jsonl_string, expected",
[
# fixed width
("""{"a":10, "b":1.1}\n {"a":20, "b":2.1}\n""", None),
# simple list
("""{"a":[1, 2, 3], "b":1.1}\n {"a":[]}\n""", None),
# simple struct
("""{"a":{"c": 123 }, "b":1.1}\n {"a": {"c": 456}}\n""", None),
# list of lists
("""{"a":[[], [1, 2], [3, 4]], "b":1.1}\n""", None),
("""{"a":[null, [1, 2], [null, 4]], "b":1.1}\n""", None),
# list of structs
# error ("""{"a":[null, {}], "b":1.1}\n""", None),
(
"""{"a":[null, {"L": 123}], "b":1.0}\n {"b":1.1}\n {"b":2.1}\n""",
None,
),
(
"""{"a":[{"L": 123}, null], "b":1.0}\n {"b":1.1}\n {"b":2.1}\n""",
None,
),
# struct of lists
(
"""{"a":{"L": [1, 2, 3]}, "b":1.1}\n {"a": {"L": [4, 5, 6]}}\n""",
None,
),
("""{"a":{"L": [1, 2, null]}, "b":1.1}\n {"a": {"L": []}}\n""", None),
# struct of structs
(
"""{"a":{"L": {"M": 123}}, "b":1.1}
{"a": {"L": {"M": 456}}}\n""",
None,
),
(
"""{"a":{"L": {"M": null}}, "b":1.1}\n {"a": {"L": {}}}\n""",
"""{"a":{"L": {}}, "b":1.1}\n {"a": {"L": {}}}\n""",
),
# list of structs of lists
("""{"a":[{"L": [1, 2, 3]}, {"L": [4, 5, 6]}], "b":1.1}\n""", None),
("""{"a":[{"L": [1, 2, null]}, {"L": []}], "b":1.1}\n""", None),
# struct of lists of structs
("""{"a":{"L": [{"M": 123}, {"M": 456}]}, "b":1.1}\n""", None),
(
"""{"a":{"L": [{"M": null}, {}]}, "b":1.1}\n""",
"""{"a":{"L": [{}, {}]}, "b":1.1}\n""",
),
],
)
def test_cudf_json_roundtrip(jsonl_string, expected):
gdf = cudf.read_json(
StringIO(jsonl_string),
lines=True,
engine="cudf",
# dtype=dict(dtypes),
)
expected = jsonl_string if expected is None else expected
gdf_string = gdf.to_json(
orient="records", lines=True, engine="cudf", include_nulls=False
)
assert_eq(gdf_string, expected.replace(" ", ""))


@pytest.mark.parametrize("sink", ["string", "file"])
def test_cudf_json_writer_sinks(sink, tmp_path_factory):
df = cudf.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
Expand Down Expand Up @@ -1185,7 +1248,7 @@ def test_json_array_of_arrays(data, lines):
# simple list with mixed types
"""{"a":[123, {}], "b":1.1}""",
"""{"a":[123, {"0": 123}], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
"""{"a":[{"0": 123}, 123], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
"""{"a":[{"L": 123}, 123], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
"""{"a":[123, {"0": 123}, 12.3], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
"""{"a":[123, {"0": 123}, null], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
"""{"a":["123", {"0": 123}], "b":1.0}\n {"b":1.1}\n {"b":2.1}""",
Expand Down