Skip to content

Commit

Permalink
feat: Add from_json Spark function
Browse files Browse the repository at this point in the history
address comments

address comments

address comments

address comments

address comments

minor change

address comments

minor change
  • Loading branch information
zhli1142015 committed Dec 25, 2024
1 parent b0a8908 commit d47f11b
Show file tree
Hide file tree
Showing 7 changed files with 879 additions and 1 deletion.
18 changes: 18 additions & 0 deletions velox/docs/functions/spark/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,21 @@ 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]
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()})
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

0 comments on commit d47f11b

Please sign in to comment.