| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.function; |
| 7 |
|
| 8 |
import java.math.BigDecimal; |
| 9 |
import java.math.MathContext; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.expression.Expression; |
| 14 |
import org.h2.expression.TypedValueExpression; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.util.MathUtils; |
| 17 |
import org.h2.value.DataType; |
| 18 |
import org.h2.value.TypeInfo; |
| 19 |
import org.h2.value.Value; |
| 20 |
import org.h2.value.ValueDecfloat; |
| 21 |
import org.h2.value.ValueNumeric; |
| 22 |
|
| 23 |
/** |
| 24 |
* A TRUNCATE_VALUE function. |
| 25 |
*/ |
| 26 |
public final class TruncateValueFunction extends FunctionN { |
| 27 |
|
| 28 |
public TruncateValueFunction(Expression arg1, Expression arg2, Expression arg3) { |
| 29 |
super(new Expression[] { arg1, arg2, arg3 }); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public Value getValue(SessionLocal session, Value v1, Value v2, Value v3) { |
| 34 |
long precision = v2.getLong(); |
| 35 |
boolean force = v3.getBoolean(); |
| 36 |
if (precision <= 0) { |
| 37 |
throw DbException.get(ErrorCode.INVALID_VALUE_PRECISION, Long.toString(precision), "1", |
| 38 |
"" + Integer.MAX_VALUE); |
| 39 |
} |
| 40 |
TypeInfo t = v1.getType(); |
| 41 |
int valueType = t.getValueType(); |
| 42 |
if (DataType.getDataType(valueType).supportsPrecision) { |
| 43 |
if (precision < t.getPrecision()) { |
| 44 |
switch (valueType) { |
| 45 |
case Value.NUMERIC: { |
| 46 |
BigDecimal bd = v1.getBigDecimal().round(new MathContext(MathUtils.convertLongToInt(precision))); |
| 47 |
if (bd.scale() < 0) { |
| 48 |
bd = bd.setScale(0); |
| 49 |
} |
| 50 |
return ValueNumeric.get(bd); |
| 51 |
} |
| 52 |
case Value.DECFLOAT: |
| 53 |
return ValueDecfloat |
| 54 |
.get(v1.getBigDecimal().round(new MathContext(MathUtils.convertLongToInt(precision)))); |
| 55 |
default: |
| 56 |
return v1.castTo(TypeInfo.getTypeInfo(valueType, precision, t.getScale(), t.getExtTypeInfo()), |
| 57 |
session); |
| 58 |
} |
| 59 |
} |
| 60 |
} else if (force) { |
| 61 |
BigDecimal bd; |
| 62 |
switch (valueType) { |
| 63 |
case Value.TINYINT: |
| 64 |
case Value.SMALLINT: |
| 65 |
case Value.INTEGER: |
| 66 |
bd = BigDecimal.valueOf(v1.getInt()); |
| 67 |
break; |
| 68 |
case Value.BIGINT: |
| 69 |
bd = BigDecimal.valueOf(v1.getLong()); |
| 70 |
break; |
| 71 |
case Value.REAL: |
| 72 |
case Value.DOUBLE: |
| 73 |
bd = v1.getBigDecimal(); |
| 74 |
break; |
| 75 |
default: |
| 76 |
return v1; |
| 77 |
} |
| 78 |
bd = bd.round(new MathContext(MathUtils.convertLongToInt(precision))); |
| 79 |
if (valueType == Value.DECFLOAT) { |
| 80 |
return ValueDecfloat.get(bd); |
| 81 |
} |
| 82 |
if (bd.scale() < 0) { |
| 83 |
bd = bd.setScale(0); |
| 84 |
} |
| 85 |
return ValueNumeric.get(bd).convertTo(valueType); |
| 86 |
} |
| 87 |
return v1; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Expression optimize(SessionLocal session) { |
| 92 |
boolean allConst = optimizeArguments(session, true); |
| 93 |
type = args[0].getType(); |
| 94 |
if (allConst) { |
| 95 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 96 |
} |
| 97 |
return this; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public String getName() { |
| 102 |
return "TRUNCATE_VALUE"; |
| 103 |
} |
| 104 |
|
| 105 |
} |