From 55f947e49aa13b6420f8a2657b9acd25708cf318 Mon Sep 17 00:00:00 2001 From: DarvenDuan Date: Wed, 22 May 2024 15:24:10 +0800 Subject: [PATCH] support return JsonType for If function --- .../expressions/functions/scalar/If.java | 5 +- .../conditional_functions/test_if_json.out | 29 ++++++++ .../conditional_functions/test_if_json.groovy | 74 +++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/query_p0/sql_functions/conditional_functions/test_if_json.out create mode 100644 regression-test/suites/query_p0/sql_functions/conditional_functions/test_if_json.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java index 8f98e7641b16b7..e7ffcebdfe5005 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/If.java @@ -36,6 +36,7 @@ import org.apache.doris.nereids.types.FloatType; import org.apache.doris.nereids.types.HllType; import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.JsonType; import org.apache.doris.nereids.types.LargeIntType; import org.apache.doris.nereids.types.MapType; import org.apache.doris.nereids.types.NullType; @@ -101,7 +102,9 @@ public class If extends ScalarFunction FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT) .args(BooleanType.INSTANCE, VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT), FunctionSignature.ret(StringType.INSTANCE) - .args(BooleanType.INSTANCE, StringType.INSTANCE, StringType.INSTANCE) + .args(BooleanType.INSTANCE, StringType.INSTANCE, StringType.INSTANCE), + FunctionSignature.ret(JsonType.INSTANCE) + .args(BooleanType.INSTANCE, JsonType.INSTANCE, JsonType.INSTANCE) ); /** diff --git a/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_json.out b/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_json.out new file mode 100644 index 00000000000000..579c66a6fcdd54 --- /dev/null +++ b/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_json.out @@ -0,0 +1,29 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !if_json_parse -- +1 {"k":"k1","v":"v1"} {"k":"k1","v":"v1"} +2 {"k":"k2","v":"v3"} {"k":"k2","v":"v3"} +3 \N \N +4 {"k":"k4","v":"v4"} {"k":"k4","v":"v4"} +5 \N {"k":"k5","v":"v5"} + +-- !case_json_parse -- +1 {"k":"k1","v":"v1"} {"k":"k1","v":"v1"} +2 {"k":"k2","v":"v3"} {"k":"k2","v":"v3"} +3 \N \N +4 {"k":"k4","v":"v4"} {"k":"k4","v":"v4"} +5 \N {"k":"k5","v":"v5"} + +-- !case_json_parse_error_to_null -- +1 {"k":"k1","v":"v1"} {"k":"k1","v":"v1"} +2 {"k":"k2","v":"v3"} {"k":"k2","v":"v3"} +3 \N \N +4 {"k":"k4","v":"v4"} {"k":"k4","v":"v4"} +5 \N {"k":"k5","v":"v5"} + +-- !case_json_parse_error_to_value -- +1 {"k":"k1","v":"v1"} {"k":"k1","v":"v1"} +2 {"k":"k2","v":"v3"} {"k":"k2","v":"v3"} +3 \N \N +4 {"k":"k4","v":"v4"} {"k":"k4","v":"v4"} +5 \N {"k":"k5","v":"v5"} + diff --git a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_if_json.groovy b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_if_json.groovy new file mode 100644 index 00000000000000..973f193b461aac --- /dev/null +++ b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_if_json.groovy @@ -0,0 +1,74 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_if_json") { + def tblName = "test_if_json" + sql """DROP TABLE IF EXISTS ${tblName};""" + sql """ + CREATE TABLE ${tblName} ( + `id` INT NULL, + `v_key` CHAR NULL, + `v_json` JSON NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`, `v_key`) + COMMENT 'OLAP' + DISTRIBUTED BY HASH(`id`) BUCKETS 10 + PROPERTIES ( + "replication_num" = "1" + ) + """ + + sql """ insert into ${tblName} values (1, 'a', '{"k":"k1","v":"v1"}')""" + sql """ insert into ${tblName} values (2, 'a', '{"k":"k2","v":"v3"}')""" + sql """ insert into ${tblName} values (3, 'b', '{"k":"k3","v":"v3"}')""" + sql """ insert into ${tblName} values (4, 'a', '{"k":"k4","v":"v4"}')""" + sql """ insert into ${tblName} values (5, 'c', '{"k":"k5","v":"v5"}')""" + + qt_if_json_parse """ + SELECT id, + if(v_key = 'a', JSON_PARSE(v_json), NULL) AS a, + if(v_key = 'b', NULL, JSON_PARSE(v_json)) AS b + FROM ${tblName} + ORDER BY id + """ + + qt_case_json_parse """ + SELECT id, + CASE v_key WHEN 'a' THEN JSON_PARSE(v_json) ELSE NULL END AS a, + CASE v_key WHEN 'b' THEN NULL ELSE JSON_PARSE(v_json) END AS b + FROM ${tblName} + ORDER BY id + """ + + qt_case_json_parse_error_to_null """ + SELECT id, + CASE v_key WHEN 'a' THEN json_parse_error_to_null(v_json) ELSE NULL END AS a, + CASE v_key WHEN 'b' THEN NULL ELSE json_parse_error_to_null(v_json) END AS b + FROM ${tblName} + ORDER BY id + """ + + qt_case_json_parse_error_to_value """ + SELECT id, + CASE v_key WHEN 'a' THEN json_parse_error_to_value(v_json,json_object()) ELSE NULL END AS a, + CASE v_key WHEN 'b' THEN NULL ELSE json_parse_error_to_value(v_json,json_object()) END AS b + FROM ${tblName} + ORDER BY id + """ + + sql """DROP TABLE IF EXISTS ${tblName};""" +}