Skip to content

Commit 3b33207

Browse files
committed
Merge branch 'gh-4205_add_contains' of github.com:gchq/stroom into gh-4209_support_and_in_eval
2 parents 0d39a82 + a323a59 commit 3b33207

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2017 Crown Copyright
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package stroom.query.language.functions;
18+
19+
import stroom.query.language.functions.ref.StoredValues;
20+
import stroom.query.language.token.Param;
21+
22+
import java.text.ParseException;
23+
import java.util.function.Supplier;
24+
25+
@SuppressWarnings("unused") //Used by FunctionFactory
26+
@FunctionDef(
27+
name = Contains.NAME,
28+
commonCategory = FunctionCategory.STRING,
29+
commonReturnType = ValBoolean.class,
30+
commonReturnDescription = "True if string contains substring",
31+
signatures = @FunctionSignature(
32+
description = "Tests if inputString contains subString.",
33+
args = {
34+
@FunctionArg(
35+
name = "inputString",
36+
description = "The string to find subString in.",
37+
argType = ValString.class),
38+
@FunctionArg(
39+
name = "subString",
40+
description = "The string to find in inputString.",
41+
argType = ValString.class)
42+
}))
43+
class Contains extends AbstractManyChildFunction {
44+
45+
static final String NAME = "contains";
46+
private Generator gen;
47+
private boolean simple;
48+
49+
public Contains(final String name) {
50+
super(name, 2, 2);
51+
}
52+
53+
@Override
54+
public void setParams(final Param[] params) throws ParseException {
55+
super.setParams(params);
56+
57+
// See if this is a static computation.
58+
simple = true;
59+
for (Param param : params) {
60+
if (!(param instanceof Val)) {
61+
simple = false;
62+
break;
63+
}
64+
}
65+
66+
if (simple) {
67+
// Static computation.
68+
final String string = params[0].toString();
69+
final String substring = params[1].toString();
70+
final boolean result = string.contains(substring);
71+
gen = new StaticValueFunction(ValBoolean.create(result)).createGenerator();
72+
}
73+
}
74+
75+
@Override
76+
public Generator createGenerator() {
77+
if (gen != null) {
78+
return gen;
79+
}
80+
return super.createGenerator();
81+
}
82+
83+
@Override
84+
protected Generator createGenerator(final Generator[] childGenerators) {
85+
return new Gen(childGenerators);
86+
}
87+
88+
@Override
89+
public boolean hasAggregate() {
90+
if (simple) {
91+
return false;
92+
}
93+
return super.hasAggregate();
94+
}
95+
96+
private static final class Gen extends AbstractManyChildGenerator {
97+
98+
Gen(final Generator[] childGenerators) {
99+
super(childGenerators);
100+
}
101+
102+
@Override
103+
public Val eval(final StoredValues storedValues, final Supplier<ChildData> childDataSupplier) {
104+
final Val val = childGenerators[0].eval(storedValues, childDataSupplier);
105+
if (!val.type().isValue()) {
106+
return val;
107+
}
108+
final Val valSubString = childGenerators[1].eval(storedValues, childDataSupplier);
109+
if (!valSubString.type().isValue()) {
110+
return ValErr.wrap(valSubString);
111+
}
112+
113+
try {
114+
final String string = val.toString();
115+
final String substring = valSubString.toString();
116+
return ValBoolean.create(string.contains(substring));
117+
118+
} catch (final RuntimeException e) {
119+
return ValErr.create(e.getMessage());
120+
}
121+
}
122+
}
123+
}

stroom-query/stroom-query-language/src/main/java/stroom/query/language/functions/Match.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setParams(final Param[] params) throws ParseException {
6868
final String value = params[0].toString();
6969
final String regex = params[1].toString();
7070

71-
if (regex.length() == 0) {
71+
if (regex.isEmpty()) {
7272
throw new ParseException(
7373
"An empty regex has been defined for second argument of '" + name + "' function", 0);
7474
}
@@ -81,7 +81,7 @@ public void setParams(final Param[] params) throws ParseException {
8181
if (params[1] instanceof Val) {
8282
// Test regex is valid.
8383
final String regex = params[1].toString();
84-
if (regex.length() == 0) {
84+
if (regex.isEmpty()) {
8585
throw new ParseException(
8686
"An empty regex has been defined for second argument of '" + name + "' function", 0);
8787
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
* Issue **#4205** : Add `contains()` function.
2+
3+
4+
```sh
5+
# ********************************************************************************
6+
# Issue title: Add `contains()` function
7+
# Issue link: https://github.com/gchq/stroom/issues/4205
8+
# ********************************************************************************
9+
10+
# ONLY the top line will be included as a change entry in the CHANGELOG.
11+
# The entry should be in GitHub flavour markdown and should be written on a SINGLE
12+
# line with no hard breaks. You can have multiple change files for a single GitHub issue.
13+
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than
14+
# 'Fixed nasty bug'.
15+
#
16+
# Examples of acceptable entries are:
17+
#
18+
#
19+
# * Issue **123** : Fix bug with an associated GitHub issue in this repository
20+
#
21+
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository
22+
#
23+
# * Fix bug with no associated GitHub issue.
24+
```

0 commit comments

Comments
 (0)