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

feat: Add Spark from_json function #11709

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions velox/docs/functions/spark/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,23 @@ JSON Functions
SELECT json_object_keys(''); -- NULL
SELECT json_object_keys(1); -- NULL
SELECT json_object_keys('"hello"'); -- NULL

.. spark:function:: from_json(jsonString) -> [json object]

zhli1142015 marked this conversation as resolved.
Show resolved Hide resolved
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).
Behaviors of the casts are shown with the examples below. ::

SELECT from_json('{"a": true}'); -- {'a'=true} // Output type: ROW({"a"}, {BOOLEAN()})
SELECT from_json('{"a": 1}'); -- {'a'=1} // Output type: ROW({"a"}, {INTEGER()})
SELECT from_json('{"a": 1.0}'); -- {'a'=1.0} // Output type: ROW({"a"}, {DOUBLE()})
SELECT from_json('["name", "age", "id"]'); -- ['name', 'age', 'id'] // Output type: ARRAY(VARCHAR())
SELECT from_json('{"a": 1, "b": 2}'); -- {'a'=1, 'b'=2} // Output type: MAP(VARCHAR(),INTEGER())
4 changes: 4 additions & 0 deletions velox/functions/sparksql/registration/RegisterSpecialForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "velox/expression/SpecialFormRegistry.h"
#include "velox/functions/sparksql/specialforms/AtLeastNNonNulls.h"
#include "velox/functions/sparksql/specialforms/DecimalRound.h"
#include "velox/functions/sparksql/specialforms/FromJson.h"
#include "velox/functions/sparksql/specialforms/MakeDecimal.h"
#include "velox/functions/sparksql/specialforms/SparkCastExpr.h"

Expand All @@ -44,6 +45,9 @@ void registerSpecialFormGeneralFunctions(const std::string& prefix) {
"cast", std::make_unique<SparkCastCallToSpecialForm>());
registerFunctionCallToSpecialForm(
"try_cast", std::make_unique<SparkTryCastCallToSpecialForm>());
exec::registerFunctionCallToSpecialForm(
FromJsonCallToSpecialForm::kFromJson,
std::make_unique<FromJsonCallToSpecialForm>());
}
} // namespace sparksql
} // namespace facebook::velox::functions
3 changes: 2 additions & 1 deletion velox/functions/sparksql/specialforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ velox_add_library(
velox_functions_spark_specialforms
AtLeastNNonNulls.cpp
DecimalRound.cpp
FromJson.cpp
MakeDecimal.cpp
SparkCastExpr.cpp
SparkCastHooks.cpp)

velox_link_libraries(velox_functions_spark_specialforms fmt::fmt
velox_expression)
velox_functions_json velox_expression)
Loading
Loading