Skip to content

Commit

Permalink
[Fix](Nereids) fix json and jsonb function in nereids
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Sep 12, 2023
1 parent 3b200e8 commit 2c9b26d
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbParseNullableErrorToNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbParseNullableErrorToValue;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbType;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbValid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.LastDay;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Least;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Left;
Expand Down Expand Up @@ -565,6 +566,8 @@ 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(JsonbValid.class, "json_valid"),
scalar(JsonbValid.class, "jsonb_valid"),
scalar(JsonbType.class, "json_type"),
scalar(JsonbType.class, "jsonb_type"),
scalar(LastDay.class, "last_day"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.JsonType;
import org.apache.doris.nereids.types.MapType;
import org.apache.doris.nereids.types.StructField;
import org.apache.doris.nereids.types.StructType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.doris.nereids.types.JsonType;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.nereids.util.ExpressionUtils;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand All @@ -39,24 +40,25 @@ public class JsonbExtract extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(JsonType.INSTANCE).args(JsonType.INSTANCE, VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(JsonType.INSTANCE).args(JsonType.INSTANCE, StringType.INSTANCE)
FunctionSignature.ret(JsonType.INSTANCE).varArgs(JsonType.INSTANCE, VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(JsonType.INSTANCE).varArgs(JsonType.INSTANCE, StringType.INSTANCE)
);

/**
* constructor with 2 arguments.
* constructor with 2 or more arguments.
*/
public JsonbExtract(Expression arg0, Expression arg1) {
super("jsonb_extract", arg0, arg1);
public JsonbExtract(Expression arg0, Expression arg1, Expression... varArgs) {
super("jsonb_extract", ExpressionUtils.mergeArguments(arg0, arg1, varArgs));
}

/**
* withChildren.
*/
@Override
public JsonbExtract withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new JsonbExtract(children.get(0), children.get(1));
Preconditions.checkArgument(children.size() >= 2);
return new JsonbExtract(children.get(0), children.get(1),
children.subList(2, children.size()).toArray(new Expression[0]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* ScalarFunction 'jsonb_valid'. This class is generated by GenerateFunction.
*/
public class JsonbValid extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IntegerType.INSTANCE).args(VarcharType.SYSTEM_DEFAULT)
);

/**
* constructor with 1 arguments.
*/
public JsonbValid(Expression arg0) {
super("json_valid", arg0);
}

/**
* withChildren.
*/
@Override
public JsonbValid withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new JsonbValid(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitJsonbValid(this, context);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbParseNullableErrorToNull;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbParseNullableErrorToValue;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbType;
import org.apache.doris.nereids.trees.expressions.functions.scalar.JsonbValid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.LastDay;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Least;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Left;
Expand Down Expand Up @@ -1072,6 +1073,10 @@ default R visitJsonbParseNullableErrorToValue(JsonbParseNullableErrorToValue fun
return visitScalarFunction(function, context);
}

default R visitJsonbValid(JsonbValid jsonbValid, C context) {
return visitScalarFunction(jsonbValid, context);
}

default R visitJsonbType(JsonbType jsonbType, C context) {
return visitScalarFunction(jsonbType, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testUnsupportedTypeThrowException() {
null,
null,
null,
AnalysisException.class,
null,
AnalysisException.class
};
for (int i = 0; i < 5; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@ suite("nereids_insert_unsupport_type") {
sql 'set enable_nereids_dml=true'
sql 'set enable_strict_consistency_dml=true'

test {
sql 'insert into json_t select id, kjson from src'
exception 'unsupported for Nereids'
}

sql 'set enable_fallback_to_original_planner=true'

sql 'insert into map_t select id, kmintint from src'
sql 'sync'
sql 'select * from map_t'
}
}

0 comments on commit 2c9b26d

Please sign in to comment.