| 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 java.util.Arrays; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.TypedValueExpression; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.value.DataType; |
| 16 |
import org.h2.value.TypeInfo; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueNull; |
| 19 |
import org.h2.value.ValueVarbinary; |
| 20 |
import org.h2.value.ValueVarchar; |
| 21 |
|
| 22 |
/** |
| 23 |
* A SUBSTRING function. |
| 24 |
*/ |
| 25 |
public final class SubstringFunction extends FunctionN { |
| 26 |
|
| 27 |
public SubstringFunction() { |
| 28 |
super(new Expression[3]); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public Value getValue(SessionLocal session, Value v1, Value v2, Value v3) { |
| 33 |
if (type.getValueType() == Value.VARBINARY) { |
| 34 |
byte[] s = v1.getBytesNoCopy(); |
| 35 |
int sl = s.length; |
| 36 |
int start = v2.getInt(); |
| 37 |
// These compatibility conditions violate the Standard |
| 38 |
if (start == 0) { |
| 39 |
start = 1; |
| 40 |
} else if (start < 0) { |
| 41 |
start = sl + start + 1; |
| 42 |
} |
| 43 |
int end = v3 == null ? Math.max(sl + 1, start) : start + v3.getInt(); |
| 44 |
// SQL Standard requires "data exception - substring error" when |
| 45 |
// end < start but H2 does not throw it for compatibility |
| 46 |
start = Math.max(start, 1); |
| 47 |
end = Math.min(end, sl + 1); |
| 48 |
if (start > sl || end <= start) { |
| 49 |
return ValueVarbinary.EMPTY; |
| 50 |
} |
| 51 |
start--; |
| 52 |
end--; |
| 53 |
if (start == 0 && end == s.length) { |
| 54 |
return v1.convertTo(TypeInfo.TYPE_VARBINARY); |
| 55 |
} |
| 56 |
return ValueVarbinary.getNoCopy(Arrays.copyOfRange(s, start, end)); |
| 57 |
} else { |
| 58 |
String s = v1.getString(); |
| 59 |
int sl = s.length(); |
| 60 |
int start = v2.getInt(); |
| 61 |
// These compatibility conditions violate the Standard |
| 62 |
if (start == 0) { |
| 63 |
start = 1; |
| 64 |
} else if (start < 0) { |
| 65 |
start = sl + start + 1; |
| 66 |
} |
| 67 |
int end = v3 == null ? Math.max(sl + 1, start) : start + v3.getInt(); |
| 68 |
// SQL Standard requires "data exception - substring error" when |
| 69 |
// end < start but H2 does not throw it for compatibility |
| 70 |
start = Math.max(start, 1); |
| 71 |
end = Math.min(end, sl + 1); |
| 72 |
if (start > sl || end <= start) { |
| 73 |
return session.getMode().treatEmptyStringsAsNull ? ValueNull.INSTANCE : ValueVarchar.EMPTY; |
| 74 |
} |
| 75 |
return ValueVarchar.get(s.substring(start - 1, end - 1), null); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public Expression optimize(SessionLocal session) { |
| 81 |
boolean allConst = optimizeArguments(session, true); |
| 82 |
int len = args.length; |
| 83 |
if (len < 2 || len > 3) { |
| 84 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), "2..3"); |
| 85 |
} |
| 86 |
TypeInfo argType = args[0].getType(); |
| 87 |
long p = argType.getPrecision(); |
| 88 |
Expression arg = args[1]; |
| 89 |
Value v; |
| 90 |
if (arg.isConstant() && (v = arg.getValue(session)) != ValueNull.INSTANCE) { |
| 91 |
// if only two arguments are used, |
| 92 |
// subtract offset from first argument length |
| 93 |
p -= v.getLong() - 1; |
| 94 |
} |
| 95 |
if (args.length == 3) { |
| 96 |
arg = args[2]; |
| 97 |
if (arg.isConstant() && (v = arg.getValue(session)) != ValueNull.INSTANCE) { |
| 98 |
// if the third argument is constant it is at most this value |
| 99 |
p = Math.min(p, v.getLong()); |
| 100 |
} |
| 101 |
} |
| 102 |
p = Math.max(0, p); |
| 103 |
type = TypeInfo.getTypeInfo( |
| 104 |
DataType.isBinaryStringType(argType.getValueType()) ? Value.VARBINARY : Value.VARCHAR, p, 0, null); |
| 105 |
if (allConst) { |
| 106 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 107 |
} |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 113 |
args[0].getUnenclosedSQL(builder.append(getName()).append('('), sqlFlags); |
| 114 |
args[1].getUnenclosedSQL(builder.append(" FROM "), sqlFlags); |
| 115 |
if (args.length > 2) { |
| 116 |
args[2].getUnenclosedSQL(builder.append(" FOR "), sqlFlags); |
| 117 |
} |
| 118 |
return builder.append(')'); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public String getName() { |
| 123 |
return "SUBSTRING"; |
| 124 |
} |
| 125 |
|
| 126 |
} |