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
@@ -0,0 +1,5 @@
# Nereids expressions — Review Guide

## checkLegalityBeforeTypeCoercion

- [ ] For all overloaded checkLegalityBeforeTypeCoercion methods, ensure child nodes are obtained via getArguments() or getArgument() instead of children() or child().
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,30 @@ public boolean nullable() throws UnboundException {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (children().get(0).getDataType().isStructType()) {
List<Expression> arguments = getArguments();
if (arguments.get(0).getDataType().isStructType()) {
// we should check in value list is all struct type
for (int i = 1; i < children().size(); i++) {
if (!children().get(i).getDataType().isStructType() && !children().get(i).getDataType().isNullType()) {
for (int i = 1; i < arguments.size(); i++) {
if (!arguments.get(i).getDataType().isStructType() && !arguments.get(i).getDataType().isNullType()) {
throw new AnalysisException("in predicate struct should compare with struct type list, but got : "
+ children().get(i).getDataType().toSql());
+ arguments.get(i).getDataType().toSql());
}
}
return;
}

if (children().get(0).getDataType().isArrayType()) {
if (arguments.get(0).getDataType().isArrayType()) {
// we should check in value list is all list type
for (int i = 1; i < children().size(); i++) {
if (!children().get(i).getDataType().isArrayType() && !children().get(i).getDataType().isNullType()) {
for (int i = 1; i < arguments.size(); i++) {
if (!arguments.get(i).getDataType().isArrayType() && !arguments.get(i).getDataType().isNullType()) {
throw new AnalysisException("in predicate list should compare with struct type list, but got : "
+ children().get(i).getDataType().toSql());
+ arguments.get(i).getDataType().toSql());
}
}
return;
}

children().forEach(c -> {
arguments.forEach(c -> {
if (c.getDataType().isObjectType()) {
throw new AnalysisException("in predicate could not contains object type: " + this.toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public Expression withInferred(boolean inferred) {
@Override
public void checkLegalityBeforeTypeCoercion() {
if (arity() == 3) {
if (child(2) instanceof StringLikeLiteral) {
String escapeChar = ((StringLikeLiteral) child(2)).getStringValue();
Expression escapeArgument = getArgument(2);
if (escapeArgument instanceof StringLikeLiteral) {
String escapeChar = ((StringLikeLiteral) escapeArgument).getStringValue();
if (escapeChar.getBytes().length != 1) {
throw new AnalysisException(
"like escape character must be a single ascii character: " + escapeChar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.TreeNode;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Variable;
import org.apache.doris.nereids.types.DataType;

import com.google.common.collect.ImmutableList;
Expand All @@ -46,12 +47,42 @@ default void checkLegalityBeforeTypeCoercion() {}
@Developing
default void checkLegalityAfterRewrite() {}

/**
* getArguments.
*/
default List<Expression> getArguments() {
return children();
boolean hasVariableArg = false;
for (Expression arg : children()) {
if (arg instanceof Variable) {
hasVariableArg = true;
break;
}
}
if (hasVariableArg) {
ImmutableList.Builder<Expression> arguments = ImmutableList.builder();
for (Expression arg : children()) {
if (arg instanceof Variable) {
arguments.add(((Variable) arg).getRealExpression());
} else {
arguments.add(arg);
}
}
return arguments.build();
} else {
return children();
}
}

/**
* getArgument.
*/
default Expression getArgument(int index) {
return child(index);
Expression arg = child(index);
if (arg instanceof Variable) {
return ((Variable) arg).getRealExpression();
} else {
return arg;
}
}

default List<DataType> getArgumentsTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public BoolAnd withDistinctAndChildren(boolean distinct, List<Expression> childr

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (!(argType.isBooleanType() || argType.isNumericType())) {
throw new AnalysisException("bool_and requires a boolean or numeric argument");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BoolOr withDistinctAndChildren(boolean distinct, List<Expression> childre

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (!(argType.isBooleanType() || argType.isNumericType())) {
throw new AnalysisException("bool_or requires a boolean or numeric argument");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BoolXor withDistinctAndChildren(boolean distinct, List<Expression> childr

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (!(argType.isBooleanType() || argType.isNumericType())) {
throw new AnalysisException("bool_xor requires a boolean or numeric argument");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private Histogram(AggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!(child(0).getDataType() instanceof PrimitiveType)) {
if (!(getArgument(0).getDataType() instanceof PrimitiveType)) {
SearchSignature.throwCanNotFoundFunctionException(this.getName(), getArguments());
}
if (arity() == 2 && !getArgument(1).isConstant()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private LinearHistogram(AggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!(child(0).getDataType() instanceof PrimitiveType)) {
if (!(getArgument(0).getDataType() instanceof PrimitiveType)) {
SearchSignature.throwCanNotFoundFunctionException(this.getName(), getArguments());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Median(NullableAggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (((!argType.isNumericType() && !argType.isNullType()) || argType.isOnlyMetricType())) {
throw new AnalysisException("median requires a numeric parameter: " + toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private MultiDistinctSum(NullableAggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if ((!argType.isNumericType() && !argType.isBooleanType() && !argType.isNullType())
|| argType.isOnlyMetricType()) {
throw new AnalysisException("sum requires a numeric or boolean parameter: " + this.toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MultiDistinctSum0(AggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if ((!argType.isNumericType() && !argType.isBooleanType() && !argType.isNullType())
|| argType.isOnlyMetricType()) {
throw new AnalysisException("sum0 requires a numeric or boolean parameter: " + this.toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ private PercentileReservoir(NullableAggregateFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!getArgument(1).isConstant()) {
Expression levelArgument = getArgument(1);
if (!levelArgument.isConstant()) {
throw new AnalysisException(
"percentile_reservoir requires second parameter must be a constant : " + this.toSql());
}
if (child(1) instanceof Literal) {
double value = ((Literal) child(1)).getDouble();
if (levelArgument instanceof Literal) {
double value = ((Literal) levelArgument).getDouble();
if (value < 0 || value > 1) {
throw new AnalysisException(
"percentile_reservoir level must be in [0, 1], but got " + value + ": " + this.toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public MultiDistinctSum0 convertToMultiDistinct() {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (!argType.isNumericType() && !argType.isBooleanType()
&& !argType.isNullType() && !argType.isStringLikeType()) {
throw new AnalysisException("sum0 requires a numeric, boolean or string parameter: " + this.toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public ExplodeMap withChildren(List<Expression> children) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!(child().getDataType() instanceof MapType)) {
Expression argument = getArgument(0);
if (!(argument.getDataType() instanceof MapType)) {
throw new AnalysisException("only support map type for explode_map function but got "
+ child().getDataType());
+ argument.getDataType());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public ExplodeMapOuter withChildren(List<Expression> children) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!(child().getDataType() instanceof MapType)) {
Expression argument = getArgument(0);
if (!(argument.getDataType() instanceof MapType)) {
throw new AnalysisException("only support map type for explode_map function but got "
+ child().getDataType());
+ argument.getDataType());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void checkLegalityBeforeTypeCoercion() {
if (children.isEmpty()) {
return;
}
DataType firstChildType = children.get(0).getDataType();
DataType firstChildType = getArgument(0).getDataType();
if (firstChildType.isJsonType() || firstChildType.isVariantType()) {
throw new AnalysisException("array does not support jsonb/variant type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ private void checkArguments(Expression arg0, Expression arg1, Expression arg2) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (!child(0).getDataType().isArrayType()) {
Expression argument = getArgument(0);
if (!argument.getDataType().isArrayType()) {
throw new AnalysisException("array_apply does not support type "
+ child(0).getDataType() + ", expression is " + toSql());
+ argument.getDataType() + ", expression is " + toSql());
}
DataType argType = ((ArrayType) child(0).getDataType()).getItemType();
DataType argType = ((ArrayType) argument.getDataType()).getItemType();
if (!(argType.isIntegralType() || argType.isFloatLikeType() || argType.isDecimalLikeType()
|| argType.isDateLikeType() || argType.isBooleanType())) {
throw new AnalysisException("array_apply does not support type " + argType + ", expression is " + toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ private ArrayAvg(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
if (child(0).getDataType().isArrayType()
&& !((ArrayType) child(0).getDataType()).getItemType().canBeCalculatedInArray()) {
Expression argument = getArgument(0);
if (argument.getDataType().isArrayType()
&& !((ArrayType) argument.getDataType()).getItemType().canBeCalculatedInArray()) {
throw new AnalysisException("array_avg does not support type "
+ child(0).getDataType().toString() + ", expression is " + toSql());
+ argument.getDataType().toString() + ", expression is " + toSql());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private ArrayCompact(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = ((ArrayType) child(0).getDataType()).getItemType();
DataType argType = ((ArrayType) getArgument(0).getDataType()).getItemType();
if (argType.isMapType() || argType.isStructType()) {
throw new AnalysisException("array_compact does not support type "
+ argType.toString() + ", expression is " + toSql());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ private ArrayCumSum(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
if (child(0).getDataType().isArrayType()
&& !((ArrayType) child(0).getDataType()).getItemType().canBeCalculatedInArray()) {
Expression argument = getArgument(0);
if (argument.getDataType().isArrayType()
&& !((ArrayType) argument.getDataType()).getItemType().canBeCalculatedInArray()) {
throw new AnalysisException("array_cum_sum does not support type "
+ child(0).getDataType().toString()
+ argument.getDataType().toString()
+ ", expression is " + toSql());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ private ArrayDifference(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
if (child(0).getDataType().isArrayType()
&& !((ArrayType) child(0).getDataType()).getItemType().canBeCalculatedInArray()) {
Expression argument = getArgument(0);
if (argument.getDataType().isArrayType()
&& !((ArrayType) argument.getDataType()).getItemType().canBeCalculatedInArray()) {
throw new AnalysisException("array_difference does not support type "
+ child(0).getDataType().toString() + ", expression is " + toSql());
+ argument.getDataType().toString() + ", expression is " + toSql());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private ArrayDistinct(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child(0).getDataType();
DataType argType = getArgument(0).getDataType();
if (argType.isArrayType()) {
DataType itemType = ((ArrayType) argType).getItemType();
if (itemType.isMapType() || itemType.isStructType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private ArrayEnumerateUniq(ScalarFunctionParams functionParams) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
for (Expression arg : children()) {
for (Expression arg : getArguments()) {
DataType argType = arg.getDataType();
if (argType.isArrayType()) {
DataType itemType = ((ArrayType) argType).getItemType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private ArrayIntersect(ScalarFunctionParams functionParams) {
@Override
public void checkLegalityBeforeTypeCoercion() {
DataType itemType = NullType.INSTANCE;
for (Expression child : children()) {
for (Expression child : getArguments()) {
DataType argType = child.getDataType();
// nullsafe
if (argType == NullType.INSTANCE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ArrayMax(ScalarFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (argType.isArrayType() && ((ArrayType) argType).getItemType().isComplexType()) {
throw new AnalysisException("array_max does not support complex types: " + toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ArrayMin(ScalarFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (argType.isArrayType() && ((ArrayType) argType).getItemType().isComplexType()) {
throw new AnalysisException("array_min does not support complex types: " + toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ArrayPosition withChildren(List<Expression> children) {
*/
@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child(0).getDataType();
DataType argType = getArgument(0).getDataType();
if (argType.isArrayType() && ((ArrayType) argType).getItemType().isComplexType()) {
throw new AnalysisException("array_position does not support complex types: " + toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private ArrayProduct(ScalarFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child().getDataType();
DataType argType = getArgument(0).getDataType();
if (argType instanceof ArrayType && ((ArrayType) argType).getItemType().isComplexType()) {
throw new AnalysisException(toSql() + " does not support type: "
+ ((ArrayType) argType).getItemType().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ArrayRemove withChildren(List<Expression> children) {

@Override
public void checkLegalityBeforeTypeCoercion() {
DataType argType = child(0).getDataType();
DataType argType = getArgument(0).getDataType();
if (argType.isArrayType() && (((ArrayType) argType).getItemType().isComplexType()
|| ((ArrayType) argType).getItemType().isVariantType()
|| ((ArrayType) argType).getItemType().isJsonType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private ArrayReverseSort(ScalarFunctionParams functionParams) {

@Override
public void checkLegalityBeforeTypeCoercion() {
if (children.get(0).getDataType() instanceof ArrayType) {
DataType argType = child(0).getDataType();
DataType argType = getArgument(0).getDataType();
if (argType instanceof ArrayType) {
// Find the innermost element type for nested arrays
DataType itemType = ((ArrayType) argType).getItemType();
while (itemType.isArrayType()) {
Expand Down
Loading
Loading