| 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 java.util.Arrays; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.function.CastSpecification; |
| 12 |
import org.h2.expression.function.ConcatFunction; |
| 13 |
import org.h2.value.DataType; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueArray; |
| 17 |
import org.h2.value.ValueNull; |
| 18 |
import org.h2.value.ValueVarbinary; |
| 19 |
import org.h2.value.ValueVarchar; |
| 20 |
|
| 21 |
/** |
| 22 |
* Character string concatenation as in {@code 'Hello' || 'World'}, binary |
| 23 |
* string concatenation as in {@code X'01' || X'AB'} or an array concatenation |
| 24 |
* as in {@code ARRAY[1, 2] || 3}. |
| 25 |
*/ |
| 26 |
public final class ConcatenationOperation extends OperationN { |
| 27 |
|
| 28 |
public ConcatenationOperation() { |
| 29 |
super(new Expression[4]); |
| 30 |
} |
| 31 |
|
| 32 |
public ConcatenationOperation(Expression op1, Expression op2) { |
| 33 |
super(new Expression[] { op1, op2 }); |
| 34 |
argsCount = 2; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public boolean needParentheses() { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 44 |
for (int i = 0, l = args.length; i < l; i++) { |
| 45 |
if (i > 0) { |
| 46 |
builder.append(" || "); |
| 47 |
} |
| 48 |
args[i].getSQL(builder, sqlFlags, AUTO_PARENTHESES); |
| 49 |
} |
| 50 |
return builder; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Value getValue(SessionLocal session) { |
| 55 |
int l = args.length; |
| 56 |
if (l == 2) { |
| 57 |
Value v1 = args[0].getValue(session); |
| 58 |
v1 = v1.convertTo(type, session); |
| 59 |
if (v1 == ValueNull.INSTANCE) { |
| 60 |
return ValueNull.INSTANCE; |
| 61 |
} |
| 62 |
Value v2 = args[1].getValue(session); |
| 63 |
v2 = v2.convertTo(type, session); |
| 64 |
if (v2 == ValueNull.INSTANCE) { |
| 65 |
return ValueNull.INSTANCE; |
| 66 |
} |
| 67 |
return getValue(session, v1, v2); |
| 68 |
} |
| 69 |
return getValue(session, l); |
| 70 |
} |
| 71 |
|
| 72 |
private Value getValue(SessionLocal session, Value l, Value r) { |
| 73 |
int valueType = type.getValueType(); |
| 74 |
if (valueType == Value.VARCHAR) { |
| 75 |
String s1 = l.getString(), s2 = r.getString(); |
| 76 |
return ValueVarchar.get(new StringBuilder(s1.length() + s2.length()).append(s1).append(s2).toString()); |
| 77 |
} else if (valueType == Value.VARBINARY) { |
| 78 |
byte[] leftBytes = l.getBytesNoCopy(), rightBytes = r.getBytesNoCopy(); |
| 79 |
int leftLength = leftBytes.length, rightLength = rightBytes.length; |
| 80 |
byte[] bytes = Arrays.copyOf(leftBytes, leftLength + rightLength); |
| 81 |
System.arraycopy(rightBytes, 0, bytes, leftLength, rightLength); |
| 82 |
return ValueVarbinary.getNoCopy(bytes); |
| 83 |
} else { |
| 84 |
Value[] leftValues = ((ValueArray) l).getList(), rightValues = ((ValueArray) r).getList(); |
| 85 |
int leftLength = leftValues.length, rightLength = rightValues.length; |
| 86 |
Value[] values = Arrays.copyOf(leftValues, leftLength + rightLength); |
| 87 |
System.arraycopy(rightValues, 0, values, leftLength, rightLength); |
| 88 |
return ValueArray.get((TypeInfo) type.getExtTypeInfo(), values, session); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private Value getValue(SessionLocal session, int l) { |
| 93 |
Value[] values = new Value[l]; |
| 94 |
for (int i = 0; i < l; i++) { |
| 95 |
Value v = args[i].getValue(session).convertTo(type, session); |
| 96 |
if (v == ValueNull.INSTANCE) { |
| 97 |
return ValueNull.INSTANCE; |
| 98 |
} |
| 99 |
values[i] = v; |
| 100 |
} |
| 101 |
int valueType = type.getValueType(); |
| 102 |
if (valueType == Value.VARCHAR) { |
| 103 |
StringBuilder builder = new StringBuilder(); |
| 104 |
for (int i = 0; i < l; i++) { |
| 105 |
builder.append(values[i].getString()); |
| 106 |
} |
| 107 |
return ValueVarchar.get(builder.toString(), session); |
| 108 |
} else if (valueType == Value.VARBINARY) { |
| 109 |
int totalLength = 0; |
| 110 |
for (int i = 0; i < l; i++) { |
| 111 |
totalLength += values[i].getBytesNoCopy().length; |
| 112 |
} |
| 113 |
byte[] v = new byte[totalLength]; |
| 114 |
int offset = 0; |
| 115 |
for (int i = 0; i < l; i++) { |
| 116 |
byte[] a = values[i].getBytesNoCopy(); |
| 117 |
int length = a.length; |
| 118 |
System.arraycopy(a, 0, v, offset, length); |
| 119 |
offset += length; |
| 120 |
} |
| 121 |
return ValueVarbinary.getNoCopy(v); |
| 122 |
} else { |
| 123 |
int totalLength = 0; |
| 124 |
for (int i = 0; i < l; i++) { |
| 125 |
totalLength += ((ValueArray) values[i]).getList().length; |
| 126 |
} |
| 127 |
Value[] v = new Value[totalLength]; |
| 128 |
int offset = 0; |
| 129 |
for (int i = 0; i < l; i++) { |
| 130 |
Value[] a = ((ValueArray) values[i]).getList(); |
| 131 |
int length = a.length; |
| 132 |
System.arraycopy(a, 0, v, offset, length); |
| 133 |
offset += length; |
| 134 |
} |
| 135 |
return ValueArray.get((TypeInfo) type.getExtTypeInfo(), v, session); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public Expression optimize(SessionLocal session) { |
| 141 |
determineType(session); |
| 142 |
inlineArguments(); |
| 143 |
if (type.getValueType() == Value.VARCHAR && session.getMode().treatEmptyStringsAsNull) { |
| 144 |
return new ConcatFunction(ConcatFunction.CONCAT, args).optimize(session); |
| 145 |
} |
| 146 |
int l = args.length; |
| 147 |
boolean allConst = true, anyConst = false; |
| 148 |
for (int i = 0; i < l; i++) { |
| 149 |
if (args[i].isConstant()) { |
| 150 |
anyConst = true; |
| 151 |
} else { |
| 152 |
allConst = false; |
| 153 |
} |
| 154 |
} |
| 155 |
if (allConst) { |
| 156 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 157 |
} |
| 158 |
if (anyConst) { |
| 159 |
int offset = 0; |
| 160 |
for (int i = 0; i < l; i++) { |
| 161 |
Expression arg1 = args[i]; |
| 162 |
if (arg1.isConstant()) { |
| 163 |
Value v1 = arg1.getValue(session).convertTo(type, session); |
| 164 |
if (v1 == ValueNull.INSTANCE) { |
| 165 |
return TypedValueExpression.get(ValueNull.INSTANCE, type); |
| 166 |
} |
| 167 |
if (isEmpty(v1)) { |
| 168 |
continue; |
| 169 |
} |
| 170 |
for (Expression arg2; i + 1 < l && (arg2 = args[i + 1]).isConstant(); i++) { |
| 171 |
Value v2 = arg2.getValue(session).convertTo(type, session); |
| 172 |
if (v2 == ValueNull.INSTANCE) { |
| 173 |
return TypedValueExpression.get(ValueNull.INSTANCE, type); |
| 174 |
} |
| 175 |
if (!isEmpty(v2)) { |
| 176 |
v1 = getValue(session, v1, v2); |
| 177 |
} |
| 178 |
} |
| 179 |
arg1 = ValueExpression.get(v1); |
| 180 |
} |
| 181 |
args[offset++] = arg1; |
| 182 |
} |
| 183 |
if (offset == 1) { |
| 184 |
Expression arg = args[0]; |
| 185 |
TypeInfo argType = arg.getType(); |
| 186 |
if (TypeInfo.areSameTypes(type, argType)) { |
| 187 |
return arg; |
| 188 |
} |
| 189 |
return new CastSpecification(arg, type); |
| 190 |
} |
| 191 |
argsCount = offset; |
| 192 |
doneWithParameters(); |
| 193 |
} |
| 194 |
return this; |
| 195 |
} |
| 196 |
|
| 197 |
private void determineType(SessionLocal session) { |
| 198 |
int l = args.length; |
| 199 |
boolean anyArray = false, allBinary = true, allCharacter = true; |
| 200 |
for (int i = 0; i < l; i++) { |
| 201 |
Expression arg = args[i].optimize(session); |
| 202 |
args[i] = arg; |
| 203 |
int t = arg.getType().getValueType(); |
| 204 |
if (t == Value.ARRAY) { |
| 205 |
anyArray = true; |
| 206 |
allBinary = allCharacter = false; |
| 207 |
} else if (t == Value.NULL) { |
| 208 |
// Ignore NULL literals |
| 209 |
} else if (DataType.isBinaryStringType(t)) { |
| 210 |
allCharacter = false; |
| 211 |
} else if (DataType.isCharacterStringType(t)) { |
| 212 |
allBinary = false; |
| 213 |
} else { |
| 214 |
allBinary = allCharacter = false; |
| 215 |
} |
| 216 |
} |
| 217 |
if (anyArray) { |
| 218 |
type = TypeInfo.getTypeInfo(Value.ARRAY, -1, 0, TypeInfo.getHigherType(args).getExtTypeInfo()); |
| 219 |
} else if (allBinary) { |
| 220 |
long precision = getPrecision(0); |
| 221 |
for (int i = 1; i < l; i++) { |
| 222 |
precision = DataType.addPrecision(precision, getPrecision(i)); |
| 223 |
} |
| 224 |
type = TypeInfo.getTypeInfo(Value.VARBINARY, precision, 0, null); |
| 225 |
} else if (allCharacter) { |
| 226 |
long precision = getPrecision(0); |
| 227 |
for (int i = 1; i < l; i++) { |
| 228 |
precision = DataType.addPrecision(precision, getPrecision(i)); |
| 229 |
} |
| 230 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, precision, 0, null); |
| 231 |
} else { |
| 232 |
type = TypeInfo.TYPE_VARCHAR; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private long getPrecision(int i) { |
| 237 |
TypeInfo t = args[i].getType(); |
| 238 |
return t.getValueType() != Value.NULL ? t.getPrecision() : 0L; |
| 239 |
} |
| 240 |
|
| 241 |
private void inlineArguments() { |
| 242 |
int valueType = type.getValueType(); |
| 243 |
int l = args.length; |
| 244 |
int count = l; |
| 245 |
for (int i = 0; i < l; i++) { |
| 246 |
Expression arg = args[i]; |
| 247 |
if (arg instanceof ConcatenationOperation && arg.getType().getValueType() == valueType) { |
| 248 |
count += arg.getSubexpressionCount() - 1; |
| 249 |
} |
| 250 |
} |
| 251 |
if (count > l) { |
| 252 |
Expression[] newArguments = new Expression[count]; |
| 253 |
for (int i = 0, offset = 0; i < l; i++) { |
| 254 |
Expression arg = args[i]; |
| 255 |
if (arg instanceof ConcatenationOperation && arg.getType().getValueType() == valueType) { |
| 256 |
ConcatenationOperation c = (ConcatenationOperation) arg; |
| 257 |
Expression[] innerArgs = c.args; |
| 258 |
int innerLength = innerArgs.length; |
| 259 |
System.arraycopy(innerArgs, 0, newArguments, offset, innerLength); |
| 260 |
offset += innerLength; |
| 261 |
} else { |
| 262 |
newArguments[offset++] = arg; |
| 263 |
} |
| 264 |
} |
| 265 |
args = newArguments; |
| 266 |
argsCount = count; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
private static boolean isEmpty(Value v) { |
| 271 |
int valueType = v.getValueType(); |
| 272 |
if (valueType == Value.VARCHAR) { |
| 273 |
return v.getString().isEmpty(); |
| 274 |
} else if (valueType == Value.VARBINARY) { |
| 275 |
return v.getBytesNoCopy().length == 0; |
| 276 |
} else { |
| 277 |
return ((ValueArray) v).getList().length == 0; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
} |