Skip to content

Commit 867c296

Browse files
authored
[ISSUE #6916] support startsWith and endsWith in sql filter. (#6915)
* support startsWith and endsWith in sql filter. * add tests * add tests
1 parent 9899be1 commit 867c296

File tree

6 files changed

+833
-379
lines changed

6 files changed

+833
-379
lines changed

filter/src/main/java/org/apache/rocketmq/filter/expression/ComparisonExpression.java

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,174 @@ public static BooleanExpression createNotContains(Expression left, String search
153153
return new NotContainsExpression(left, search);
154154
}
155155

156+
static class StartsWithExpression extends UnaryExpression implements BooleanExpression {
157+
158+
String search;
159+
160+
public StartsWithExpression(Expression right, String search) {
161+
super(right);
162+
this.search = search;
163+
}
164+
165+
public String getExpressionSymbol() {
166+
return "STARTSWITH";
167+
}
168+
169+
public Object evaluate(EvaluationContext message) throws Exception {
170+
171+
if (search == null || search.length() == 0) {
172+
return Boolean.FALSE;
173+
}
174+
175+
Object rv = this.getRight().evaluate(message);
176+
177+
if (rv == null) {
178+
return Boolean.FALSE;
179+
}
180+
181+
if (!(rv instanceof String)) {
182+
return Boolean.FALSE;
183+
}
184+
185+
return ((String)rv).startsWith(search) ? Boolean.TRUE : Boolean.FALSE;
186+
}
187+
188+
public boolean matches(EvaluationContext message) throws Exception {
189+
Object object = evaluate(message);
190+
return object != null && object == Boolean.TRUE;
191+
}
192+
}
193+
194+
static class NotStartsWithExpression extends UnaryExpression implements BooleanExpression {
195+
196+
String search;
197+
198+
public NotStartsWithExpression(Expression right, String search) {
199+
super(right);
200+
this.search = search;
201+
}
202+
203+
public String getExpressionSymbol() {
204+
return "NOT STARTSWITH";
205+
}
206+
207+
public Object evaluate(EvaluationContext message) throws Exception {
208+
209+
if (search == null || search.length() == 0) {
210+
return Boolean.FALSE;
211+
}
212+
213+
Object rv = this.getRight().evaluate(message);
214+
215+
if (rv == null) {
216+
return Boolean.FALSE;
217+
}
218+
219+
if (!(rv instanceof String)) {
220+
return Boolean.FALSE;
221+
}
222+
223+
return ((String)rv).startsWith(search) ? Boolean.FALSE : Boolean.TRUE;
224+
}
225+
226+
public boolean matches(EvaluationContext message) throws Exception {
227+
Object object = evaluate(message);
228+
return object != null && object == Boolean.TRUE;
229+
}
230+
}
231+
232+
public static BooleanExpression createStartsWith(Expression left, String search) {
233+
return new StartsWithExpression(left, search);
234+
}
235+
236+
public static BooleanExpression createNotStartsWith(Expression left, String search) {
237+
return new NotStartsWithExpression(left, search);
238+
}
239+
240+
static class EndsWithExpression extends UnaryExpression implements BooleanExpression {
241+
242+
String search;
243+
244+
public EndsWithExpression(Expression right, String search) {
245+
super(right);
246+
this.search = search;
247+
}
248+
249+
public String getExpressionSymbol() {
250+
return "ENDSWITH";
251+
}
252+
253+
public Object evaluate(EvaluationContext message) throws Exception {
254+
255+
if (search == null || search.length() == 0) {
256+
return Boolean.FALSE;
257+
}
258+
259+
Object rv = this.getRight().evaluate(message);
260+
261+
if (rv == null) {
262+
return Boolean.FALSE;
263+
}
264+
265+
if (!(rv instanceof String)) {
266+
return Boolean.FALSE;
267+
}
268+
269+
return ((String)rv).endsWith(search) ? Boolean.TRUE : Boolean.FALSE;
270+
}
271+
272+
public boolean matches(EvaluationContext message) throws Exception {
273+
Object object = evaluate(message);
274+
return object != null && object == Boolean.TRUE;
275+
}
276+
}
277+
278+
static class NotEndsWithExpression extends UnaryExpression implements BooleanExpression {
279+
280+
String search;
281+
282+
public NotEndsWithExpression(Expression right, String search) {
283+
super(right);
284+
this.search = search;
285+
}
286+
287+
public String getExpressionSymbol() {
288+
return "NOT ENDSWITH";
289+
}
290+
291+
public Object evaluate(EvaluationContext message) throws Exception {
292+
293+
if (search == null || search.length() == 0) {
294+
return Boolean.FALSE;
295+
}
296+
297+
Object rv = this.getRight().evaluate(message);
298+
299+
if (rv == null) {
300+
return Boolean.FALSE;
301+
}
302+
303+
if (!(rv instanceof String)) {
304+
return Boolean.FALSE;
305+
}
306+
307+
return ((String)rv).endsWith(search) ? Boolean.FALSE : Boolean.TRUE;
308+
}
309+
310+
public boolean matches(EvaluationContext message) throws Exception {
311+
Object object = evaluate(message);
312+
return object != null && object == Boolean.TRUE;
313+
}
314+
}
315+
316+
public static BooleanExpression createEndsWith(Expression left, String search) {
317+
return new EndsWithExpression(left, search);
318+
}
319+
320+
public static BooleanExpression createNotEndsWith(Expression left, String search) {
321+
return new NotEndsWithExpression(left, search);
322+
}
323+
156324
@SuppressWarnings({"rawtypes", "unchecked"})
157325
public static BooleanExpression createInFilter(Expression left, List elements) {
158326

0 commit comments

Comments
 (0)