rapidsai / cudf

cuDF - GPU DataFrame Library
https://docs.rapids.ai/api/cudf/stable/
Apache License 2.0
8.36k stars 891 forks source link

[BUG] `cudf::io::read_json` does not verify output column structures with the input schema #16799

Open ttnghia opened 1 month ago

ttnghia commented 1 month ago

Look at this example:

  std::string const json_string = R"({"data": [1, 2, 3], "data": [4, 5, 6]})";

  std::map<std::string, cudf::io::schema_element> dtype_schema;
  dtype_schema["data"] = cudf::io::schema_element{cudf::data_type{cudf::type_id::STRUCT}, {}};

  auto& child_schema = dtype_schema["data"].child_types;
  child_schema["a"]  = cudf::io::schema_element{cudf::data_type{cudf::type_id::INT64}, {}};
  child_schema["b"]  = cudf::io::schema_element{cudf::data_type{cudf::type_id::INT64}, {}};

  cudf::io::json_reader_options json_lines_options =
    cudf::io::json_reader_options::builder(
      cudf::io::source_info{json_string.c_str(), json_string.size()})
      .dtypes(dtype_schema)
      .lines(true);

  auto const table = cudf::io::read_json(json_lines_options);
  cudf::test::print(table.tbl->get_column(0).view());

Output:

cudf::list_view<int64_t>:
Length : 1
Offsets : 0, 3
   1, 2, 3, 4, 5, 6

Notice that the input schema in the example above is "data": STRUCT<"a": INT64, "b": INT64>. On the other hand, the data field in the input JSON is an array instead. We can observe that the data column after parsing (which is a lists column) is just gathered for generating the output. The desired output here should be all nulls instead, and the correct behavior here should be to make sure the output columns have the right types/structures that we are looking for as specified in the input schema.

karthikeyann commented 3 weeks ago

input type is only a hint unless prune_columns(true) Try prune_columns(true)