Debian Codegraph @

src/main/org/h2/expression/condition/BetweenPredicate.java

raw
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.TypedValueExpression;
12 import org.h2.expression.ValueExpression;
13 import org.h2.table.ColumnResolver;
14 import org.h2.table.TableFilter;
15 import org.h2.value.TypeInfo;
16 import org.h2.value.Value;
17 import org.h2.value.ValueBoolean;
18 import org.h2.value.ValueNull;
19
20 /**
21 * BETWEEN predicate.
22 */
23 public final class BetweenPredicate extends Condition {
24
25 private Expression left;
26
27 private final boolean not;
28
29 private final boolean whenOperand;
30
31 private boolean symmetric;
32
33 private Expression a, b;
34
35 public BetweenPredicate(Expression left, boolean not, boolean whenOperand, boolean symmetric, Expression a,
36 Expression b) {
37 this.left = left;
38 this.not = not;
39 this.whenOperand = whenOperand;
40 this.symmetric = symmetric;
41 this.a = a;
42 this.b = b;
43 }
44
45 @Override
46 public boolean needParentheses() {
47 return true;
48 }
49
50 @Override
51 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
52 return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags);
53 }
54
55 @Override
56 public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) {
57 if (not) {
58 builder.append(" NOT");
59 }
60 builder.append(" BETWEEN ");
61 if (symmetric) {
62 builder.append("SYMMETRIC ");
63 }
64 a.getSQL(builder, sqlFlags, AUTO_PARENTHESES).append(" AND ");
65 return b.getSQL(builder, sqlFlags, AUTO_PARENTHESES);
66 }
67
68 @Override
69 public Expression optimize(SessionLocal session) {
70 left = left.optimize(session);
71 a = a.optimize(session);
72 b = b.optimize(session);
73 TypeInfo leftType = left.getType();
74 TypeInfo.checkComparable(leftType, a.getType());
75 TypeInfo.checkComparable(leftType, b.getType());
76 if (whenOperand) {
77 return this;
78 }
79 Value value = left.isConstant() ? left.getValue(session) : null,
80 aValue = a.isConstant() ? a.getValue(session) : null,
81 bValue = b.isConstant() ? b.getValue(session) : null;
82 if (value != null) {
83 if (value == ValueNull.INSTANCE) {
84 return TypedValueExpression.UNKNOWN;
85 }
86 if (aValue != null && bValue != null) {
87 return ValueExpression.getBoolean(getValue(session, value, aValue, bValue));
88 }
89 }
90 if (symmetric) {
91 if (aValue == ValueNull.INSTANCE || bValue == ValueNull.INSTANCE) {
92 return TypedValueExpression.UNKNOWN;
93 }
94 } else if (aValue == ValueNull.INSTANCE && bValue == ValueNull.INSTANCE) {
95 return TypedValueExpression.UNKNOWN;
96 }
97 if (aValue != null && bValue != null && session.compareWithNull(aValue, bValue, false) == 0) {
98 return new Comparison(not ? Comparison.NOT_EQUAL : Comparison.EQUAL, left, a, false).optimize(session);
99 }
100 return this;
101 }
102
103 @Override
104 public Value getValue(SessionLocal session) {
105 Value value = left.getValue(session);
106 if (value == ValueNull.INSTANCE) {
107 return ValueNull.INSTANCE;
108 }
109 return getValue(session, value, a.getValue(session), b.getValue(session));
110 }
111
112 @Override
113 public boolean getWhenValue(SessionLocal session, Value left) {
114 if (!whenOperand) {
115 return super.getWhenValue(session, left);
116 }
117 if (left == ValueNull.INSTANCE) {
118 return false;
119 }
120 return getValue(session, left, a.getValue(session), b.getValue(session)).isTrue();
121 }
122
123 private Value getValue(SessionLocal session, Value value, Value aValue, Value bValue) {
124 int cmp1 = session.compareWithNull(aValue, value, false);
125 int cmp2 = session.compareWithNull(value, bValue, false);
126 if (cmp1 == Integer.MIN_VALUE) {
127 return symmetric || cmp2 <= 0 ? ValueNull.INSTANCE : ValueBoolean.get(not);
128 } else if (cmp2 == Integer.MIN_VALUE) {
129 return symmetric || cmp1 <= 0 ? ValueNull.INSTANCE : ValueBoolean.get(not);
130 } else {
131 return ValueBoolean.get(not ^ //
132 (symmetric ? cmp1 <= 0 && cmp2 <= 0 || cmp1 >= 0 && cmp2 >= 0 : cmp1 <= 0 && cmp2 <= 0));
133 }
134 }
135
136 @Override
137 public boolean isWhenConditionOperand() {
138 return whenOperand;
139 }
140
141 @Override
142 public Expression getNotIfPossible(SessionLocal session) {
143 if (whenOperand) {
144 return null;
145 }
146 return new BetweenPredicate(left, !not, false, symmetric, a, b);
147 }
148
149 @Override
150 public void createIndexConditions(SessionLocal session, TableFilter filter) {
151 if (!not && !whenOperand && !symmetric) {
152 Comparison.createIndexConditions(filter, a, left, Comparison.SMALLER_EQUAL);
153 Comparison.createIndexConditions(filter, left, b, Comparison.SMALLER_EQUAL);
154 }
155 }
156
157 @Override
158 public void setEvaluatable(TableFilter tableFilter, boolean value) {
159 left.setEvaluatable(tableFilter, value);
160 a.setEvaluatable(tableFilter, value);
161 b.setEvaluatable(tableFilter, value);
162 }
163
164 @Override
165 public void updateAggregate(SessionLocal session, int stage) {
166 left.updateAggregate(session, stage);
167 a.updateAggregate(session, stage);
168 b.updateAggregate(session, stage);
169 }
170
171 @Override
172 public void mapColumns(ColumnResolver resolver, int level, int state) {
173 left.mapColumns(resolver, level, state);
174 a.mapColumns(resolver, level, state);
175 b.mapColumns(resolver, level, state);
176 }
177
178 @Override
179 public boolean isEverything(ExpressionVisitor visitor) {
180 return left.isEverything(visitor) && a.isEverything(visitor) && b.isEverything(visitor);
181 }
182
183 @Override
184 public int getCost() {
185 return left.getCost() + a.getCost() + b.getCost() + 1;
186 }
187
188 @Override
189 public int getSubexpressionCount() {
190 return 3;
191 }
192
193 @Override
194 public Expression getSubexpression(int index) {
195 switch (index) {
196 case 0:
197 return left;
198 case 1:
199 return a;
200 case 2:
201 return b;
202 default:
203 throw new IndexOutOfBoundsException();
204 }
205 }
206
207 }