|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.utils.datetime; |
| 7 | + |
| 8 | +import java.time.Duration; |
| 9 | +import java.time.Period; |
| 10 | +import java.time.temporal.TemporalAmount; |
| 11 | +import org.apache.calcite.avatica.util.TimeUnit; |
| 12 | +import org.opensearch.sql.data.model.*; |
| 13 | +import org.opensearch.sql.data.type.ExprCoreType; |
| 14 | +import org.opensearch.sql.exception.SemanticCheckException; |
| 15 | +import org.opensearch.sql.expression.function.FunctionProperties; |
| 16 | + |
| 17 | +public final class DateTimeConversionUtils { |
| 18 | + private DateTimeConversionUtils() {} |
| 19 | + |
| 20 | + /** |
| 21 | + * Convert the given ExprValue to an ExprTimestampValue. If the input is a string, it will convert |
| 22 | + * date / time / timestamp strings to ExprTimestampValue. |
| 23 | + * |
| 24 | + * @param value the value to convert, can be either a ExprDateValue, ExprTimeValue, |
| 25 | + * ExprTimestampValue or ExprStringValue |
| 26 | + * @param properties the function properties |
| 27 | + * @return the converted ExprTimestampValue |
| 28 | + */ |
| 29 | + public static ExprTimestampValue forceConvertToTimestampValue( |
| 30 | + ExprValue value, FunctionProperties properties) { |
| 31 | + return switch (value) { |
| 32 | + case ExprTimestampValue timestampValue -> timestampValue; |
| 33 | + case ExprDateValue dateValue -> (ExprTimestampValue) |
| 34 | + ExprValueUtils.timestampValue(dateValue.timestampValue()); |
| 35 | + case ExprTimeValue timeValue -> (ExprTimestampValue) |
| 36 | + ExprValueUtils.timestampValue(timeValue.timestampValue(properties)); |
| 37 | + case ExprStringValue stringValue -> new ExprTimestampValue( |
| 38 | + DateTimeParser.parse(stringValue.stringValue())); |
| 39 | + default -> throw new SemanticCheckException( |
| 40 | + String.format( |
| 41 | + "Cannot convert %s to timestamp, only STRING, DATE, TIME and TIMESTAMP are supported", |
| 42 | + value.type())); |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Convert the given ExprValue to an ExprTimestampValue. If the input is a string, it only accepts |
| 48 | + * a string formatted as a valid timestamp 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'. |
| 49 | + * |
| 50 | + * @param value the value to convert, can be either a ExprDateValue, ExprTimeValue, |
| 51 | + * ExprTimestampValue or ExprStringValue |
| 52 | + * @param properties the function properties |
| 53 | + * @return the converted ExprTimestampValue |
| 54 | + */ |
| 55 | + public static ExprTimestampValue convertToTimestampValue( |
| 56 | + ExprValue value, FunctionProperties properties) { |
| 57 | + if (value instanceof ExprTimestampValue timestampValue) { |
| 58 | + return timestampValue; |
| 59 | + } else if (value instanceof ExprTimeValue) { |
| 60 | + return new ExprTimestampValue(((ExprTimeValue) value).timestampValue(properties)); |
| 61 | + } else if (value.type() == ExprCoreType.STRING) { |
| 62 | + return new ExprTimestampValue(value.stringValue()); |
| 63 | + } else { |
| 64 | + try { |
| 65 | + return new ExprTimestampValue(value.timestampValue()); |
| 66 | + } catch (SemanticCheckException e) { |
| 67 | + throw new SemanticCheckException( |
| 68 | + String.format( |
| 69 | + "Cannot convert %s to timestamp, only STRING, DATE, TIME and TIMESTAMP are" |
| 70 | + + " supported", |
| 71 | + value.type()), |
| 72 | + e); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Convert the given ExprValue to an ExprDateValue. If the input is a string, it only accepts a |
| 79 | + * string formatted as a valid date 'yyyy-MM-dd'. |
| 80 | + * |
| 81 | + * @param value the value to convert, can be either a ExprDateValue, ExprTimeValue, |
| 82 | + * ExprTimestampValue or ExprStringValue |
| 83 | + * @param properties the function properties |
| 84 | + * @return the converted ExprDateValue |
| 85 | + */ |
| 86 | + public static ExprDateValue convertToDateValue(ExprValue value, FunctionProperties properties) { |
| 87 | + switch (value) { |
| 88 | + case ExprDateValue dateValue -> { |
| 89 | + return dateValue; |
| 90 | + } |
| 91 | + case ExprTimeValue timeValue -> { |
| 92 | + return new ExprDateValue(timeValue.dateValue(properties)); |
| 93 | + } |
| 94 | + case ExprTimestampValue timestampValue -> { |
| 95 | + return new ExprDateValue(timestampValue.dateValue()); |
| 96 | + } |
| 97 | + case ExprStringValue ignored -> { |
| 98 | + return new ExprDateValue(value.stringValue()); |
| 99 | + } |
| 100 | + default -> { |
| 101 | + throw new SemanticCheckException( |
| 102 | + String.format( |
| 103 | + "Cannot convert %s to date, only STRING, DATE, TIME and TIMESTAMP are supported", |
| 104 | + value.type())); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Create a temporal amount of the given number of units. For duration below a day, it returns |
| 111 | + * duration; for duration including and above a day, it returns period for natural days, months, |
| 112 | + * quarters, and years, which may be of unfixed lengths. |
| 113 | + * |
| 114 | + * @param number The count of unit |
| 115 | + * @param unit The unit of the temporal amount |
| 116 | + * @return A temporal amount value, can be either a Period or a Duration |
| 117 | + */ |
| 118 | + public static TemporalAmount convertToTemporalAmount(long number, TimeUnit unit) { |
| 119 | + return switch (unit) { |
| 120 | + case YEAR -> Period.ofYears((int) number); |
| 121 | + case QUARTER -> Period.ofMonths((int) number * 3); |
| 122 | + case MONTH -> Period.ofMonths((int) number); |
| 123 | + case WEEK -> Period.ofWeeks((int) number); |
| 124 | + case DAY -> Period.ofDays((int) number); |
| 125 | + case HOUR -> Duration.ofHours(number); |
| 126 | + case MINUTE -> Duration.ofMinutes(number); |
| 127 | + case SECOND -> Duration.ofSeconds(number); |
| 128 | + case MILLISECOND -> Duration.ofMillis(number); |
| 129 | + case MICROSECOND -> Duration.ofNanos(number * 1000); |
| 130 | + case NANOSECOND -> Duration.ofNanos(number); |
| 131 | + |
| 132 | + default -> throw new UnsupportedOperationException( |
| 133 | + "No mapping defined for Calcite TimeUnit: " + unit); |
| 134 | + }; |
| 135 | + } |
| 136 | +} |
0 commit comments