Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.ComputePrecision;
import org.apache.doris.nereids.trees.expressions.functions.CustomSignature;
import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
Expand All @@ -38,12 +39,9 @@
/**
* ScalarFunction 'named_struct'.
*/
public class CreateNamedStruct extends ScalarFunction
implements ExplicitlyCastableSignature, AlwaysNotNullable {
public class CreateNamedStruct extends ScalarFunction implements CustomSignature, ComputePrecision, AlwaysNotNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(StructType.SYSTEM_DEFAULT).args()
);
public static final FunctionSignature SIGNATURE = FunctionSignature.ret(StructType.SYSTEM_DEFAULT).args();

/**
* constructor with 0 or more arguments.
Expand Down Expand Up @@ -83,23 +81,29 @@ public CreateNamedStruct withChildren(List<Expression> children) {
}

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

@Override
public List<FunctionSignature> getSignatures() {
public FunctionSignature customSignature() {
if (arity() == 0) {
return SIGNATURES;
return SIGNATURE;
} else {
ImmutableList.Builder<StructField> structFields = ImmutableList.builder();
for (int i = 0; i < arity(); i = i + 2) {
StringLikeLiteral nameLiteral = (StringLikeLiteral) child(i);
structFields.add(new StructField(nameLiteral.getStringValue(),
children.get(i + 1).getDataType(), true, ""));
}
return ImmutableList.of(FunctionSignature.ret(new StructType(structFields.build()))
.args(children.stream().map(ExpressionTrait::getDataType).toArray(DataType[]::new)));
return FunctionSignature.ret(new StructType(structFields.build()))
.args(children.stream().map(ExpressionTrait::getDataType).toArray(DataType[]::new));
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ a \N abc
\N \N \N
NULL null \N

-- !sql_before --
{"col_11":123.321, "col_12":{"col":1, "col1":345.24}}

-- !sql_after --
{"col_11":123.321, "col_12":{"col":1, "col1":345.24}}
{"col_11":123.321, "col_12":{"col":1, "col1":12345.24}}

Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@
-- !sql --
10000000000

-- !sql --
123.321 {"col":1, "col1":12345.24}

-- !sql --
123.321 {"col":1, "col1":12345.24}

Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ suite("test_struct_functions") {
qt_select_struct_element_2 "SELECT struct_element(k3,'f1'),struct_element(k3,'f2'),struct_element(k3,'f3') FROM ${tableName} ORDER BY k1"
qt_select_struct_element_3 "SELECT struct_element(k4,1),struct_element(k4,2),struct_element(k4,3),struct_element(k4,4) FROM ${tableName} ORDER BY k1"
qt_select_struct_element_4 "SELECT struct_element(k5,1),struct_element(k5,2),struct_element(k5,3) FROM ${tableName} ORDER BY k1"

//The precision of the decimal type in the test select is inconsistent with the precision of the function named_struct containing the decimal type.
sql """ drop table if exists t01 --force """;
sql """ create table if not exists t01 (a decimal(6,3), d struct<col:bigint, col1:decimal(7,2)>) properties ("replication_num"="1");"""
sql """ insert into t01 values (123.321, named_struct('col', 1, 'col1', 345.24));"""
qt_sql_before """ select named_struct("col_11", a, "col_12", d) from t01; """
sql """ insert into t01 values (123.321, named_struct('col', 1, 'col1', 12345.24));"""
qt_sql_after """ select named_struct("col_11", a, "col_12", d) from t01; """
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ suite("test_struct_functions_by_literal") {

qt_sql "select struct_element(named_struct('f1', 1, 'f2', 2, 'f3', 3), 'f1')"
qt_sql "select struct_element(named_struct('f1', 1, 'f2', 1000, 'f3', 10000000000), 3)"

//The precision of the decimal type in the test select is inconsistent with the precision of the function named_struct containing the decimal type.
qt_sql "select cast(123.321 as decimal(6,3)), named_struct('col', 1, 'col1', 12345.24)"
qt_sql "select cast(123.321 as decimal(6,3)), named_struct('col', 1, 'col1', cast(12345.24 as decimal(7,2)))"
}
Loading