Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhli1142015 committed Dec 18, 2024
1 parent d5d801b commit e3e80be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
18 changes: 8 additions & 10 deletions velox/docs/functions/spark/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ JSON Functions

.. spark:function:: from_json(jsonString) -> [json object]
Casting a JSON text to the function's output type returns the value
represented by the JSON text if it matches the output type; otherwise, NULL
is returned.
The function supports ARRAY, MAP, and ROW as output types. For primitive
values, supported types include BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT,
REAL, DOUBLE or VARCHAR. Casting to ARRAY and MAP is supported when the
element type of the array or the value type of the map is one of these
supported types. For maps, the key type must be VARCHAR. When casting to
ROW, only JSON objects are supported, and the keys in the JSON object must
match the field names of the ROW exactly (case-sensitive).
Casts a JSON string to an ARRAY, MAP, or ROW type, with the output type
determined by the expression. Returns NULL, if the input string is unparsable.
Supported element types include BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT,
REAL, DOUBLE, VARCHAR, ARRAY, MAP, and ROW. When casting to ARRAY or MAP,
the element type of the array or the value type of the map must be one of
these supported types, and for maps, the key type must be VARCHAR. Casting
to ROW supports only JSON objects, where the keys must exactly match the ROW
field names (case sensitivity).
Behaviors of the casts are shown with the examples below. ::

SELECT from_json('{"a": true}'); -- {'a'=true} // Output type: ROW({"a"}, {BOOLEAN()})
Expand Down
10 changes: 5 additions & 5 deletions velox/functions/sparksql/specialforms/FromJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace facebook::velox::exec;
namespace facebook::velox::functions::sparksql {
namespace {

/// Struct for extracting JSON data and writing it with type-specific handling.
// Struct for extracting JSON data and writing it with type-specific handling.
template <typename Input>
struct ExtractJsonTypeImpl {
template <TypeKind kind>
Expand Down Expand Up @@ -78,9 +78,9 @@ struct ExtractJsonTypeImpl {
static simdjson::error_code
apply(Input value, exec::GenericWriter& writer, bool /*isRoot*/) {
SIMDJSON_ASSIGN_OR_RAISE(auto type, value.type());
auto& w = writer.castTo<bool>();
switch (type) {
case simdjson::ondemand::json_type::boolean: {
auto& w = writer.castTo<bool>();
SIMDJSON_ASSIGN_OR_RAISE(w, value.get_bool());
break;
}
Expand Down Expand Up @@ -296,7 +296,7 @@ struct ExtractJsonTypeImpl {
return simdjson::SUCCESS;
}

// Casts a JSON value to a float point, handling both numeric Special cases
// Casts a JSON value to a float point, handling both numeric special cases
// for NaN and Infinity.
template <typename T>
static simdjson::error_code castJsonToFloatingPoint(
Expand Down Expand Up @@ -352,8 +352,8 @@ struct ExtractJsonTypeImpl {
/// - Boolean: Only `true` and `false` are valid; others return `NULL`.
/// - Integral Types: Accepts only integers; floats or strings return `NULL`.
/// - Float/Double: All numbers are valid; strings like `"NaN"`, `"+INF"`,
/// `"+Infinity"`, `"Infinity"`, `"-INF"`,
/// `"-Infinity"` are accepted, others return `NULL`.
/// `"+Infinity"`, `"Infinity"`, `"-INF"`, `"-Infinity"` are accepted, others
/// return `NULL`.
/// - Array: Accepts JSON objects only if the array is the root type with ROW
/// child type.
/// - Map: Keys must be `VARCHAR` type.
Expand Down
22 changes: 12 additions & 10 deletions velox/functions/sparksql/tests/FromJsonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ TEST_F(FromJsonTest, basicArray) {
auto expected = makeArrayVector<int64_t>({{1}, {2}, {}});
auto input = makeFlatVector<std::string>({R"([1])", R"([2])", R"([])"});
testFromJson(input, expected);

auto rowVector = makeRowVector({"a"}, {makeFlatVector<int64_t>({1, 2, 2})});
std::vector<vector_size_t> offsets;
offsets.push_back(0);
offsets.push_back(1);
offsets.push_back(2);
auto arrayVector = makeArrayVector(offsets, rowVector);
input = makeFlatVector<std::string>(
{R"({"a": 1})", R"([{"a": 2}])", R"([{"a": 2}])"});
testFromJson(input, arrayVector);
}

TEST_F(FromJsonTest, basicMap) {
Expand Down Expand Up @@ -184,6 +174,18 @@ TEST_F(FromJsonTest, basicString) {
testFromJson(input, makeRowVector({"a"}, {expected}));
}

TEST_F(FromJsonTest, nestedComplexType) {
auto rowVector = makeRowVector({"a"}, {makeFlatVector<int64_t>({1, 2, 2})});
std::vector<vector_size_t> offsets;
offsets.push_back(0);
offsets.push_back(1);
offsets.push_back(2);
auto arrayVector = makeArrayVector(offsets, rowVector);
auto input = makeFlatVector<std::string>(
{R"({"a": 1})", R"([{"a": 2}])", R"([{"a": 2}])"});
testFromJson(input, arrayVector);
}

TEST_F(FromJsonTest, keyCaseSensitive) {
auto expected1 = makeNullableFlatVector<int64_t>({1, 2, 4});
auto expected2 = makeNullableFlatVector<int64_t>({3, 4, 5});
Expand Down

0 comments on commit e3e80be

Please sign in to comment.