mrhhsg commented on code in PR #55575:
URL: https://github.com/apache/doris/pull/55575#discussion_r2315149364
##########
fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java:
##########
@@ -762,6 +763,7 @@ public class BuiltinScalarFunctions implements
FunctionHelper {
scalar(JsonInsert.class, "json_insert", "jsonb_insert"),
scalar(JsonReplace.class, "json_replace", "jsonb_replace"),
scalar(JsonSet.class, "json_set", "jsonb_set"),
+ scalar(JsonRemove.class, "json_remove", "jsonb_remove"),
Review Comment:
The newly added function does not need alias 'jsonb_xxx'.
##########
regression-test/suites/query_p0/sql_functions/json_functions/test_json_function.groovy:
##########
@@ -165,4 +165,44 @@ suite("test_json_function", "arrow_flight_sql") {
exception "Json path error: Invalid Json Path for value: \$**"
}
+
+ qt_json_remove1 """
+ SELECT JSON_REMOVE('{"a": 1, "b": 2, "c": 3}', '\$.b') AS 'Result';
+ """
+
+ qt_json_remove2 """
+ SELECT JSON_REMOVE('{"Name": "Homer", "Gender": "Male", "Age": 39}',
'\$.Age') AS 'Result';
+ """
+
+ qt_json_remove3 """
+ SELECT JSON_REMOVE('{"Name": "Homer", "Age": 39}', '\$.Gender') AS
'Result';
+ """
+
+ qt_json_remove4 """
+ SELECT JSON_REMOVE('[1, 2, 3]', '\$[0]') AS 'Result';
+ """
+
+ qt_json_remove5 """
+ SELECT JSON_REMOVE('[1, 2, [3, 4, 5]]', '\$[2][1]') AS 'Result';
+ """
+
+ qt_json_remove6 """
+ SELECT JSON_REMOVE('[1, 2, 3, 4, 5]', '\$[1]', '\$[3]') AS 'Result';
+ """
+
+ qt_json_remove7 """
+ SELECT JSON_REMOVE('[1, 2, 3, 4, 5]', '\$[3]') AS 'One Path',
JSON_REMOVE('[1, 2, 3, 4, 5]', '\$[1]', '\$[3]') AS 'Two Paths';
+ """
+
+ qt_json_remove8 """
+ SELECT JSON_REMOVE('[1, 2, [3, 4, 5]]', '\$[0]', '\$[1][1]') AS 'Result';
+ """
+
+ qt_json_remove9 """
+ SELECT JSON_REMOVE('[1, 2, [3, 4, 5]]', '\$[2][1]', '\$[0]') AS 'Result';
+ """
+
+ qt_json_remove10 """
+ SELECT JSON_REMOVE('{"Person": {"Name": "Homer","Age": 39,"Hobbies":
["Eating", "Sleeping", "Base Jumping"]}}', '\$.Person.Age',
'\$.Person.Hobbies[2]') AS 'Result';
Review Comment:
Add cases:
1. About NULL arguments.
2. About selecting rows from a real table.
3. About returning error.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/JsonFunctionRewrite.java:
##########
@@ -141,6 +142,25 @@ private static <T extends ScalarFunction> Expression
rewriteJsonModifyArguments(
});
}
+ private static <T extends ScalarFunction> Expression
rewriteJsonRemoveArguments(T function) {
Review Comment:
No need to rewrite the first argument with `to_json`.
##########
be/src/vec/functions/function_jsonb.cpp:
##########
@@ -1991,95 +2034,141 @@ class FunctionJsonbModify : public IFunction {
DorisVector<DorisVector<JsonbValue*>> json_values(keys_count);
DorisVector<JsonbWriter> writer_holders(input_rows_count);
- RETURN_IF_ERROR(parse_paths_and_values(json_paths, json_values,
arguments, input_rows_count,
- json_path_columns,
json_path_constant,
- json_path_null_maps,
json_value_columns,
- json_value_constant,
json_value_null_maps));
-
- for (size_t i = 1; i < arguments.size(); i += 2) {
- const size_t index = i / 2;
- auto& json_path = json_paths[index];
- auto& json_value = json_values[index];
+ if constexpr (modify_type == JsonbModifyType::Remove) {
+ // For JSON_REMOVE, only parse paths
+ RETURN_IF_ERROR(parse_paths_only(json_paths, arguments,
input_rows_count,
+ json_path_columns,
json_path_constant,
+ json_path_null_maps));
+ } else {
+ RETURN_IF_ERROR(parse_paths_and_values(
+ json_paths, json_values, arguments, input_rows_count,
json_path_columns,
+ json_path_constant, json_path_null_maps,
json_value_columns,
+ json_value_constant, json_value_null_maps));
+ }
+ if constexpr (modify_type == JsonbModifyType::Remove) {
Review Comment:
It seems that implementing `json_remove` as a standalone function might be
more reasonable.
json_remove 和 json_replace/insert/set 的逻辑几乎是完全分离的,建议弄成单独的函数。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]