| 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.value; |
| 7 |
|
| 8 |
import java.math.BigDecimal; |
| 9 |
import java.math.RoundingMode; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.engine.CastDataProvider; |
| 13 |
import org.h2.message.DbException; |
| 14 |
|
| 15 |
/** |
| 16 |
* Implementation of the DECFLOAT data type. |
| 17 |
*/ |
| 18 |
public final class ValueDecfloat extends ValueBigDecimalBase { |
| 19 |
|
| 20 |
/** |
| 21 |
* The value 'zero'. |
| 22 |
*/ |
| 23 |
public static final ValueDecfloat ZERO = new ValueDecfloat(BigDecimal.ZERO); |
| 24 |
|
| 25 |
/** |
| 26 |
* The value 'one'. |
| 27 |
*/ |
| 28 |
public static final ValueDecfloat ONE = new ValueDecfloat(BigDecimal.ONE); |
| 29 |
|
| 30 |
/** |
| 31 |
* The positive infinity value. |
| 32 |
*/ |
| 33 |
public static final ValueDecfloat POSITIVE_INFINITY = new ValueDecfloat(null); |
| 34 |
|
| 35 |
/** |
| 36 |
* The negative infinity value. |
| 37 |
*/ |
| 38 |
public static final ValueDecfloat NEGATIVE_INFINITY = new ValueDecfloat(null); |
| 39 |
|
| 40 |
/** |
| 41 |
* The not a number value. |
| 42 |
*/ |
| 43 |
public static final ValueDecfloat NAN = new ValueDecfloat(null); |
| 44 |
|
| 45 |
private ValueDecfloat(BigDecimal value) { |
| 46 |
super(value); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public String getString() { |
| 51 |
if (value == null) { |
| 52 |
if (this == POSITIVE_INFINITY) { |
| 53 |
return "Infinity"; |
| 54 |
} else if (this == NEGATIVE_INFINITY) { |
| 55 |
return "-Infinity"; |
| 56 |
} else { |
| 57 |
return "NaN"; |
| 58 |
} |
| 59 |
} |
| 60 |
return value.toString(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 65 |
if ((sqlFlags & NO_CASTS) == 0) { |
| 66 |
return getSQL(builder.append("CAST(")).append(" AS DECFLOAT)"); |
| 67 |
} |
| 68 |
return getSQL(builder); |
| 69 |
} |
| 70 |
|
| 71 |
private StringBuilder getSQL(StringBuilder builder) { |
| 72 |
if (value != null) { |
| 73 |
return builder.append(value); |
| 74 |
} else if (this == POSITIVE_INFINITY) { |
| 75 |
return builder.append("'Infinity'"); |
| 76 |
} else if (this == NEGATIVE_INFINITY) { |
| 77 |
return builder.append("'-Infinity'"); |
| 78 |
} else { |
| 79 |
return builder.append("'NaN'"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public TypeInfo getType() { |
| 85 |
TypeInfo type = this.type; |
| 86 |
if (type == null) { |
| 87 |
this.type = type = new TypeInfo(DECFLOAT, value != null ? value.precision() : 1, 0, null); |
| 88 |
} |
| 89 |
return type; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public int getValueType() { |
| 94 |
return DECFLOAT; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public Value add(Value v) { |
| 99 |
BigDecimal value2 = ((ValueDecfloat) v).value; |
| 100 |
if (value != null) { |
| 101 |
if (value2 != null) { |
| 102 |
return get(value.add(value2)); |
| 103 |
} |
| 104 |
return v; |
| 105 |
} else if (value2 != null || this == v) { |
| 106 |
return this; |
| 107 |
} |
| 108 |
return NAN; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public Value subtract(Value v) { |
| 113 |
BigDecimal value2 = ((ValueDecfloat) v).value; |
| 114 |
if (value != null) { |
| 115 |
if (value2 != null) { |
| 116 |
return get(value.subtract(value2)); |
| 117 |
} |
| 118 |
return v == POSITIVE_INFINITY ? NEGATIVE_INFINITY : v == NEGATIVE_INFINITY ? POSITIVE_INFINITY : NAN; |
| 119 |
} else if (value2 != null) { |
| 120 |
return this; |
| 121 |
} else if (this == POSITIVE_INFINITY) { |
| 122 |
if (v == NEGATIVE_INFINITY) { |
| 123 |
return POSITIVE_INFINITY; |
| 124 |
} |
| 125 |
} else if (this == NEGATIVE_INFINITY && v == POSITIVE_INFINITY) { |
| 126 |
return NEGATIVE_INFINITY; |
| 127 |
} |
| 128 |
return NAN; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public Value negate() { |
| 133 |
if (value != null) { |
| 134 |
return get(value.negate()); |
| 135 |
} |
| 136 |
return this == POSITIVE_INFINITY ? NEGATIVE_INFINITY : this == NEGATIVE_INFINITY ? POSITIVE_INFINITY : NAN; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public Value multiply(Value v) { |
| 141 |
BigDecimal value2 = ((ValueDecfloat) v).value; |
| 142 |
if (value != null) { |
| 143 |
if (value2 != null) { |
| 144 |
return get(value.multiply(value2)); |
| 145 |
} |
| 146 |
if (v == POSITIVE_INFINITY) { |
| 147 |
int s = value.signum(); |
| 148 |
if (s > 0) { |
| 149 |
return POSITIVE_INFINITY; |
| 150 |
} else if (s < 0) { |
| 151 |
return NEGATIVE_INFINITY; |
| 152 |
} |
| 153 |
} else if (v == NEGATIVE_INFINITY) { |
| 154 |
int s = value.signum(); |
| 155 |
if (s > 0) { |
| 156 |
return NEGATIVE_INFINITY; |
| 157 |
} else if (s < 0) { |
| 158 |
return POSITIVE_INFINITY; |
| 159 |
} |
| 160 |
} |
| 161 |
} else if (value2 != null) { |
| 162 |
if (this == POSITIVE_INFINITY) { |
| 163 |
int s = value2.signum(); |
| 164 |
if (s > 0) { |
| 165 |
return POSITIVE_INFINITY; |
| 166 |
} else if (s < 0) { |
| 167 |
return NEGATIVE_INFINITY; |
| 168 |
} |
| 169 |
} else if (this == NEGATIVE_INFINITY) { |
| 170 |
int s = value2.signum(); |
| 171 |
if (s > 0) { |
| 172 |
return NEGATIVE_INFINITY; |
| 173 |
} else if (s < 0) { |
| 174 |
return POSITIVE_INFINITY; |
| 175 |
} |
| 176 |
} |
| 177 |
} else if (this == POSITIVE_INFINITY) { |
| 178 |
if (v == POSITIVE_INFINITY) { |
| 179 |
return POSITIVE_INFINITY; |
| 180 |
} else if (v == NEGATIVE_INFINITY) { |
| 181 |
return NEGATIVE_INFINITY; |
| 182 |
} |
| 183 |
} else if (this == NEGATIVE_INFINITY) { |
| 184 |
if (v == POSITIVE_INFINITY) { |
| 185 |
return NEGATIVE_INFINITY; |
| 186 |
} else if (v == NEGATIVE_INFINITY) { |
| 187 |
return POSITIVE_INFINITY; |
| 188 |
} |
| 189 |
} |
| 190 |
return NAN; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public Value divide(Value v, TypeInfo quotientType) { |
| 195 |
BigDecimal value2 = ((ValueDecfloat) v).value; |
| 196 |
if (value2 != null && value2.signum() == 0) { |
| 197 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 198 |
} |
| 199 |
if (value != null) { |
| 200 |
if (value2 != null) { |
| 201 |
return divide(value, value2, quotientType); |
| 202 |
} else { |
| 203 |
if (v != NAN) { |
| 204 |
return ZERO; |
| 205 |
} |
| 206 |
} |
| 207 |
} else if (value2 != null && this != NAN) { |
| 208 |
return (this == POSITIVE_INFINITY) == (value2.signum() > 0) ? POSITIVE_INFINITY : NEGATIVE_INFINITY; |
| 209 |
} |
| 210 |
return NAN; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Divides to {@link BigDecimal} values and returns a {@code DECFLOAT} |
| 215 |
* result of the specified data type. |
| 216 |
* |
| 217 |
* @param dividend the dividend |
| 218 |
* @param divisor the divisor |
| 219 |
* @param quotientType the type of quotient |
| 220 |
* @return the quotient |
| 221 |
*/ |
| 222 |
public static ValueDecfloat divide(BigDecimal dividend, BigDecimal divisor, TypeInfo quotientType) { |
| 223 |
int quotientPrecision = (int) quotientType.getPrecision(); |
| 224 |
BigDecimal quotient = dividend.divide(divisor, |
| 225 |
dividend.scale() - dividend.precision() + divisor.precision() - divisor.scale() + quotientPrecision, |
| 226 |
RoundingMode.HALF_DOWN); |
| 227 |
int precision = quotient.precision(); |
| 228 |
if (precision > quotientPrecision) { |
| 229 |
quotient = quotient.setScale(quotient.scale() - precision + quotientPrecision, RoundingMode.HALF_UP); |
| 230 |
} |
| 231 |
return get(quotient); |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public Value modulus(Value v) { |
| 236 |
BigDecimal value2 = ((ValueDecfloat) v).value; |
| 237 |
if (value2 != null && value2.signum() == 0) { |
| 238 |
throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL()); |
| 239 |
} |
| 240 |
if (value != null) { |
| 241 |
if (value2 != null) { |
| 242 |
return get(value.remainder(value2)); |
| 243 |
} else if (v != NAN) { |
| 244 |
return this; |
| 245 |
} |
| 246 |
} |
| 247 |
return NAN; |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 252 |
BigDecimal value2 = ((ValueDecfloat) o).value; |
| 253 |
if (value != null) { |
| 254 |
if (value2 != null) { |
| 255 |
return value.compareTo(value2); |
| 256 |
} |
| 257 |
return o == NEGATIVE_INFINITY ? 1 : -1; |
| 258 |
} else if (value2 != null) { |
| 259 |
return this == NEGATIVE_INFINITY ? -1 : 1; |
| 260 |
} else if (this == o) { |
| 261 |
return 0; |
| 262 |
} else if (this == NEGATIVE_INFINITY) { |
| 263 |
return -1; |
| 264 |
} else if (o == NEGATIVE_INFINITY) { |
| 265 |
return 1; |
| 266 |
} else { |
| 267 |
return this == POSITIVE_INFINITY ? -1 : 1; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public int getSignum() { |
| 273 |
if (value != null) { |
| 274 |
return value.signum(); |
| 275 |
} |
| 276 |
return this == POSITIVE_INFINITY ? 1 : this == NEGATIVE_INFINITY ? -1 : 0; |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public BigDecimal getBigDecimal() { |
| 281 |
if (value != null) { |
| 282 |
return value; |
| 283 |
} |
| 284 |
throw getDataConversionError(NUMERIC); |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
public float getFloat() { |
| 289 |
if (value != null) { |
| 290 |
return value.floatValue(); |
| 291 |
} else if (this == POSITIVE_INFINITY) { |
| 292 |
return Float.POSITIVE_INFINITY; |
| 293 |
} else if (this == NEGATIVE_INFINITY) { |
| 294 |
return Float.NEGATIVE_INFINITY; |
| 295 |
} else { |
| 296 |
return Float.NaN; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public double getDouble() { |
| 302 |
if (value != null) { |
| 303 |
return value.doubleValue(); |
| 304 |
} else if (this == POSITIVE_INFINITY) { |
| 305 |
return Double.POSITIVE_INFINITY; |
| 306 |
} else if (this == NEGATIVE_INFINITY) { |
| 307 |
return Double.NEGATIVE_INFINITY; |
| 308 |
} else { |
| 309 |
return Double.NaN; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public int hashCode() { |
| 315 |
return value != null ? getClass().hashCode() * 31 + value.hashCode() : System.identityHashCode(this); |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public boolean equals(Object other) { |
| 320 |
if (other instanceof ValueDecfloat) { |
| 321 |
BigDecimal value2 = ((ValueDecfloat) other).value; |
| 322 |
if (value != null) { |
| 323 |
return value.equals(value2); |
| 324 |
} else if (value2 == null && this == other) { |
| 325 |
return true; |
| 326 |
} |
| 327 |
} |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public int getMemory() { |
| 333 |
return value != null ? value.precision() + 120 : 32; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Returns {@code true}, if this value is finite. |
| 338 |
* |
| 339 |
* @return {@code true}, if this value is finite, {@code false} otherwise |
| 340 |
*/ |
| 341 |
public boolean isFinite() { |
| 342 |
return value != null; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Get or create a DECFLOAT value for the given big decimal. |
| 347 |
* |
| 348 |
* @param dec the big decimal |
| 349 |
* @return the value |
| 350 |
*/ |
| 351 |
public static ValueDecfloat get(BigDecimal dec) { |
| 352 |
dec = dec.stripTrailingZeros(); |
| 353 |
if (BigDecimal.ZERO.equals(dec)) { |
| 354 |
return ZERO; |
| 355 |
} else if (BigDecimal.ONE.equals(dec)) { |
| 356 |
return ONE; |
| 357 |
} |
| 358 |
return (ValueDecfloat) Value.cache(new ValueDecfloat(dec)); |
| 359 |
} |
| 360 |
|
| 361 |
} |