| 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; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.message.DbException; |
| 10 |
import org.h2.util.DateTimeUtils; |
| 11 |
import org.h2.util.TimeZoneProvider; |
| 12 |
import org.h2.value.DataType; |
| 13 |
import org.h2.value.TypeInfo; |
| 14 |
import org.h2.value.Value; |
| 15 |
import org.h2.value.ValueInterval; |
| 16 |
import org.h2.value.ValueNull; |
| 17 |
import org.h2.value.ValueTimeTimeZone; |
| 18 |
import org.h2.value.ValueTimestamp; |
| 19 |
import org.h2.value.ValueTimestampTimeZone; |
| 20 |
|
| 21 |
/** |
| 22 |
* A time zone specification (AT { TIME ZONE | LOCAL }). |
| 23 |
*/ |
| 24 |
public final class TimeZoneOperation extends Operation1_2 { |
| 25 |
|
| 26 |
public TimeZoneOperation(Expression left, Expression right) { |
| 27 |
super(left, right); |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 32 |
left.getSQL(builder, sqlFlags, AUTO_PARENTHESES).append(" AT "); |
| 33 |
if (right != null) { |
| 34 |
right.getSQL(builder.append("TIME ZONE "), sqlFlags, AUTO_PARENTHESES); |
| 35 |
} else { |
| 36 |
builder.append("LOCAL"); |
| 37 |
} |
| 38 |
return builder; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public Value getValue(SessionLocal session) { |
| 43 |
Value l = left.getValue(session); |
| 44 |
Value a = l.convertTo(type, session); |
| 45 |
if (a == ValueNull.INSTANCE) { |
| 46 |
return ValueNull.INSTANCE; |
| 47 |
} |
| 48 |
Value b; |
| 49 |
if (right == null) { |
| 50 |
int t = l.getValueType(); |
| 51 |
if (t == Value.TIME || t == Value.TIMESTAMP) { |
| 52 |
// Already in time zone of the session |
| 53 |
return a; |
| 54 |
} |
| 55 |
b = null; |
| 56 |
} else { |
| 57 |
b = right.getValue(session); |
| 58 |
if (b == ValueNull.INSTANCE) { |
| 59 |
return ValueNull.INSTANCE; |
| 60 |
} |
| 61 |
} |
| 62 |
if (a.getValueType() == Value.TIMESTAMP_TZ) { |
| 63 |
ValueTimestampTimeZone v = (ValueTimestampTimeZone) a; |
| 64 |
long dateValue = v.getDateValue(); |
| 65 |
long timeNanos = v.getTimeNanos(); |
| 66 |
int offsetSeconds = v.getTimeZoneOffsetSeconds(); |
| 67 |
int newOffset = b != null // |
| 68 |
? parseTimeZone(b, dateValue, timeNanos, offsetSeconds, true) |
| 69 |
: session.currentTimeZone() |
| 70 |
.getTimeZoneOffsetUTC(DateTimeUtils.getEpochSeconds(dateValue, timeNanos, offsetSeconds)); |
| 71 |
if (offsetSeconds != newOffset) { |
| 72 |
a = DateTimeUtils.timestampTimeZoneAtOffset(dateValue, timeNanos, offsetSeconds, newOffset); |
| 73 |
} |
| 74 |
} else { |
| 75 |
ValueTimeTimeZone v = (ValueTimeTimeZone) a; |
| 76 |
long timeNanos = v.getNanos(); |
| 77 |
int offsetSeconds = v.getTimeZoneOffsetSeconds(); |
| 78 |
int newOffset = b != null |
| 79 |
? parseTimeZone(b, DateTimeUtils.EPOCH_DATE_VALUE, timeNanos, offsetSeconds, false) |
| 80 |
: session.currentTimeZone().getTimeZoneOffsetUTC(DateTimeUtils |
| 81 |
.getEpochSeconds(session.currentTimestamp().getDateValue(), timeNanos, offsetSeconds)); |
| 82 |
if (offsetSeconds != newOffset) { |
| 83 |
timeNanos += (newOffset - offsetSeconds) * DateTimeUtils.NANOS_PER_SECOND; |
| 84 |
a = ValueTimeTimeZone.fromNanos(DateTimeUtils.normalizeNanosOfDay(timeNanos), newOffset); |
| 85 |
} |
| 86 |
} |
| 87 |
return a; |
| 88 |
} |
| 89 |
|
| 90 |
private static int parseTimeZone(Value b, long dateValue, long timeNanos, int offsetSeconds, |
| 91 |
boolean allowTimeZoneName) { |
| 92 |
if (DataType.isCharacterStringType(b.getValueType())) { |
| 93 |
TimeZoneProvider timeZone; |
| 94 |
try { |
| 95 |
timeZone = TimeZoneProvider.ofId(b.getString()); |
| 96 |
} catch (RuntimeException ex) { |
| 97 |
throw DbException.getInvalidValueException("time zone", b.getTraceSQL()); |
| 98 |
} |
| 99 |
if (!allowTimeZoneName && !timeZone.hasFixedOffset()) { |
| 100 |
throw DbException.getInvalidValueException("time zone", b.getTraceSQL()); |
| 101 |
} |
| 102 |
return timeZone.getTimeZoneOffsetUTC(DateTimeUtils.getEpochSeconds(dateValue, timeNanos, offsetSeconds)); |
| 103 |
} |
| 104 |
return parseInterval(b); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Parses a daytime interval as time zone offset. |
| 109 |
* |
| 110 |
* @param interval the interval |
| 111 |
* @return the time zone offset in seconds |
| 112 |
*/ |
| 113 |
public static int parseInterval(Value interval) { |
| 114 |
ValueInterval i = (ValueInterval) interval.convertTo(TypeInfo.TYPE_INTERVAL_HOUR_TO_SECOND); |
| 115 |
long h = i.getLeading(), seconds = i.getRemaining(); |
| 116 |
if (h > 18 || h == 18 && seconds != 0 || seconds % DateTimeUtils.NANOS_PER_SECOND != 0) { |
| 117 |
throw DbException.getInvalidValueException("time zone", i.getTraceSQL()); |
| 118 |
} |
| 119 |
int newOffset = (int) (h * 3_600 + seconds / DateTimeUtils.NANOS_PER_SECOND); |
| 120 |
if (i.isNegative()) { |
| 121 |
newOffset = -newOffset; |
| 122 |
} |
| 123 |
return newOffset; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public Expression optimize(SessionLocal session) { |
| 128 |
left = left.optimize(session); |
| 129 |
if (right != null) { |
| 130 |
right = right.optimize(session); |
| 131 |
} |
| 132 |
TypeInfo type = left.getType(); |
| 133 |
int valueType = Value.TIMESTAMP_TZ, scale = ValueTimestamp.MAXIMUM_SCALE; |
| 134 |
int lType = type.getValueType(); |
| 135 |
switch (lType) { |
| 136 |
case Value.TIMESTAMP: |
| 137 |
case Value.TIMESTAMP_TZ: |
| 138 |
scale = type.getScale(); |
| 139 |
break; |
| 140 |
case Value.TIME: |
| 141 |
case Value.TIME_TZ: |
| 142 |
valueType = Value.TIME_TZ; |
| 143 |
scale = type.getScale(); |
| 144 |
break; |
| 145 |
default: |
| 146 |
StringBuilder builder = left.getSQL(new StringBuilder(), TRACE_SQL_FLAGS, AUTO_PARENTHESES); |
| 147 |
int offset = builder.length(); |
| 148 |
builder.append(" AT "); |
| 149 |
if (right != null) { |
| 150 |
right.getSQL(builder.append("TIME ZONE "), TRACE_SQL_FLAGS, AUTO_PARENTHESES); |
| 151 |
} else { |
| 152 |
builder.append("LOCAL"); |
| 153 |
} |
| 154 |
throw DbException.getSyntaxError(builder.toString(), offset, "time, timestamp"); |
| 155 |
} |
| 156 |
this.type = TypeInfo.getTypeInfo(valueType, -1, scale, null); |
| 157 |
if (left.isConstant() && (lType == Value.TIME_TZ || lType == Value.TIMESTAMP_TZ) && right != null |
| 158 |
&& right.isConstant()) { |
| 159 |
return ValueExpression.get(getValue(session)); |
| 160 |
} |
| 161 |
return this; |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 166 |
if (visitor.getType() == ExpressionVisitor.DETERMINISTIC) { |
| 167 |
if (right == null) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
int lType = left.getType().getValueType(); |
| 171 |
if (lType == Value.TIME || lType == Value.TIMESTAMP) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
} |
| 175 |
return left.isEverything(visitor) && (right == null || right.isEverything(visitor)); |
| 176 |
} |
| 177 |
|
| 178 |
} |