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 6 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
28 changes: 28 additions & 0 deletions velox/docs/functions/spark/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,31 @@ JSON Functions
SELECT json_object_keys(''); -- NULL
SELECT json_object_keys(1); -- NULL
SELECT json_object_keys('"hello"'); -- NULL

.. spark:function:: from_json(jsonString) -> array / map / row

zhli1142015 marked this conversation as resolved.
Show resolved Hide resolved
Casts ``jsonString`` to an ARRAY, MAP, or ROW type, with the output type
determined by the expression. Returns NULL, if the input string is unparsable.
zhli1142015 marked this conversation as resolved.
Show resolved Hide resolved
Supported element types include BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT,
REAL, DOUBLE, VARCHAR, ARRAY, MAP and ROW. When casting to ARRAY or MAP,
zhli1142015 marked this conversation as resolved.
Show resolved Hide resolved
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.
The current implementation has the following limitations.

* Does not support user provided options.

* Only supports partial result mode, which requires spark configuration spark.sql.json.enablePartialResults = true.

* Does not support single quotes as delimiters.

* Does not support schemas that include a corrupt record column.

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())
SELECT from_json('{"a": {"b": 1}}'); -- {'a'={b=1}} // Output type: ROW({"a"}, {ROW({"b"}, {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