| 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 static org.h2.util.DateTimeUtils.NANOS_PER_DAY; |
| 9 |
import static org.h2.util.DateTimeUtils.NANOS_PER_HOUR; |
| 10 |
import static org.h2.util.DateTimeUtils.NANOS_PER_MINUTE; |
| 11 |
import static org.h2.util.DateTimeUtils.NANOS_PER_SECOND; |
| 12 |
|
| 13 |
import java.math.BigDecimal; |
| 14 |
import java.math.RoundingMode; |
| 15 |
|
| 16 |
import org.h2.api.Interval; |
| 17 |
import org.h2.api.IntervalQualifier; |
| 18 |
import org.h2.engine.CastDataProvider; |
| 19 |
import org.h2.message.DbException; |
| 20 |
import org.h2.util.DateTimeUtils; |
| 21 |
import org.h2.util.IntervalUtils; |
| 22 |
|
| 23 |
/** |
| 24 |
* Implementation of the INTERVAL data type. |
| 25 |
*/ |
| 26 |
public final class ValueInterval extends Value { |
| 27 |
|
| 28 |
/** |
| 29 |
* The default leading field precision for intervals. |
| 30 |
*/ |
| 31 |
public static final int DEFAULT_PRECISION = 2; |
| 32 |
|
| 33 |
/** |
| 34 |
* The maximum leading field precision for intervals. |
| 35 |
*/ |
| 36 |
public static final int MAXIMUM_PRECISION = 18; |
| 37 |
|
| 38 |
/** |
| 39 |
* The default scale for intervals with seconds. |
| 40 |
*/ |
| 41 |
public static final int DEFAULT_SCALE = 6; |
| 42 |
|
| 43 |
/** |
| 44 |
* The maximum scale for intervals with seconds. |
| 45 |
*/ |
| 46 |
public static final int MAXIMUM_SCALE = 9; |
| 47 |
|
| 48 |
private static final long[] MULTIPLIERS = { |
| 49 |
// INTERVAL_SECOND |
| 50 |
DateTimeUtils.NANOS_PER_SECOND, |
| 51 |
// INTERVAL_YEAR_TO_MONTH |
| 52 |
12, |
| 53 |
// INTERVAL_DAY_TO_HOUR |
| 54 |
24, |
| 55 |
// INTERVAL_DAY_TO_MINUTE |
| 56 |
24 * 60, |
| 57 |
// INTERVAL_DAY_TO_SECOND |
| 58 |
DateTimeUtils.NANOS_PER_DAY, |
| 59 |
// INTERVAL_HOUR_TO_MINUTE: |
| 60 |
60, |
| 61 |
// INTERVAL_HOUR_TO_SECOND |
| 62 |
DateTimeUtils.NANOS_PER_HOUR, |
| 63 |
// INTERVAL_MINUTE_TO_SECOND |
| 64 |
DateTimeUtils.NANOS_PER_MINUTE // |
| 65 |
}; |
| 66 |
|
| 67 |
private final int valueType; |
| 68 |
|
| 69 |
private final boolean negative; |
| 70 |
|
| 71 |
private final long leading; |
| 72 |
|
| 73 |
private final long remaining; |
| 74 |
|
| 75 |
/** |
| 76 |
* Create a ValueInterval instance. |
| 77 |
* |
| 78 |
* @param qualifier |
| 79 |
* qualifier |
| 80 |
* @param negative |
| 81 |
* whether interval is negative |
| 82 |
* @param leading |
| 83 |
* value of leading field |
| 84 |
* @param remaining |
| 85 |
* values of all remaining fields |
| 86 |
* @return interval value |
| 87 |
*/ |
| 88 |
public static ValueInterval from(IntervalQualifier qualifier, boolean negative, long leading, long remaining) { |
| 89 |
negative = IntervalUtils.validateInterval(qualifier, negative, leading, remaining); |
| 90 |
return (ValueInterval) Value |
| 91 |
.cache(new ValueInterval(qualifier.ordinal() + INTERVAL_YEAR, negative, leading, remaining)); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns display size for the specified qualifier, precision and |
| 96 |
* fractional seconds precision. |
| 97 |
* |
| 98 |
* @param type |
| 99 |
* the value type |
| 100 |
* @param precision |
| 101 |
* leading field precision |
| 102 |
* @param scale |
| 103 |
* fractional seconds precision. Ignored if specified type of |
| 104 |
* interval does not have seconds. |
| 105 |
* @return display size |
| 106 |
*/ |
| 107 |
public static int getDisplaySize(int type, int precision, int scale) { |
| 108 |
switch (type) { |
| 109 |
case INTERVAL_YEAR: |
| 110 |
case INTERVAL_HOUR: |
| 111 |
// INTERVAL '-11' YEAR |
| 112 |
// INTERVAL '-11' HOUR |
| 113 |
return 17 + precision; |
| 114 |
case INTERVAL_MONTH: |
| 115 |
// INTERVAL '-11' MONTH |
| 116 |
return 18 + precision; |
| 117 |
case INTERVAL_DAY: |
| 118 |
// INTERVAL '-11' DAY |
| 119 |
return 16 + precision; |
| 120 |
case INTERVAL_MINUTE: |
| 121 |
// INTERVAL '-11' MINUTE |
| 122 |
return 19 + precision; |
| 123 |
case INTERVAL_SECOND: |
| 124 |
// INTERVAL '-11' SECOND |
| 125 |
// INTERVAL '-11.999999' SECOND |
| 126 |
return scale > 0 ? 20 + precision + scale : 19 + precision; |
| 127 |
case INTERVAL_YEAR_TO_MONTH: |
| 128 |
// INTERVAL '-11-11' YEAR TO MONTH |
| 129 |
return 29 + precision; |
| 130 |
case INTERVAL_DAY_TO_HOUR: |
| 131 |
// INTERVAL '-11 23' DAY TO HOUR |
| 132 |
return 27 + precision; |
| 133 |
case INTERVAL_DAY_TO_MINUTE: |
| 134 |
// INTERVAL '-11 23:59' DAY TO MINUTE |
| 135 |
return 32 + precision; |
| 136 |
case INTERVAL_DAY_TO_SECOND: |
| 137 |
// INTERVAL '-11 23:59.59' DAY TO SECOND |
| 138 |
// INTERVAL '-11 23:59.59.999999' DAY TO SECOND |
| 139 |
return scale > 0 ? 36 + precision + scale : 35 + precision; |
| 140 |
case INTERVAL_HOUR_TO_MINUTE: |
| 141 |
// INTERVAL '-11:59' HOUR TO MINUTE |
| 142 |
return 30 + precision; |
| 143 |
case INTERVAL_HOUR_TO_SECOND: |
| 144 |
// INTERVAL '-11:59:59' HOUR TO SECOND |
| 145 |
// INTERVAL '-11:59:59.999999' HOUR TO SECOND |
| 146 |
return scale > 0 ? 34 + precision + scale : 33 + precision; |
| 147 |
case INTERVAL_MINUTE_TO_SECOND: |
| 148 |
// INTERVAL '-11:59' MINUTE TO SECOND |
| 149 |
// INTERVAL '-11:59.999999' MINUTE TO SECOND |
| 150 |
return scale > 0 ? 33 + precision + scale : 32 + precision; |
| 151 |
default: |
| 152 |
throw DbException.getUnsupportedException(Integer.toString(type)); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private ValueInterval(int type, boolean negative, long leading, long remaining) { |
| 157 |
this.valueType = type; |
| 158 |
this.negative = negative; |
| 159 |
this.leading = leading; |
| 160 |
this.remaining = remaining; |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 165 |
return IntervalUtils.appendInterval(builder, getQualifier(), negative, leading, remaining); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public TypeInfo getType() { |
| 170 |
return TypeInfo.getTypeInfo(valueType); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public int getValueType() { |
| 175 |
return valueType; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public int getMemory() { |
| 180 |
// Java 11 with -XX:-UseCompressedOops |
| 181 |
return 48; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Check if the precision is smaller or equal than the given precision. |
| 186 |
* |
| 187 |
* @param prec |
| 188 |
* the maximum precision |
| 189 |
* @return true if the precision of this value is smaller or equal to the |
| 190 |
* given precision |
| 191 |
*/ |
| 192 |
boolean checkPrecision(long prec) { |
| 193 |
if (prec < MAXIMUM_PRECISION) { |
| 194 |
for (long l = leading, p = 1, precision = 0; l >= p; p *= 10) { |
| 195 |
if (++precision > prec) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
ValueInterval setPrecisionAndScale(TypeInfo targetType, Object column) { |
| 204 |
int targetScale = targetType.getScale(); |
| 205 |
ValueInterval v = this; |
| 206 |
convertScale: if (targetScale < ValueInterval.MAXIMUM_SCALE) { |
| 207 |
long range; |
| 208 |
switch (valueType) { |
| 209 |
case INTERVAL_SECOND: |
| 210 |
range = NANOS_PER_SECOND; |
| 211 |
break; |
| 212 |
case INTERVAL_DAY_TO_SECOND: |
| 213 |
range = NANOS_PER_DAY; |
| 214 |
break; |
| 215 |
case INTERVAL_HOUR_TO_SECOND: |
| 216 |
range = NANOS_PER_HOUR; |
| 217 |
break; |
| 218 |
case INTERVAL_MINUTE_TO_SECOND: |
| 219 |
range = NANOS_PER_MINUTE; |
| 220 |
break; |
| 221 |
default: |
| 222 |
break convertScale; |
| 223 |
} |
| 224 |
long l = leading; |
| 225 |
long r = DateTimeUtils.convertScale(remaining, targetScale, |
| 226 |
l == 999_999_999_999_999_999L ? range : Long.MAX_VALUE); |
| 227 |
if (r != remaining) { |
| 228 |
if (r >= range) { |
| 229 |
l++; |
| 230 |
r -= range; |
| 231 |
} |
| 232 |
v = ValueInterval.from(v.getQualifier(), v.isNegative(), l, r); |
| 233 |
} |
| 234 |
} |
| 235 |
if (!v.checkPrecision(targetType.getPrecision())) { |
| 236 |
throw v.getValueTooLongException(targetType, column); |
| 237 |
} |
| 238 |
return v; |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public String getString() { |
| 243 |
return IntervalUtils.appendInterval(new StringBuilder(), getQualifier(), negative, leading, remaining) |
| 244 |
.toString(); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public long getLong() { |
| 249 |
long l = leading; |
| 250 |
if (valueType >= INTERVAL_SECOND && remaining != 0L |
| 251 |
&& remaining >= MULTIPLIERS[valueType - INTERVAL_SECOND] >> 1) { |
| 252 |
l++; |
| 253 |
} |
| 254 |
return negative ? -l : l; |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public BigDecimal getBigDecimal() { |
| 259 |
if (valueType < INTERVAL_SECOND || remaining == 0L) { |
| 260 |
return BigDecimal.valueOf(negative ? -leading : leading); |
| 261 |
} |
| 262 |
BigDecimal m = BigDecimal.valueOf(MULTIPLIERS[valueType - INTERVAL_SECOND]); |
| 263 |
BigDecimal bd = BigDecimal.valueOf(leading) |
| 264 |
.add(BigDecimal.valueOf(remaining).divide(m, m.precision(), RoundingMode.HALF_DOWN)) // |
| 265 |
.stripTrailingZeros(); |
| 266 |
return negative ? bd.negate() : bd; |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
public float getFloat() { |
| 271 |
if (valueType < INTERVAL_SECOND || remaining == 0L) { |
| 272 |
return negative ? -leading : leading; |
| 273 |
} |
| 274 |
return getBigDecimal().floatValue(); |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public double getDouble() { |
| 279 |
if (valueType < INTERVAL_SECOND || remaining == 0L) { |
| 280 |
return negative ? -leading : leading; |
| 281 |
} |
| 282 |
return getBigDecimal().doubleValue(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns the interval. |
| 287 |
* |
| 288 |
* @return the interval |
| 289 |
*/ |
| 290 |
public Interval getInterval() { |
| 291 |
return new Interval(getQualifier(), negative, leading, remaining); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns the interval qualifier. |
| 296 |
* |
| 297 |
* @return the interval qualifier |
| 298 |
*/ |
| 299 |
public IntervalQualifier getQualifier() { |
| 300 |
return IntervalQualifier.valueOf(valueType - INTERVAL_YEAR); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Returns where the interval is negative. |
| 305 |
* |
| 306 |
* @return where the interval is negative |
| 307 |
*/ |
| 308 |
public boolean isNegative() { |
| 309 |
return negative; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Returns value of leading field of this interval. For {@code SECOND} |
| 314 |
* intervals returns integer part of seconds. |
| 315 |
* |
| 316 |
* @return value of leading field |
| 317 |
*/ |
| 318 |
public long getLeading() { |
| 319 |
return leading; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Returns combined value of remaining fields of this interval. For |
| 324 |
* {@code SECOND} intervals returns nanoseconds. |
| 325 |
* |
| 326 |
* @return combined value of remaining fields |
| 327 |
*/ |
| 328 |
public long getRemaining() { |
| 329 |
return remaining; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public int hashCode() { |
| 334 |
final int prime = 31; |
| 335 |
int result = 1; |
| 336 |
result = prime * result + valueType; |
| 337 |
result = prime * result + (negative ? 1231 : 1237); |
| 338 |
result = prime * result + (int) (leading ^ leading >>> 32); |
| 339 |
result = prime * result + (int) (remaining ^ remaining >>> 32); |
| 340 |
return result; |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public boolean equals(Object obj) { |
| 345 |
if (this == obj) { |
| 346 |
return true; |
| 347 |
} |
| 348 |
if (!(obj instanceof ValueInterval)) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
ValueInterval other = (ValueInterval) obj; |
| 352 |
return valueType == other.valueType && negative == other.negative && leading == other.leading |
| 353 |
&& remaining == other.remaining; |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) { |
| 358 |
ValueInterval other = (ValueInterval) v; |
| 359 |
if (negative != other.negative) { |
| 360 |
return negative ? -1 : 1; |
| 361 |
} |
| 362 |
int cmp = Long.compare(leading, other.leading); |
| 363 |
if (cmp == 0) { |
| 364 |
cmp = Long.compare(remaining, other.remaining); |
| 365 |
} |
| 366 |
return negative ? -cmp : cmp; |
| 367 |
} |
| 368 |
|
| 369 |
@Override |
| 370 |
public int getSignum() { |
| 371 |
return negative ? -1 : leading == 0L && remaining == 0L ? 0 : 1; |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public Value add(Value v) { |
| 376 |
return IntervalUtils.intervalFromAbsolute(getQualifier(), |
| 377 |
IntervalUtils.intervalToAbsolute(this).add(IntervalUtils.intervalToAbsolute((ValueInterval) v))); |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public Value subtract(Value v) { |
| 382 |
return IntervalUtils.intervalFromAbsolute(getQualifier(), |
| 383 |
IntervalUtils.intervalToAbsolute(this).subtract(IntervalUtils.intervalToAbsolute((ValueInterval) v))); |
| 384 |
} |
| 385 |
|
| 386 |
@Override |
| 387 |
public Value negate() { |
| 388 |
if (leading == 0L && remaining == 0L) { |
| 389 |
return this; |
| 390 |
} |
| 391 |
return Value.cache(new ValueInterval(valueType, !negative, leading, remaining)); |
| 392 |
} |
| 393 |
|
| 394 |
} |