| 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.condition; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.expression.ExpressionVisitor; |
| 11 |
import org.h2.expression.ValueExpression; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.table.ColumnResolver; |
| 14 |
import org.h2.table.TableFilter; |
| 15 |
import org.h2.util.json.JSONBytesSource; |
| 16 |
import org.h2.util.json.JSONItemType; |
| 17 |
import org.h2.util.json.JSONStringSource; |
| 18 |
import org.h2.util.json.JSONValidationTarget; |
| 19 |
import org.h2.util.json.JSONValidationTargetWithUniqueKeys; |
| 20 |
import org.h2.util.json.JSONValidationTargetWithoutUniqueKeys; |
| 21 |
import org.h2.value.Value; |
| 22 |
import org.h2.value.ValueBoolean; |
| 23 |
import org.h2.value.ValueJson; |
| 24 |
import org.h2.value.ValueNull; |
| 25 |
|
| 26 |
/** |
| 27 |
* IS JSON predicate. |
| 28 |
*/ |
| 29 |
public final class IsJsonPredicate extends Condition { |
| 30 |
|
| 31 |
private Expression left; |
| 32 |
private final boolean not; |
| 33 |
private final boolean whenOperand; |
| 34 |
private final boolean withUniqueKeys; |
| 35 |
private final JSONItemType itemType; |
| 36 |
|
| 37 |
public IsJsonPredicate(Expression left, boolean not, boolean whenOperand, boolean withUniqueKeys, |
| 38 |
JSONItemType itemType) { |
| 39 |
this.left = left; |
| 40 |
this.whenOperand = whenOperand; |
| 41 |
this.not = not; |
| 42 |
this.withUniqueKeys = withUniqueKeys; |
| 43 |
this.itemType = itemType; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public boolean needParentheses() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 53 |
return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 58 |
builder.append(" IS"); |
| 59 |
if (not) { |
| 60 |
builder.append(" NOT"); |
| 61 |
} |
| 62 |
builder.append(" JSON"); |
| 63 |
switch (itemType) { |
| 64 |
case VALUE: |
| 65 |
break; |
| 66 |
case ARRAY: |
| 67 |
builder.append(" ARRAY"); |
| 68 |
break; |
| 69 |
case OBJECT: |
| 70 |
builder.append(" OBJECT"); |
| 71 |
break; |
| 72 |
case SCALAR: |
| 73 |
builder.append(" SCALAR"); |
| 74 |
break; |
| 75 |
default: |
| 76 |
throw DbException.getInternalError("itemType=" + itemType); |
| 77 |
} |
| 78 |
if (withUniqueKeys) { |
| 79 |
builder.append(" WITH UNIQUE KEYS"); |
| 80 |
} |
| 81 |
return builder; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public Expression optimize(SessionLocal session) { |
| 86 |
left = left.optimize(session); |
| 87 |
if (!whenOperand && left.isConstant()) { |
| 88 |
return ValueExpression.getBoolean(getValue(session)); |
| 89 |
} |
| 90 |
return this; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public Value getValue(SessionLocal session) { |
| 95 |
Value l = left.getValue(session); |
| 96 |
if (l == ValueNull.INSTANCE) { |
| 97 |
return ValueNull.INSTANCE; |
| 98 |
} |
| 99 |
return ValueBoolean.get(getValue(l)); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 104 |
if (!whenOperand) { |
| 105 |
return super.getWhenValue(session, left); |
| 106 |
} |
| 107 |
if (left == ValueNull.INSTANCE) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
return getValue(left); |
| 111 |
} |
| 112 |
|
| 113 |
private boolean getValue(Value left) { |
| 114 |
boolean result; |
| 115 |
switch (left.getValueType()) { |
| 116 |
case Value.VARBINARY: |
| 117 |
case Value.BINARY: |
| 118 |
case Value.BLOB: { |
| 119 |
byte[] bytes = left.getBytesNoCopy(); |
| 120 |
JSONValidationTarget target = withUniqueKeys ? new JSONValidationTargetWithUniqueKeys() |
| 121 |
: new JSONValidationTargetWithoutUniqueKeys(); |
| 122 |
try { |
| 123 |
result = itemType.includes(JSONBytesSource.parse(bytes, target)) ^ not; |
| 124 |
} catch (RuntimeException ex) { |
| 125 |
result = not; |
| 126 |
} |
| 127 |
break; |
| 128 |
} |
| 129 |
case Value.JSON: { |
| 130 |
JSONItemType valueItemType = ((ValueJson) left).getItemType(); |
| 131 |
if (!itemType.includes(valueItemType)) { |
| 132 |
result = not; |
| 133 |
break; |
| 134 |
} else if (!withUniqueKeys || valueItemType == JSONItemType.SCALAR) { |
| 135 |
result = !not; |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
//$FALL-THROUGH$ |
| 140 |
case Value.VARCHAR: |
| 141 |
case Value.VARCHAR_IGNORECASE: |
| 142 |
case Value.CHAR: |
| 143 |
case Value.CLOB: { |
| 144 |
String string = left.getString(); |
| 145 |
JSONValidationTarget target = withUniqueKeys ? new JSONValidationTargetWithUniqueKeys() |
| 146 |
: new JSONValidationTargetWithoutUniqueKeys(); |
| 147 |
try { |
| 148 |
result = itemType.includes(JSONStringSource.parse(string, target)) ^ not; |
| 149 |
} catch (RuntimeException ex) { |
| 150 |
result = not; |
| 151 |
} |
| 152 |
break; |
| 153 |
} |
| 154 |
default: |
| 155 |
result = not; |
| 156 |
} |
| 157 |
return result; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public boolean isWhenConditionOperand() { |
| 162 |
return whenOperand; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public Expression getNotIfPossible(SessionLocal session) { |
| 167 |
if (whenOperand) { |
| 168 |
return null; |
| 169 |
} |
| 170 |
return new IsJsonPredicate(left, !not, false, withUniqueKeys, itemType); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 175 |
left.setEvaluatable(tableFilter, b); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void updateAggregate(SessionLocal session, int stage) { |
| 180 |
left.updateAggregate(session, stage); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 185 |
left.mapColumns(resolver, level, state); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 190 |
return left.isEverything(visitor); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public int getCost() { |
| 195 |
int cost = left.getCost(); |
| 196 |
if (left.getType().getValueType() == Value.JSON && (!withUniqueKeys || itemType == JSONItemType.SCALAR)) { |
| 197 |
cost++; |
| 198 |
} else { |
| 199 |
cost += 10; |
| 200 |
} |
| 201 |
return cost; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public int getSubexpressionCount() { |
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public Expression getSubexpression(int index) { |
| 211 |
if (index == 0) { |
| 212 |
return left; |
| 213 |
} |
| 214 |
throw new IndexOutOfBoundsException(); |
| 215 |
} |
| 216 |
|
| 217 |
} |