1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.expression;
import java.sql.SQLException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.Session;
import org.h2.index.IndexCondition;
import org.h2.message.Message;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.StringUtils;
import org.h2.value.CompareMode;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
import org.h2.value.ValueString;
/**
* Pattern matching comparison expression: WHERE NAME LIKE ?
*/
public class CompareLike extends Condition {
private static final int MATCH = 0, ONE = 1, ANY = 2;
private final CompareMode compareMode;
private final boolean regexp;
private Expression left;
private Expression right;
private Expression escape;
private boolean isInit;
private char[] pattern;
private String patternString;
private Pattern patternRegexp;
private int[] types;
private int patternLength;
private boolean ignoreCase;
public CompareLike(CompareMode compareMode, Expression left, Expression right, Expression escape, boolean regexp) {
this.compareMode = compareMode;
this.regexp = regexp;
this.left = left;
this.right = right;
this.escape = escape;
}
public String getSQL() {
String sql;
if (regexp) {
sql = left.getSQL() + " REGEXP " + right.getSQL();
} else {
sql = left.getSQL() + " LIKE " + right.getSQL();
if (escape != null) {
sql += " ESCAPE " + escape.getSQL();
}
}
return "(" + sql + ")";
}
public Expression optimize(Session session) throws SQLException {
left = left.optimize(session);
right = right.optimize(session);
if (left.getType() == Value.STRING_IGNORECASE) {
ignoreCase = true;
}
if (left.isValueSet()) {
Value l = left.getValue(session);
if (l == ValueNull.INSTANCE) {
// NULL LIKE something > NULL
return ValueExpression.NULL;
}
}
if (escape != null) {
escape = escape.optimize(session);
}
if (right.isValueSet() && (escape == null || escape.isValueSet())) {
if (left.isValueSet()) {
return ValueExpression.get(getValue(session));
}
Value r = right.getValue(session);
if (r == ValueNull.INSTANCE) {
// something LIKE NULL > NULL
return ValueExpression.NULL;
}
Value e = escape == null ? null : escape.getValue(session);
if (e == ValueNull.INSTANCE) {
return ValueExpression.NULL;
}
String p = r.getString();
initPattern(p, getEscapeChar(e));
if ("%".equals(p)) {
// optimization for X LIKE '%': convert to X IS NOT NULL
return new Comparison(session, Comparison.IS_NOT_NULL, left, null).optimize(session);
}
if (isFullMatch()) {
// optimization for X LIKE 'Hello': convert to X = 'Hello'
Value value = ValueString.get(patternString);
Expression expr = ValueExpression.get(value);
return new Comparison(session, Comparison.EQUAL, left, expr).optimize(session);
}
isInit = true;
}
return this;
}
private char getEscapeChar(Value e) {
if (e == null) {
return Constants.DEFAULT_ESCAPE_CHAR;
}
String es = e.getString();
char esc;
if (es == null || es.length() == 0) {
esc = Constants.DEFAULT_ESCAPE_CHAR;
} else {
esc = es.charAt(0);
}
return esc;
}
public void createIndexConditions(Session session, TableFilter filter) throws SQLException {
if (regexp) {
return;
}
if (!(left instanceof ExpressionColumn)) {
return;
}
ExpressionColumn l = (ExpressionColumn) left;
if (filter != l.getTableFilter()) {
return;
}
// parameters are always evaluatable, but
// we need to check the actual value now
// (at prepare time)
// otherwise we would need to prepare at execute time,
// which is maybe slower (but maybe not in this case!)
// TODO optimizer: like: check what other databases do
if (!right.isValueSet()) {
return;
}
if (escape != null && !escape.isValueSet()) {
return;
}
String p = right.getValue(session).getString();
Value e = escape == null ? null : escape.getValue(session);
if (e == ValueNull.INSTANCE) {
// should already be optimized
throw Message.getInternalError();
}
initPattern(p, getEscapeChar(e));
if (patternLength <= 0 || types[0] != MATCH) {
// can't use an index
return;
}
int dataType = l.getColumn().getType();
if (dataType != Value.STRING && dataType != Value.STRING_IGNORECASE && dataType != Value.STRING_FIXED) {
// column is not a varchar - can't use the index
return;
}
int maxMatch = 0;
StringBuffer buff = new StringBuffer();
while (maxMatch < patternLength && types[maxMatch] == MATCH) {
buff.append(pattern[maxMatch++]);
}
String begin = buff.toString();
if (maxMatch == patternLength) {
filter.addIndexCondition(new IndexCondition(Comparison.EQUAL, l, ValueExpression
.get(ValueString.get(begin))));
} else {
// TODO check if this is correct according to Unicode rules (code
// points)
String end;
if (begin.length() > 0) {
filter.addIndexCondition(new IndexCondition(Comparison.BIGGER_EQUAL, l, ValueExpression.get(ValueString
.get(begin))));
char next = begin.charAt(begin.length() - 1);
// search the 'next' unicode character (or at least a character
// that is higher)
for (int i = 1; i < 2000; i++) {
end = begin.substring(0, begin.length() - 1) + (char) (next + i);
if (compareMode.compareString(begin, end, ignoreCase) == -1) {
filter.addIndexCondition(new IndexCondition(Comparison.SMALLER, l, ValueExpression
.get(ValueString.get(end))));
break;
}
}
}
}
}
public Value getValue(Session session) throws SQLException {
Value l = left.getValue(session);
if (l == ValueNull.INSTANCE) {
return l;
}
if (!isInit) {
Value r = right.getValue(session);
if (r == ValueNull.INSTANCE) {
return r;
}
String p = r.getString();
Value e = escape == null ? null : escape.getValue(session);
if (e == ValueNull.INSTANCE) {
return ValueNull.INSTANCE;
}
initPattern(p, getEscapeChar(e));
}
String value = l.getString();
boolean result;
if (regexp) {
// result = patternRegexp.matcher(value).matches();
result = patternRegexp.matcher(value).find();
} else {
result = compareAt(value, 0, 0, value.length());
}
return ValueBoolean.get(result);
}
private boolean compare(String s, int pi, int si) {
// TODO check if this is correct according to Unicode rules (code points)
return compareMode.equalsChars(patternString, pi, s, si, ignoreCase);
}
private boolean compareAt(String s, int pi, int si, int sLen) {
for (; pi < patternLength; pi++) {
int type = types[pi];
switch (type) {
case MATCH:
if ((si >= sLen) || !compare(s, pi, si++)) {
return false;
}
break;
case ONE:
if (si++ >= sLen) {
return false;
}
break;
case ANY:
if (++pi >= patternLength) {
return true;
}
while (si < sLen) {
if (compare(s, pi, si) && compareAt(s, pi, si, sLen)) {
return true;
}
si++;
}
return false;
default:
throw Message.getInternalError("type=" + type);
}
}
return si == sLen;
}
/**
* Test if the value matches the pattern.
*
* @param pattern the pattern
* @param value the value
* @param escape the escape character
* @return true if the value matches
*/
public boolean test(String pattern, String value, char escape) throws SQLException {
initPattern(pattern, escape);
return compareAt(value, 0, 0, value.length());
}
private void initPattern(String p, char escape) throws SQLException {
if (regexp) {
patternString = p;
try {
if (ignoreCase) {
patternRegexp = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
} else {
patternRegexp = Pattern.compile(p);
}
} catch (PatternSyntaxException e) {
throw Message.getSQLException(ErrorCode.LIKE_ESCAPE_ERROR_1, new String[]{p}, e);
}
return;
}
patternLength = 0;
if (p == null) {
types = null;
pattern = null;
return;
}
int len = p.length();
pattern = new char[len];
types = new int[len];
boolean lastAny = false;
for (int i = 0; i < len; i++) {
char c = p.charAt(i);
int type;
if (escape == c) {
if (i >= len - 1) {
throw Message.getSQLException(ErrorCode.LIKE_ESCAPE_ERROR_1, StringUtils.addAsterisk(p, i));
}
c = p.charAt(++i);
if (c != '_' && c != '%' && c != escape) {
throw Message.getSQLException(ErrorCode.LIKE_ESCAPE_ERROR_1, StringUtils.addAsterisk(p, i));
}
type = MATCH;
lastAny = false;
} else if (c == '%') {
if (lastAny) {
continue;
}
type = ANY;
lastAny = true;
} else if (c == '_') {
type = ONE;
} else {
type = MATCH;
lastAny = false;
}
types[patternLength] = type;
pattern[patternLength++] = c;
}
for (int i = 0; i < patternLength - 1; i++) {
if ((types[i] == ANY) && (types[i + 1] == ONE)) {
types[i] = ONE;
types[i + 1] = ANY;
}
}
patternString = new String(pattern, 0, patternLength);
}
private boolean isFullMatch() {
if (types == null) {
return false;
}
for (int i = 0; i < types.length; i++) {
if (types[i] != MATCH) {
return false;
}
}
return true;
}
public void mapColumns(ColumnResolver resolver, int level) throws SQLException {
left.mapColumns(resolver, level);
right.mapColumns(resolver, level);
if (escape != null) {
escape.mapColumns(resolver, level);
}
}
public void setEvaluatable(TableFilter tableFilter, boolean b) {
left.setEvaluatable(tableFilter, b);
right.setEvaluatable(tableFilter, b);
if (escape != null) {
escape.setEvaluatable(tableFilter, b);
}
}
public void updateAggregate(Session session) throws SQLException {
left.updateAggregate(session);
right.updateAggregate(session);
if (escape != null) {
escape.updateAggregate(session);
}
}
public boolean isEverything(ExpressionVisitor visitor) {
return left.isEverything(visitor) && right.isEverything(visitor)
&& (escape == null || escape.isEverything(visitor));
}
public int getCost() {
return left.getCost() + right.getCost() + 3;
}
}