| 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.engine.Mode.ModeEnum; |
| 13 |
import org.h2.expression.Expression; |
| 14 |
import org.h2.expression.TypedValueExpression; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.mvstore.db.Store; |
| 17 |
import org.h2.value.TypeInfo; |
| 18 |
import org.h2.value.Value; |
| 19 |
import org.h2.value.ValueArray; |
| 20 |
import org.h2.value.ValueBoolean; |
| 21 |
import org.h2.value.ValueCollectionBase; |
| 22 |
import org.h2.value.ValueNull; |
| 23 |
|
| 24 |
/** |
| 25 |
* An array function. |
| 26 |
*/ |
| 27 |
public final class ArrayFunction extends FunctionN { |
| 28 |
|
| 29 |
/** |
| 30 |
* TRIM_ARRAY(). |
| 31 |
*/ |
| 32 |
public static final int TRIM_ARRAY = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* ARRAY_CONTAINS() (non-standard). |
| 36 |
*/ |
| 37 |
public static final int ARRAY_CONTAINS = TRIM_ARRAY + 1; |
| 38 |
|
| 39 |
/** |
| 40 |
* ARRAY_SLICE() (non-standard). |
| 41 |
*/ |
| 42 |
public static final int ARRAY_SLICE = ARRAY_CONTAINS + 1; |
| 43 |
|
| 44 |
private static final String[] NAMES = { // |
| 45 |
"TRIM_ARRAY", "ARRAY_CONTAINS", "ARRAY_SLICE" // |
| 46 |
}; |
| 47 |
|
| 48 |
private final int function; |
| 49 |
|
| 50 |
public ArrayFunction(Expression arg1, Expression arg2, Expression arg3, int function) { |
| 51 |
super(arg3 == null ? new Expression[] { arg1, arg2 } : new Expression[] { arg1, arg2, arg3 }); |
| 52 |
this.function = function; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Value getValue(SessionLocal session) { |
| 57 |
Value v1 = args[0].getValue(session), v2 = args[1].getValue(session); |
| 58 |
switch (function) { |
| 59 |
case TRIM_ARRAY: { |
| 60 |
if (v2 == ValueNull.INSTANCE) { |
| 61 |
v1 = ValueNull.INSTANCE; |
| 62 |
break; |
| 63 |
} |
| 64 |
int trim = v2.getInt(); |
| 65 |
if (trim < 0) { |
| 66 |
// This exception should be thrown even when array is null |
| 67 |
throw DbException.get(ErrorCode.ARRAY_ELEMENT_ERROR_2, Integer.toString(trim), // |
| 68 |
"0..CARDINALITY(array)"); |
| 69 |
} |
| 70 |
if (v1 == ValueNull.INSTANCE) { |
| 71 |
break; |
| 72 |
} |
| 73 |
final ValueArray array = v1.convertToAnyArray(session); |
| 74 |
Value[] elements = array.getList(); |
| 75 |
int length = elements.length; |
| 76 |
if (trim > length) { |
| 77 |
throw DbException.get(ErrorCode.ARRAY_ELEMENT_ERROR_2, Integer.toString(trim), "0.." + length); |
| 78 |
} else if (trim == 0) { |
| 79 |
v1 = array; |
| 80 |
} else { |
| 81 |
v1 = ValueArray.get(array.getComponentType(), Arrays.copyOf(elements, length - trim), session); |
| 82 |
} |
| 83 |
break; |
| 84 |
} |
| 85 |
case ARRAY_CONTAINS: { |
| 86 |
int t = v1.getValueType(); |
| 87 |
if (t == Value.ARRAY || t == Value.ROW) { |
| 88 |
Value[] list = ((ValueCollectionBase) v1).getList(); |
| 89 |
v1 = ValueBoolean.FALSE; |
| 90 |
for (Value v : list) { |
| 91 |
if (session.areEqual(v, v2)) { |
| 92 |
v1 = ValueBoolean.TRUE; |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
} else { |
| 97 |
v1 = ValueNull.INSTANCE; |
| 98 |
} |
| 99 |
break; |
| 100 |
} |
| 101 |
case ARRAY_SLICE: { |
| 102 |
Value v3; |
| 103 |
if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE |
| 104 |
|| (v3 = args[2].getValue(session)) == ValueNull.INSTANCE) { |
| 105 |
v1 = ValueNull.INSTANCE; |
| 106 |
break; |
| 107 |
} |
| 108 |
ValueArray array = v1.convertToAnyArray(session); |
| 109 |
// SQL is 1-based |
| 110 |
int index1 = v2.getInt() - 1; |
| 111 |
// 1-based and inclusive as postgreSQL (-1+1) |
| 112 |
int index2 = v3.getInt(); |
| 113 |
// https://www.postgresql.org/docs/current/arrays.html#ARRAYS-ACCESSING |
| 114 |
// For historical reasons postgreSQL ignore invalid indexes |
| 115 |
final boolean isPG = session.getMode().getEnum() == ModeEnum.PostgreSQL; |
| 116 |
if (index1 > index2) { |
| 117 |
v1 = isPG ? ValueArray.get(array.getComponentType(), Value.EMPTY_VALUES, session) : ValueNull.INSTANCE; |
| 118 |
break; |
| 119 |
} |
| 120 |
if (index1 < 0) { |
| 121 |
if (isPG) { |
| 122 |
index1 = 0; |
| 123 |
} else { |
| 124 |
v1 = ValueNull.INSTANCE; |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
if (index2 > array.getList().length) { |
| 129 |
if (isPG) { |
| 130 |
index2 = array.getList().length; |
| 131 |
} else { |
| 132 |
v1 = ValueNull.INSTANCE; |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
v1 = ValueArray.get(array.getComponentType(), Arrays.copyOfRange(array.getList(), index1, index2), // |
| 137 |
session); |
| 138 |
break; |
| 139 |
} |
| 140 |
default: |
| 141 |
throw DbException.getInternalError("function=" + function); |
| 142 |
} |
| 143 |
return v1; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public Expression optimize(SessionLocal session) { |
| 148 |
boolean allConst = optimizeArguments(session, true); |
| 149 |
switch (function) { |
| 150 |
case TRIM_ARRAY: |
| 151 |
case ARRAY_SLICE: { |
| 152 |
Expression arg = args[0]; |
| 153 |
type = arg.getType(); |
| 154 |
int t = type.getValueType(); |
| 155 |
if (t != Value.ARRAY && t != Value.NULL) { |
| 156 |
throw Store.getInvalidExpressionTypeException(getName() + " array argument", arg); |
| 157 |
} |
| 158 |
break; |
| 159 |
} |
| 160 |
case ARRAY_CONTAINS: |
| 161 |
type = TypeInfo.TYPE_BOOLEAN; |
| 162 |
break; |
| 163 |
default: |
| 164 |
throw DbException.getInternalError("function=" + function); |
| 165 |
} |
| 166 |
if (allConst) { |
| 167 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 168 |
} |
| 169 |
return this; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public String getName() { |
| 174 |
return NAMES[function]; |
| 175 |
} |
| 176 |
|
| 177 |
} |