[fix](Nereids) adjust min/max stats for cast function if types are comparable (#28166)

estimate column stats for "cast(col, XXXType)"

-----cast-est------
query4 41169 40335 40267 40267
query58 463 361 401 361
Total cold run time: 41632 ms
Total hot run time: 40628 ms

----master------
query4 40624 40180 40299 40180
query58 487 389 420 389
Total cold run time: 41111 ms
Total hot run time: 40569 ms
This commit is contained in:
minghong
2024-01-12 20:38:13 +08:00
committed by yiguolei
parent bbfc3d037e
commit a69ce49b07
21 changed files with 323 additions and 136 deletions

View File

@ -42,7 +42,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects;
public class DecimalLiteral extends LiteralExpr {
public class DecimalLiteral extends NumericLiteralExpr {
private static final Logger LOG = LogManager.getLogger(DecimalLiteral.class);
private BigDecimal value;

View File

@ -35,7 +35,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.NumberFormat;
public class FloatLiteral extends LiteralExpr {
public class FloatLiteral extends NumericLiteralExpr {
private double value;
public FloatLiteral() {

View File

@ -37,7 +37,7 @@ import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class IntLiteral extends LiteralExpr {
public class IntLiteral extends NumericLiteralExpr {
private static final Logger LOG = LogManager.getLogger(IntLiteral.class);
public static final long TINY_INT_MIN = Byte.MIN_VALUE; // -2^7 ~ 2^7 - 1

View File

@ -38,7 +38,7 @@ import java.nio.ByteOrder;
import java.util.Objects;
// large int for the num that native types can not
public class LargeIntLiteral extends LiteralExpr {
public class LargeIntLiteral extends NumericLiteralExpr {
private static final Logger LOG = LogManager.getLogger(LargeIntLiteral.class);
// -2^127

View File

@ -0,0 +1,31 @@
// 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.
// This file is copied from
// https://github.com/apache/impala/blob/branch-2.9.0/fe/src/main/java/org/apache/impala/LiteralExpr.java
// and modified by Doris
package org.apache.doris.analysis;
public abstract class NumericLiteralExpr extends LiteralExpr {
public NumericLiteralExpr() {
super();
}
public NumericLiteralExpr(NumericLiteralExpr other) {
super(other);
}
}

View File

@ -18,6 +18,7 @@
package org.apache.doris.nereids.stats;
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.analysis.NumericLiteralExpr;
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Add;
@ -169,35 +170,50 @@ public class ExpressionEstimation extends ExpressionVisitor<ColumnStatistic, Sta
}
private ColumnStatistic castMinMax(ColumnStatistic colStats, DataType targetType) {
if (colStats.minExpr instanceof StringLiteral || colStats.maxExpr instanceof StringLiteral) {
if (targetType.isDateLikeType()) {
ColumnStatisticBuilder builder = new ColumnStatisticBuilder(colStats);
if (colStats.minExpr != null) {
try {
String strMin = colStats.minExpr.getStringValue();
DateLiteral dateMinLiteral = new DateLiteral(strMin);
long min = dateMinLiteral.getValue();
builder.setMinValue(min);
builder.setMinExpr(dateMinLiteral.toLegacyLiteral());
} catch (AnalysisException e) {
// ignore exception. do not convert min
}
// cast str to date/datetime
if (colStats.minExpr instanceof StringLiteral
&& colStats.maxExpr instanceof StringLiteral
&& targetType.isDateLikeType()) {
boolean convertSuccess = true;
ColumnStatisticBuilder builder = new ColumnStatisticBuilder(colStats);
if (colStats.minExpr != null) {
try {
String strMin = colStats.minExpr.getStringValue();
DateLiteral dateMinLiteral = new DateLiteral(strMin);
long min = dateMinLiteral.getValue();
builder.setMinValue(min);
builder.setMinExpr(dateMinLiteral.toLegacyLiteral());
} catch (AnalysisException e) {
convertSuccess = false;
}
if (colStats.maxExpr != null) {
try {
String strMax = colStats.maxExpr.getStringValue();
DateLiteral dateMaxLiteral = new DateLiteral(strMax);
long max = dateMaxLiteral.getValue();
builder.setMaxValue(max);
builder.setMaxExpr(dateMaxLiteral.toLegacyLiteral());
} catch (AnalysisException e) {
// ignore exception. do not convert max
}
}
if (convertSuccess && colStats.maxExpr != null) {
try {
String strMax = colStats.maxExpr.getStringValue();
DateLiteral dateMaxLiteral = new DateLiteral(strMax);
long max = dateMaxLiteral.getValue();
builder.setMaxValue(max);
builder.setMaxExpr(dateMaxLiteral.toLegacyLiteral());
} catch (AnalysisException e) {
convertSuccess = false;
}
}
if (convertSuccess) {
return builder.build();
}
}
return colStats;
// cast numeric to numeric
if (colStats.minExpr instanceof NumericLiteralExpr && colStats.maxExpr instanceof NumericLiteralExpr) {
if (targetType.isNumericType()) {
return colStats;
}
}
// cast other date types, set min/max infinity
ColumnStatisticBuilder builder = new ColumnStatisticBuilder(colStats);
builder.setMinExpr(null).setMinValue(Double.NEGATIVE_INFINITY)
.setMaxExpr(null).setMaxValue(Double.POSITIVE_INFINITY);
return builder.build();
}
@Override

View File

@ -31,7 +31,7 @@ import java.util.Objects;
/**
* decimal type literal
*/
public class DecimalLiteral extends Literal {
public class DecimalLiteral extends FractionalLiteral {
private final BigDecimal value;

View File

@ -31,7 +31,7 @@ import java.util.Objects;
/**
* Literal for DecimalV3 Type
*/
public class DecimalV3Literal extends Literal {
public class DecimalV3Literal extends FractionalLiteral {
private final BigDecimal value;

View File

@ -26,7 +26,7 @@ import org.apache.doris.nereids.types.DoubleType;
/**
* Double literal
*/
public class DoubleLiteral extends Literal {
public class DoubleLiteral extends FractionalLiteral {
private final double value;

View File

@ -25,7 +25,7 @@ import org.apache.doris.nereids.types.FloatType;
/**
* float type literal
*/
public class FloatLiteral extends Literal {
public class FloatLiteral extends FractionalLiteral {
private final float value;

View File

@ -0,0 +1,34 @@
// 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.literal;
import org.apache.doris.nereids.types.DataType;
/**
* float/double/decimal
*/
public abstract class FractionalLiteral extends NumericLiteral {
/**
* Constructor for FractionalLiteral.
*
* @param dataType logical data type in Nereids
*/
public FractionalLiteral(DataType dataType) {
super(dataType);
}
}

View File

@ -20,7 +20,7 @@ package org.apache.doris.nereids.trees.expressions.literal;
import org.apache.doris.nereids.types.DataType;
/** IntegralLiteral */
public abstract class IntegerLikeLiteral extends Literal {
public abstract class IntegerLikeLiteral extends NumericLiteral {
/**
* Constructor for Literal.
*

View File

@ -0,0 +1,34 @@
// 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.literal;
import org.apache.doris.nereids.types.DataType;
/**
* numeric literal
*/
public abstract class NumericLiteral extends Literal {
/**
* Constructor for NumericLiteral.
*
* @param dataType logical data type in Nereids
*/
public NumericLiteral(DataType dataType) {
super(dataType);
}
}