| 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 java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.expression.ExpressionColumn; |
| 13 |
import org.h2.expression.ExpressionList; |
| 14 |
import org.h2.expression.ValueExpression; |
| 15 |
import org.h2.index.IndexCondition; |
| 16 |
import org.h2.table.TableFilter; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueBoolean; |
| 19 |
import org.h2.value.ValueNull; |
| 20 |
import org.h2.value.ValueRow; |
| 21 |
|
| 22 |
/** |
| 23 |
* Null predicate (IS [NOT] NULL). |
| 24 |
*/ |
| 25 |
public final class NullPredicate extends SimplePredicate { |
| 26 |
|
| 27 |
private boolean optimized; |
| 28 |
|
| 29 |
public NullPredicate(Expression left, boolean not, boolean whenOperand) { |
| 30 |
super(left, not, whenOperand); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 35 |
return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 40 |
return builder.append(not ? " IS NOT NULL" : " IS NULL"); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public Expression optimize(SessionLocal session) { |
| 45 |
if (optimized) { |
| 46 |
return this; |
| 47 |
} |
| 48 |
Expression o = super.optimize(session); |
| 49 |
if (o != this) { |
| 50 |
return o; |
| 51 |
} |
| 52 |
optimized = true; |
| 53 |
if (!whenOperand && left instanceof ExpressionList) { |
| 54 |
ExpressionList list = (ExpressionList) left; |
| 55 |
if (!list.isArray()) { |
| 56 |
for (int i = 0, count = list.getSubexpressionCount(); i < count; i++) { |
| 57 |
if (list.getSubexpression(i).isNullConstant()) { |
| 58 |
if (not) { |
| 59 |
return ValueExpression.FALSE; |
| 60 |
} |
| 61 |
ArrayList<Expression> newList = new ArrayList<>(count - 1); |
| 62 |
for (int j = 0; j < i; j++) { |
| 63 |
newList.add(list.getSubexpression(j)); |
| 64 |
} |
| 65 |
for (int j = i + 1; j < count; j++) { |
| 66 |
Expression e = list.getSubexpression(j); |
| 67 |
if (!e.isNullConstant()) { |
| 68 |
newList.add(e); |
| 69 |
} |
| 70 |
} |
| 71 |
left = newList.size() == 1 ? newList.get(0) // |
| 72 |
: new ExpressionList(newList.toArray(new Expression[0]), false); |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
return this; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Value getValue(SessionLocal session) { |
| 83 |
return ValueBoolean.get(getValue(left.getValue(session))); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 88 |
if (!whenOperand) { |
| 89 |
return super.getWhenValue(session, left); |
| 90 |
} |
| 91 |
return getValue(left); |
| 92 |
} |
| 93 |
|
| 94 |
private boolean getValue(Value left) { |
| 95 |
if (left.getType().getValueType() == Value.ROW) { |
| 96 |
for (Value v : ((ValueRow) left).getList()) { |
| 97 |
if (v != ValueNull.INSTANCE ^ not) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
return left == ValueNull.INSTANCE ^ not; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public Expression getNotIfPossible(SessionLocal session) { |
| 108 |
if (whenOperand) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
Expression o = optimize(session); |
| 112 |
if (o != this) { |
| 113 |
return o.getNotIfPossible(session); |
| 114 |
} |
| 115 |
switch (left.getType().getValueType()) { |
| 116 |
case Value.UNKNOWN: |
| 117 |
case Value.ROW: |
| 118 |
return null; |
| 119 |
} |
| 120 |
return new NullPredicate(left, !not, false); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 125 |
if (not || whenOperand || !filter.getTable().isQueryComparable()) { |
| 126 |
return; |
| 127 |
} |
| 128 |
if (left instanceof ExpressionColumn) { |
| 129 |
createNullIndexCondition(filter, (ExpressionColumn) left); |
| 130 |
} else if (left instanceof ExpressionList) { |
| 131 |
ExpressionList list = (ExpressionList) left; |
| 132 |
if (!list.isArray()) { |
| 133 |
for (int i = 0, count = list.getSubexpressionCount(); i < count; i++) { |
| 134 |
Expression e = list.getSubexpression(i); |
| 135 |
if (e instanceof ExpressionColumn) { |
| 136 |
createNullIndexCondition(filter, (ExpressionColumn) e); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private static void createNullIndexCondition(TableFilter filter, ExpressionColumn c) { |
| 144 |
/* |
| 145 |
* Columns with row value data type aren't valid, but perform such check |
| 146 |
* to be sure. |
| 147 |
*/ |
| 148 |
if (filter == c.getTableFilter() && c.getType().getValueType() != Value.ROW) { |
| 149 |
filter.addIndexCondition(IndexCondition.get(Comparison.EQUAL_NULL_SAFE, c, ValueExpression.NULL)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
} |