| 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 org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.expression.TypedValueExpression; |
| 11 |
import org.h2.expression.ValueExpression; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.Domain; |
| 14 |
import org.h2.table.Column; |
| 15 |
import org.h2.util.DateTimeTemplate; |
| 16 |
import org.h2.util.HasSQL; |
| 17 |
import org.h2.value.DataType; |
| 18 |
import org.h2.value.TypeInfo; |
| 19 |
import org.h2.value.Value; |
| 20 |
import org.h2.value.ValueNull; |
| 21 |
import org.h2.value.ValueVarchar; |
| 22 |
|
| 23 |
/** |
| 24 |
* A cast specification. |
| 25 |
*/ |
| 26 |
public final class CastSpecification extends Function1_2 { |
| 27 |
|
| 28 |
private Domain domain; |
| 29 |
|
| 30 |
public CastSpecification(Expression arg, Column column, Expression template) { |
| 31 |
super(arg, template); |
| 32 |
type = column.getType(); |
| 33 |
domain = column.getDomain(); |
| 34 |
} |
| 35 |
|
| 36 |
public CastSpecification(Expression arg, Column column) { |
| 37 |
super(arg, null); |
| 38 |
type = column.getType(); |
| 39 |
domain = column.getDomain(); |
| 40 |
} |
| 41 |
|
| 42 |
public CastSpecification(Expression arg, TypeInfo type) { |
| 43 |
super(arg, null); |
| 44 |
this.type = type; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected Value getValue(SessionLocal session, Value v1, Value v2) { |
| 49 |
if (v2 != null) { |
| 50 |
v1 = getValueWithTemplate(v1, v2, session); |
| 51 |
} |
| 52 |
v1 = v1.castTo(type, session); |
| 53 |
if (domain != null) { |
| 54 |
domain.checkConstraints(session, v1); |
| 55 |
} |
| 56 |
return v1; |
| 57 |
} |
| 58 |
|
| 59 |
private Value getValueWithTemplate(Value v, Value template, SessionLocal session) { |
| 60 |
if (v == ValueNull.INSTANCE) { |
| 61 |
return ValueNull.INSTANCE; |
| 62 |
} |
| 63 |
int valueType = v.getValueType(); |
| 64 |
if (DataType.isDateTimeType(valueType)) { |
| 65 |
if (DataType.isCharacterStringType(type.getValueType())) { |
| 66 |
return ValueVarchar.get(DateTimeTemplate.of(template.getString()).format(v), session); |
| 67 |
} |
| 68 |
} else if (DataType.isCharacterStringType(valueType)) { |
| 69 |
if (DataType.isDateTimeType(type.getValueType())) { |
| 70 |
return DateTimeTemplate.of(template.getString()).parse(v.getString(), type, session); |
| 71 |
} |
| 72 |
} |
| 73 |
throw DbException.getUnsupportedException( |
| 74 |
type.getSQL(v.getType().getSQL(new StringBuilder("CAST with template from "), HasSQL.TRACE_SQL_FLAGS) |
| 75 |
.append(" to "), HasSQL.DEFAULT_SQL_FLAGS).toString()); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public Expression optimize(SessionLocal session) { |
| 80 |
left = left.optimize(session); |
| 81 |
if (right != null) { |
| 82 |
right = right.optimize(session); |
| 83 |
} |
| 84 |
if (left.isConstant() && (right == null || right.isConstant())) { |
| 85 |
Value v = getValue(session); |
| 86 |
if (v == ValueNull.INSTANCE || canOptimizeCast(left.getType().getValueType(), type.getValueType())) { |
| 87 |
return TypedValueExpression.get(v, type); |
| 88 |
} |
| 89 |
} |
| 90 |
return this; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public boolean isConstant() { |
| 95 |
return left instanceof ValueExpression && (right == null || right.isConstant()) |
| 96 |
&& canOptimizeCast(left.getType().getValueType(), type.getValueType()); |
| 97 |
} |
| 98 |
|
| 99 |
private static boolean canOptimizeCast(int src, int dst) { |
| 100 |
switch (src) { |
| 101 |
case Value.TIME: |
| 102 |
switch (dst) { |
| 103 |
case Value.TIME_TZ: |
| 104 |
case Value.TIMESTAMP: |
| 105 |
case Value.TIMESTAMP_TZ: |
| 106 |
return false; |
| 107 |
} |
| 108 |
break; |
| 109 |
case Value.TIME_TZ: |
| 110 |
switch (dst) { |
| 111 |
case Value.TIME: |
| 112 |
case Value.TIMESTAMP: |
| 113 |
case Value.TIMESTAMP_TZ: |
| 114 |
return false; |
| 115 |
} |
| 116 |
break; |
| 117 |
case Value.DATE: |
| 118 |
if (dst == Value.TIMESTAMP_TZ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
break; |
| 122 |
case Value.TIMESTAMP: |
| 123 |
switch (dst) { |
| 124 |
case Value.TIME_TZ: |
| 125 |
case Value.TIMESTAMP_TZ: |
| 126 |
return false; |
| 127 |
} |
| 128 |
break; |
| 129 |
case Value.TIMESTAMP_TZ: |
| 130 |
switch (dst) { |
| 131 |
case Value.TIME: |
| 132 |
case Value.DATE: |
| 133 |
case Value.TIMESTAMP: |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 142 |
builder.append("CAST("); |
| 143 |
left.getUnenclosedSQL(builder, left instanceof ValueExpression ? sqlFlags | NO_CASTS : sqlFlags) // |
| 144 |
.append(" AS "); |
| 145 |
(domain != null ? domain : type).getSQL(builder, sqlFlags); |
| 146 |
if (right != null) { |
| 147 |
right.getSQL(builder.append(" FORMAT "), sqlFlags); |
| 148 |
} |
| 149 |
return builder.append(')'); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public String getName() { |
| 154 |
return "CAST"; |
| 155 |
} |
| 156 |
|
| 157 |
} |