[Feature](json) Support json_search function in 2.1 (#41590)

cherry-pick #40948 

Like mysql, json_search returns the path which point to a json string
witch match the pattern.
`SELECT JSON_SEARCH('["A",[{"B":"1"}],{"C":"AB"},{"D":"BC"}]', 'one',
'A_') as res;`
```
+----------+
| res      |
+----------+
| "$[2].C" |
+----------+
```

Co-authored-by: liutang123 <liulijia@gmail.com>
This commit is contained in:
Lijia Liu
2024-10-11 16:33:07 +08:00
committed by GitHub
parent e9cfbb56b3
commit 4ac07fe918
11 changed files with 827 additions and 106 deletions

View File

@ -227,6 +227,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonObject;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonQuote;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonReplace;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonSearch;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonSet;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonUnQuote;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbExistsPath;
@ -708,6 +709,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(JsonbParseNullableErrorToNull.class, "jsonb_parse_nullable_error_to_null"),
scalar(JsonbParseNullableErrorToValue.class, "json_parse_nullable_error_to_value"),
scalar(JsonbParseNullableErrorToValue.class, "jsonb_parse_nullable_error_to_value"),
scalar(JsonSearch.class, "json_search"),
scalar(JsonbValid.class, "json_valid"),
scalar(JsonbValid.class, "jsonb_valid"),
scalar(JsonbType.class, "json_type"),

View File

@ -0,0 +1,62 @@
// 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.
package org.apache.doris.nereids.trees.expressions.functions.scalar;
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.JsonType;
import org.apache.doris.nereids.types.VarcharType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* JsonSearch returns the json path pointing to a json string witch contains the search string.
*/
public class JsonSearch extends ScalarFunction implements ExplicitlyCastableSignature, AlwaysNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(JsonType.INSTANCE)
.args(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT)
);
public JsonSearch(Expression arg0, Expression arg1, Expression arg2) {
super("json_search", arg0, arg1, arg2);
}
@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
@Override
public JsonSearch withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 3);
return new JsonSearch(children.get(0), children.get(1), children.get(2));
}
@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitJsonSearch(this, context);
}
}

View File

@ -230,6 +230,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonObject;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonQuote;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonReplace;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonSearch;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonSet;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonUnQuote;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbExistsPath;
@ -1305,6 +1306,10 @@ public interface ScalarFunctionVisitor<R, C> {
return visitScalarFunction(jsonKeys, context);
}
default R visitJsonSearch(JsonSearch jsonSearch, C context) {
return visitScalarFunction(jsonSearch, context);
}
default R visitJsonInsert(JsonInsert jsonInsert, C context) {
return visitScalarFunction(jsonInsert, context);
}