[round](decimalv2) round precise decimalv2 value (#22258)

This commit is contained in:
Gabriel
2023-07-27 10:00:36 +08:00
committed by GitHub
parent 163a38a527
commit 341c45974c
7 changed files with 57 additions and 18 deletions

View File

@ -151,7 +151,6 @@ private:
public:
static NO_INLINE void apply(const Container& in, UInt32 in_scale, Container& out,
Int16 out_scale) {
constexpr bool is_decimalv2 = IsDecimalV2<T>;
Int16 scale_arg = in_scale - out_scale;
if (scale_arg > 0) {
size_t scale = int_exp10(scale_arg);
@ -162,14 +161,13 @@ public:
if (out_scale < 0) {
while (p_in < end_in) {
Op::compute(p_in, scale, p_out,
is_decimalv2 ? int_exp10(9 - out_scale) : int_exp10(-out_scale));
Op::compute(p_in, scale, p_out, int_exp10(-out_scale));
++p_in;
++p_out;
}
} else {
while (p_in < end_in) {
Op::compute(p_in, scale, p_out, is_decimalv2 ? scale : 1);
Op::compute(p_in, scale, p_out, 1);
++p_in;
++p_out;
}

View File

@ -1689,6 +1689,14 @@ public class FunctionCallExpr extends Expr {
&& (args[ix].isArrayType())
&& ((ArrayType) args[ix]).getItemType().isDecimalV3()))) {
continue;
} else if (!argTypes[i].matchesType(args[ix])
&& ROUND_FUNCTION_SET.contains(fnName.getFunction())
&& ConnectContext.get() != null
&& ConnectContext.get().getSessionVariable().roundPreciseDecimalV2Value
&& argTypes[i].isDecimalV2()
&& args[ix].isDecimalV3()) {
uncheckedCastChild(ScalarType.createDecimalV3Type(ScalarType.MAX_DECIMALV2_PRECISION,
((ScalarType) argTypes[i]).getScalarScale()), i);
} else if (!argTypes[i].matchesType(args[ix])
&& !(argTypes[i].isDecimalV3OrContainsDecimalV3()
&& args[ix].isDecimalV3OrContainsDecimalV3())) {

View File

@ -21,12 +21,14 @@ import org.apache.doris.analysis.ArithmeticExpr;
import org.apache.doris.analysis.BinaryPredicate;
import org.apache.doris.analysis.CastExpr;
import org.apache.doris.analysis.CompoundPredicate;
import org.apache.doris.analysis.FunctionCallExpr;
import org.apache.doris.analysis.InPredicate;
import org.apache.doris.analysis.IsNullPredicate;
import org.apache.doris.analysis.LikePredicate;
import org.apache.doris.analysis.MatchPredicate;
import org.apache.doris.builtins.ScalarBuiltins;
import org.apache.doris.catalog.Function.NullableMode;
import org.apache.doris.qe.ConnectContext;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
@ -453,6 +455,18 @@ public class FunctionSet<T> {
return false;
}
}
// If set `roundPreciseDecimalV2Value`, only use decimalv3 as target type to execute round function
if (ConnectContext.get() != null
&& ConnectContext.get().getSessionVariable().roundPreciseDecimalV2Value
&& FunctionCallExpr.ROUND_FUNCTION_SET.contains(desc.functionName())
&& descArgType.isDecimalV2() && candicateArgType.getPrimitiveType() != PrimitiveType.DECIMAL128) {
return false;
} else if (ConnectContext.get() != null
&& ConnectContext.get().getSessionVariable().roundPreciseDecimalV2Value
&& FunctionCallExpr.ROUND_FUNCTION_SET.contains(desc.functionName())
&& descArgType.isDecimalV2() && candicateArgType.getPrimitiveType() == PrimitiveType.DECIMAL128) {
return true;
}
if ((descArgType.isDecimalV3() && candicateArgType.isDecimalV2())
|| (descArgType.isDecimalV2() && candicateArgType.isDecimalV3())) {
return false;

View File

@ -363,6 +363,8 @@ public class SessionVariable implements Serializable, Writable {
public static final String CBO_NET_WEIGHT = "cbo_net_weight";
public static final String ROUND_PRECISE_DECIMALV2_VALUE = "round_precise_decimalv2_value";
public static final List<String> DEBUG_VARIABLES = ImmutableList.of(
SKIP_DELETE_PREDICATE,
SKIP_DELETE_BITMAP,
@ -377,6 +379,9 @@ public class SessionVariable implements Serializable, Writable {
// if it is setStmt, we needn't collect session origin value
public boolean isSingleSetVar = false;
@VariableMgr.VarAttr(name = ROUND_PRECISE_DECIMALV2_VALUE)
public boolean roundPreciseDecimalV2Value = false;
@VariableMgr.VarAttr(name = INSERT_VISIBLE_TIMEOUT_MS, needForward = true)
public long insertVisibleTimeoutMs = DEFAULT_INSERT_VISIBLE_TIMEOUT_MS;

View File

@ -1242,10 +1242,11 @@ visible_functions = {
[['floor', 'dfloor'], 'DOUBLE', ['DOUBLE'], ''],
[['round', 'dround'], 'DOUBLE', ['DOUBLE'], ''],
[['round_bankers'], 'DOUBLE', ['DOUBLE'], ''],
[['ceil', 'ceiling', 'dceil'], 'DECIMALV2', ['DECIMALV2'], ''],
[['floor', 'dfloor'], 'DECIMALV2', ['DECIMALV2'], ''],
[['round', 'dround'], 'DECIMALV2', ['DECIMALV2'], ''],
[['round_bankers'], 'DECIMALV2', ['DECIMALV2'], ''],
[['ceil', 'ceiling', 'dceil'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['floor', 'dfloor'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round', 'dround'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round_bankers'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['truncate'], 'DOUBLE', ['DOUBLE'], ''],
[['ceil', 'ceiling', 'dceil'], 'DECIMAL32', ['DECIMAL32'], ''],
[['floor', 'dfloor'], 'DECIMAL32', ['DECIMAL32'], ''],
[['round', 'dround'], 'DECIMAL32', ['DECIMAL32'], ''],
@ -1259,25 +1260,20 @@ visible_functions = {
[['round', 'dround'], 'DECIMAL128', ['DECIMAL128'], ''],
[['round_bankers'], 'DECIMAL128', ['DECIMAL128'], ''],
[['round', 'dround'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round', 'dround'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['round', 'dround'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['round', 'dround'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['round', 'dround'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['round_bankers', 'round_bankers'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round_bankers', 'round_bankers'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['round_bankers'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['round_bankers'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['round_bankers'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['truncate'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['truncate'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['truncate'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['truncate'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['truncate'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],

View File

@ -12,9 +12,9 @@
10.12
-- !truncate --
1 1989 1001 123.1 0.1 6.3
2 1986 1001 1243.5 20.2 789.2
3 1989 1002 24453.3 78945.0 3653.9
1.0 1989.0 1001.0 123.1 0.1 6.3
2.0 1986.0 1001.0 1243.5 20.2 789.2
3.0 1989.0 1002.0 24453.3 78945.0 3654.0
-- !select --
16 16 16
@ -116,6 +116,12 @@
-- !query --
0.000 0.000 0.000
-- !query --
16.02
-- !query --
16.02
-- !query --
16.03
@ -123,8 +129,14 @@
16.02
-- !query --
16.030000000
16.03
-- !query --
16.020000000
16.02
-- !query --
16.03
-- !query --
16.02

View File

@ -145,6 +145,12 @@
sql """ DROP TABLE IF EXISTS `test_decimalv2` """
sql """ CREATE TABLE `test_decimalv2` ( id int, decimal_col DECIMAL(19,5)) ENGINE=OLAP duplicate KEY (id) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ( "replication_allocation" = "tag.location.default: 1"); """
sql """ insert into test_decimalv2 values (1, 16.025); """
sql """ set round_precise_decimalv2_value=false; """
qt_query """ select round(decimal_col,2) from test_decimalv2; """
qt_query """ select truncate(decimal_col,2) from test_decimalv2; """
qt_query """ select ceil(decimal_col,2) from test_decimalv2; """
qt_query """ select floor(decimal_col,2) from test_decimalv2; """
sql """ set round_precise_decimalv2_value=true; """
qt_query """ select round(decimal_col,2) from test_decimalv2; """
qt_query """ select truncate(decimal_col,2) from test_decimalv2; """
qt_query """ select ceil(decimal_col,2) from test_decimalv2; """