forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionBuilder.java
373 lines (297 loc) · 11.9 KB
/
ExpressionBuilder.java
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
package org.infinispan.objectfilter.impl.syntax.parser;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import org.infinispan.objectfilter.impl.logging.Log;
import org.infinispan.objectfilter.impl.ql.PropertyPath;
import org.infinispan.objectfilter.impl.ql.QueryRendererDelegate;
import org.infinispan.objectfilter.impl.syntax.AggregationExpr;
import org.infinispan.objectfilter.impl.syntax.AndExpr;
import org.infinispan.objectfilter.impl.syntax.BetweenExpr;
import org.infinispan.objectfilter.impl.syntax.BooleanExpr;
import org.infinispan.objectfilter.impl.syntax.ComparisonExpr;
import org.infinispan.objectfilter.impl.syntax.ConstantBooleanExpr;
import org.infinispan.objectfilter.impl.syntax.ConstantValueExpr;
import org.infinispan.objectfilter.impl.syntax.FullTextBoostExpr;
import org.infinispan.objectfilter.impl.syntax.FullTextOccurExpr;
import org.infinispan.objectfilter.impl.syntax.FullTextRangeExpr;
import org.infinispan.objectfilter.impl.syntax.FullTextRegexpExpr;
import org.infinispan.objectfilter.impl.syntax.FullTextTermExpr;
import org.infinispan.objectfilter.impl.syntax.IsNullExpr;
import org.infinispan.objectfilter.impl.syntax.LikeExpr;
import org.infinispan.objectfilter.impl.syntax.NotExpr;
import org.infinispan.objectfilter.impl.syntax.OrExpr;
import org.infinispan.objectfilter.impl.syntax.PropertyValueExpr;
import org.jboss.logging.Logger;
/**
* Builder for the creation of WHERE/HAVING clause filters targeting a single entity.
* <p/>
* Implemented as a stack of {@link LazyBooleanExpr}s which allows to add elements to the constructed query in a
* uniform manner while traversing through the original query parse tree.
*
* @author anistor@redhat.com
* @since 9.0
*/
final class ExpressionBuilder<TypeMetadata> {
private static final Log log = Logger.getMessageLogger(Log.class, ExpressionBuilder.class.getName());
private final ObjectPropertyHelper<TypeMetadata> propertyHelper;
private TypeMetadata entityType;
/**
* Keep track of all the parent expressions ({@code AND}, {@code OR}, {@code NOT}) of the WHERE/HAVING clause of the
* built query.
*/
private final Deque<LazyBooleanExpr> stack = new ArrayDeque<>();
ExpressionBuilder(ObjectPropertyHelper<TypeMetadata> propertyHelper) {
this.propertyHelper = propertyHelper;
}
public void setEntityType(TypeMetadata entityType) {
this.entityType = entityType;
stack.push(new LazyRootBooleanExpr());
}
public void addFullTextTerm(PropertyPath<?> propertyPath, Object comparisonObject, Integer fuzzySlop) {
push(new FullTextTermExpr(makePropertyValueExpr(propertyPath), comparisonObject, fuzzySlop));
}
public void addFullTextRegexp(PropertyPath<?> propertyPath, String regexp) {
push(new FullTextRegexpExpr(makePropertyValueExpr(propertyPath), regexp));
}
public void addFullTextRange(PropertyPath<?> propertyPath, boolean includeLower, Object lower, Object upper, boolean includeUpper) {
push(new FullTextRangeExpr(makePropertyValueExpr(propertyPath), includeLower, lower, upper, includeUpper));
}
public void addComparison(PropertyPath<?> propertyPath, ComparisonExpr.Type comparisonType, Object value) {
Comparable typedValue = (Comparable) propertyHelper.convertToBackendType(entityType, propertyPath.asArrayPath(), value);
push(new ComparisonExpr(makePropertyValueExpr(propertyPath), new ConstantValueExpr(typedValue), comparisonType));
}
public void addRange(PropertyPath<?> propertyPath, Object lower, Object upper) {
Comparable lowerValue = (Comparable) propertyHelper.convertToBackendType(entityType, propertyPath.asArrayPath(), lower);
Comparable upperValue = (Comparable) propertyHelper.convertToBackendType(entityType, propertyPath.asArrayPath(), upper);
push(new BetweenExpr(makePropertyValueExpr(propertyPath), new ConstantValueExpr(lowerValue), new ConstantValueExpr(upperValue)));
}
public void addIn(PropertyPath<?> propertyPath, List<Object> values) {
PropertyValueExpr valueExpr = makePropertyValueExpr(propertyPath);
List<BooleanExpr> children = new ArrayList<>(values.size());
for (Object element : values) { //todo [anistor] need more efficient implementation
Comparable typedValue = (Comparable) propertyHelper.convertToBackendType(entityType, propertyPath.asArrayPath(), element);
ComparisonExpr booleanExpr = new ComparisonExpr(valueExpr, new ConstantValueExpr(typedValue), ComparisonExpr.Type.EQUAL);
children.add(booleanExpr);
}
if (children.size() == 1) {
// simplify INs with just one clause by removing the wrapper boolean expression
push(children.get(0));
} else {
push(new OrExpr(children));
}
}
public void addLike(PropertyPath<?> propertyPath, Object patternValue, Character escapeCharacter) {
// TODO [anistor] escapeCharacter is ignored for now
push(new LikeExpr(makePropertyValueExpr(propertyPath), patternValue));
}
public void addIsNull(PropertyPath<?> propertyPath) {
push(new IsNullExpr(makePropertyValueExpr(propertyPath)));
}
public void pushAnd() {
push(new LazyAndExpr());
}
public void pushOr() {
push(new LazyOrExpr());
}
public void pushNot() {
push(new LazyNegationExpr());
}
public void pushFullTextBoost(float boost) {
push(new LazyFTBoostExpr(boost));
}
public void pushFullTextOccur(QueryRendererDelegate.Occur occur) {
push(new LazyFTOccurExpr(occur));
}
private void push(BooleanExpr expr) {
push(new LazyLeafBooleanExpr(expr));
}
private void push(LazyBooleanExpr expr) {
// add as sub-expression to the current top expression
stack.peek().addChild(expr);
// push to expression stack if required
if (expr.isParent()) {
stack.push(expr);
}
}
public void pop() {
stack.pop();
}
public BooleanExpr build() {
return stack.getFirst().get();
}
private PropertyValueExpr makePropertyValueExpr(PropertyPath<?> propertyPath) {
//todo [anistor] 2 calls to propertyHelper .. very inefficient
boolean isRepeated = propertyHelper.isRepeatedProperty(entityType, propertyPath.asArrayPath());
Class<?> primitiveType = propertyHelper.getPrimitivePropertyType(entityType, propertyPath.asArrayPath());
if (propertyPath instanceof AggregationPropertyPath) {
return new AggregationExpr((AggregationPropertyPath) propertyPath, isRepeated, primitiveType);
} else {
return new PropertyValueExpr(propertyPath, isRepeated, primitiveType);
}
}
public void addConstantBoolean(boolean booleanConstant) {
push(ConstantBooleanExpr.forBoolean(booleanConstant));
}
/**
* Delays construction until {@link #get} is called.
*/
abstract static class LazyBooleanExpr {
/**
* Indicates whether this expression can have sub-expressions or not.
*/
public boolean isParent() {
return true;
}
/**
* Adds the given lazy expression to this.
*
* @param child the child to add
*/
public abstract void addChild(LazyBooleanExpr child);
/**
* Returns the query represented by this lazy expression. Contains the all sub-expressions if this is a parent.
*
* @return the expression represented by this lazy expression
*/
public abstract BooleanExpr get();
}
private static final class LazyLeafBooleanExpr extends LazyBooleanExpr {
private final BooleanExpr booleanExpr;
LazyLeafBooleanExpr(BooleanExpr booleanExpr) {
this.booleanExpr = booleanExpr;
}
@Override
public boolean isParent() {
return false;
}
@Override
public void addChild(LazyBooleanExpr child) {
throw new UnsupportedOperationException("Adding a sub-expression to a non-parent expression is illegal");
}
@Override
public BooleanExpr get() {
return booleanExpr;
}
}
private static final class LazyAndExpr extends LazyBooleanExpr {
private final List<LazyBooleanExpr> children = new ArrayList<>(3);
@Override
public void addChild(LazyBooleanExpr child) {
children.add(child);
}
@Override
public BooleanExpr get() {
if (children.isEmpty()) {
throw new IllegalStateException("A conjunction must have at least one child");
}
BooleanExpr firstChild = children.get(0).get();
if (children.size() == 1) {
return firstChild;
}
AndExpr andExpr = new AndExpr(firstChild);
for (int i = 1; i < children.size(); i++) {
BooleanExpr child = children.get(i).get();
andExpr.getChildren().add(child);
}
return andExpr;
}
}
private static final class LazyOrExpr extends LazyBooleanExpr {
private final List<LazyBooleanExpr> children = new ArrayList<>(3);
@Override
public void addChild(LazyBooleanExpr child) {
children.add(child);
}
@Override
public BooleanExpr get() {
if (children.isEmpty()) {
throw new IllegalStateException("A disjunction must have at least one child");
}
BooleanExpr firstChild = children.get(0).get();
if (children.size() == 1) {
return firstChild;
}
OrExpr orExpr = new OrExpr(firstChild);
for (int i = 1; i < children.size(); i++) {
BooleanExpr child = children.get(i).get();
orExpr.getChildren().add(child);
}
return orExpr;
}
}
private static final class LazyNegationExpr extends LazyBooleanExpr {
private LazyBooleanExpr child;
@Override
public void addChild(LazyBooleanExpr child) {
if (this.child != null) {
throw log.getNotMoreThanOnePredicateInNegationAllowedException(child);
}
this.child = child;
}
public LazyBooleanExpr getChild() {
return child;
}
@Override
public BooleanExpr get() {
return new NotExpr(getChild().get());
}
}
private static final class LazyFTBoostExpr extends LazyBooleanExpr {
private LazyBooleanExpr child;
private final float boost;
LazyFTBoostExpr(float boost) {
this.boost = boost;
}
@Override
public void addChild(LazyBooleanExpr child) {
if (this.child != null) {
throw log.getNotMoreThanOnePredicateInNegationAllowedException(child);
}
this.child = child;
}
public LazyBooleanExpr getChild() {
return child;
}
@Override
public BooleanExpr get() {
return new FullTextBoostExpr(getChild().get(), boost);
}
}
private static final class LazyFTOccurExpr extends LazyBooleanExpr {
private LazyBooleanExpr child;
private final QueryRendererDelegate.Occur occur;
LazyFTOccurExpr(QueryRendererDelegate.Occur occur) {
this.occur = occur;
}
@Override
public void addChild(LazyBooleanExpr child) {
if (this.child != null) {
throw log.getNotMoreThanOnePredicateInNegationAllowedException(child);
}
this.child = child;
}
public LazyBooleanExpr getChild() {
return child;
}
@Override
public BooleanExpr get() {
return new FullTextOccurExpr(getChild().get(), occur);
}
}
private static final class LazyRootBooleanExpr extends LazyBooleanExpr {
private LazyBooleanExpr child;
@Override
public void addChild(LazyBooleanExpr child) {
if (this.child != null) {
throw log.getNotMoreThanOnePredicateInRootOfWhereClauseAllowedException(child);
}
this.child = child;
}
@Override
public BooleanExpr get() {
return child == null ? null : child.get();
}
}
}