-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support casting of Map type to string in JSON reader (#14936)
Addresses part of #14288 Depends on #14939 (mixed type ignore nulls fix) In the input schema, if a struct column is given as STRING type, it's forced to be a STRING column. This could be used to support map type in spark JSON reader. (Force a map type to be a STRING, and use different parser to extract this string column as key, value columns) To enable this forcing, mixed type as string should be enabled in json_reader_options. Authors: - Karthikeyan (https://github.com/karthikeyann) - Nghia Truong (https://github.com/ttnghia) Approvers: - Andy Grove (https://github.com/andygrove) - Mike Wilson (https://github.com/hyperbolic2346) - Shruti Shivakumar (https://github.com/shrshi) - Bradley Dice (https://github.com/bdice) URL: #14936
- Loading branch information
1 parent
b08dd9b
commit 6a03827
Showing
6 changed files
with
235 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "nested_json.hpp" | ||
|
||
#include <cudf/detail/utilities/visitor_overload.hpp> | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace cudf::io::json::detail { | ||
|
||
std::optional<schema_element> child_schema_element(std::string const& col_name, | ||
cudf::io::json_reader_options const& options) | ||
{ | ||
return std::visit( | ||
cudf::detail::visitor_overload{ | ||
[col_name](std::vector<data_type> const& user_dtypes) -> std::optional<schema_element> { | ||
auto column_index = atol(col_name.data()); | ||
return (static_cast<std::size_t>(column_index) < user_dtypes.size()) | ||
? std::optional<schema_element>{{user_dtypes[column_index]}} | ||
: std::optional<schema_element>{}; | ||
}, | ||
[col_name]( | ||
std::map<std::string, data_type> const& user_dtypes) -> std::optional<schema_element> { | ||
return (user_dtypes.find(col_name) != std::end(user_dtypes)) | ||
? std::optional<schema_element>{{user_dtypes.find(col_name)->second}} | ||
: std::optional<schema_element>{}; | ||
}, | ||
[col_name]( | ||
std::map<std::string, schema_element> const& user_dtypes) -> std::optional<schema_element> { | ||
return (user_dtypes.find(col_name) != std::end(user_dtypes)) | ||
? user_dtypes.find(col_name)->second | ||
: std::optional<schema_element>{}; | ||
}}, | ||
options.get_dtypes()); | ||
} | ||
|
||
// example schema and its path. | ||
// "a": int {"a", int} | ||
// "a": [ int ] {"a", list}, {"element", int} | ||
// "a": { "b": int} {"a", struct}, {"b", int} | ||
// "a": [ {"b": int }] {"a", list}, {"element", struct}, {"b", int} | ||
// "a": [ null] {"a", list}, {"element", str} | ||
// back() is root. | ||
// front() is leaf. | ||
std::optional<data_type> get_path_data_type( | ||
host_span<std::pair<std::string, cudf::io::json::NodeT>> path, schema_element const& root) | ||
{ | ||
if (path.empty() || path.size() == 1) { | ||
return root.type; | ||
} else { | ||
if (path.back().second == NC_STRUCT && root.type.id() == type_id::STRUCT) { | ||
auto const child_name = path.first(path.size() - 1).back().first; | ||
auto const child_schema_it = root.child_types.find(child_name); | ||
return (child_schema_it != std::end(root.child_types)) | ||
? get_path_data_type(path.first(path.size() - 1), child_schema_it->second) | ||
: std::optional<data_type>{}; | ||
} else if (path.back().second == NC_LIST && root.type.id() == type_id::LIST) { | ||
auto const child_schema_it = root.child_types.find(list_child_name); | ||
return (child_schema_it != std::end(root.child_types)) | ||
? get_path_data_type(path.first(path.size() - 1), child_schema_it->second) | ||
: std::optional<data_type>{}; | ||
} | ||
return std::optional<data_type>{}; | ||
} | ||
} | ||
|
||
std::optional<data_type> get_path_data_type( | ||
host_span<std::pair<std::string, cudf::io::json::NodeT>> path, | ||
cudf::io::json_reader_options const& options) | ||
{ | ||
if (path.empty()) return {}; | ||
std::optional<schema_element> col_schema = child_schema_element(path.back().first, options); | ||
// check if it has value, then do recursive call and return. | ||
if (col_schema.has_value()) { | ||
return get_path_data_type(path, col_schema.value()); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
// idea: write a memoizer using template and lambda?, then call recursively. | ||
std::vector<path_from_tree::path_rep> path_from_tree::get_path(NodeIndexT this_col_id) | ||
{ | ||
std::vector<path_rep> path; | ||
// TODO Need to stop at row root. so, how to find row root? | ||
while (this_col_id != parent_node_sentinel) { | ||
auto type = column_categories[this_col_id]; | ||
std::string name = ""; | ||
// TODO make this ifelse into a separate lambda function, along with parent_col_id. | ||
auto parent_col_id = column_parent_ids[this_col_id]; | ||
if (parent_col_id == parent_node_sentinel || column_categories[parent_col_id] == NC_LIST) { | ||
if (is_array_of_arrays && parent_col_id == row_array_parent_col_id) { | ||
name = column_names[this_col_id]; | ||
} else { | ||
name = list_child_name; | ||
} | ||
} else if (column_categories[parent_col_id] == NC_FN) { | ||
auto field_name_col_id = parent_col_id; | ||
parent_col_id = column_parent_ids[parent_col_id]; | ||
name = column_names[field_name_col_id]; | ||
} | ||
// "name": type/schema | ||
path.emplace_back(name, type); | ||
this_col_id = parent_col_id; | ||
if (this_col_id == row_array_parent_col_id) return path; | ||
} | ||
return {}; | ||
} | ||
|
||
} // namespace cudf::io::json::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters