Skip to content

Commit 13f37d5

Browse files
morckxkupietz
authored andcommitted
Allow comparison operators also for integers
Resolves #218 Change-Id: I4317c348c7ecbeb9112578facf19f546cff89227 Reviewed-on: https://korap.ids-mannheim.de/gerrit/c/KorAP/Koral/+/9098 Reviewed-by: Marc Kupietz <[email protected]>
1 parent 7a4bff7 commit 13f37d5

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryProcessor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ else if (nodeCat.equals("constraint")) {
134134
String match = operatorNode.getText();
135135
term.put("match", "match:" + interpretMatchOperator(match));
136136

137-
if (!checkOperatorValueConformance(term)) {
137+
if (!inferValueTypeAndCheckOperatorConformance(term)) {
138138
ArrayList<Object> errors = new ArrayList<>(3);
139139
errors.add(StatusCodes.INCOMPATIBLE_OPERATOR_AND_OPERAND);
140140
errors.add("Operator "+match+" is not acceptable.");
@@ -168,7 +168,7 @@ else if (nodeCat.equals("dateConstraint")) {
168168
term.putAll(parseValue(dateNode));
169169
String match = dateOpNode.getText();
170170
term.put("match", "match:" + interpretMatchOperator(match));
171-
if (!checkOperatorValueConformance(term)) {
171+
if (!inferValueTypeAndCheckOperatorConformance(term)) {
172172
requestMap = new HashMap<String, Object>();
173173
return;
174174
}
@@ -295,10 +295,13 @@ else if (flag.contains("I"))
295295

296296
/**
297297
* Checks whether the combination of operator and value is legal
298-
* (inequation operators <,>,<=,>= may only be used with dates).
298+
* (inequation operators <,>,<=,>= may only be used with dates and integers).
299299
*/
300-
private boolean checkOperatorValueConformance (
300+
private boolean inferValueTypeAndCheckOperatorConformance(
301301
Map<String, Object> term) {
302+
if ((term.get("type") == null) && ((String) term.get("value")).matches("[0-9]+")) {
303+
term.put("type", "type:integer");
304+
}
302305
String match = (String) term.get("match");
303306
String type = (String) term.get("type");
304307
if (type == null || type.equals("type:regex")) {

src/test/java/de/ids_mannheim/korap/query/test/collection/CollectionQueryProcessorTest.java

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ public void testTwoConjuncts () throws JsonProcessingException, IOException {
380380
.asText());
381381
assertEquals("pubDate", res.at("/collection/operands/1/key").asText());
382382
assertEquals("2014", res.at("/collection/operands/1/value").asText());
383-
assertTrue(res.at("/collection/operands/1/type")
384-
.isMissingNode());
383+
assertEquals("type:integer", res.at("/collection/operands/1/type").asText());
385384
assertEquals("match:eq", res.at("/collection/operands/1/match")
386385
.asText());
387386
assertTrue(res.at("/warnings/0").isMissingNode());
@@ -671,6 +670,80 @@ public void testMixed () throws JsonProcessingException, IOException {
671670
}
672671

673672

673+
@Test
674+
public void testIntegerConstraints() throws IOException {
675+
collection = "nTok != 200";
676+
qs.setQuery(query, ql);
677+
qs.setCollection(collection);
678+
res = mapper.readTree(qs.toJSON());
679+
assertEquals("koral:doc", res.at("/collection/@type").asText());
680+
assertEquals("nTok", res.at("/collection/key").asText());
681+
assertEquals("200", res.at("/collection/value").asText());
682+
assertEquals("type:integer", res.at("/collection/type").asText());
683+
assertEquals("match:ne", res.at("/collection/match").asText());
684+
685+
686+
collection = "nTok > 200";
687+
qs.setQuery(query, ql);
688+
qs.setCollection(collection);
689+
res = mapper.readTree(qs.toJSON());
690+
assertEquals("koral:doc", res.at("/collection/@type").asText());
691+
assertEquals("nTok", res.at("/collection/key").asText());
692+
assertEquals("200", res.at("/collection/value").asText());
693+
assertEquals("type:integer", res.at("/collection/type").asText());
694+
assertEquals("match:gt", res.at("/collection/match").asText());
695+
696+
collection = "nTok != 2000";
697+
qs.setQuery(query, ql);
698+
qs.setCollection(collection);
699+
res = mapper.readTree(qs.toJSON());
700+
assertEquals("koral:doc", res.at("/collection/@type").asText());
701+
assertEquals("nTok", res.at("/collection/key").asText());
702+
assertEquals("2000", res.at("/collection/value").asText());
703+
assertEquals("type:integer", res.at("/collection/type").asText());
704+
assertEquals("match:ne", res.at("/collection/match").asText());
705+
706+
collection = "nTok < 2000";
707+
qs.setQuery(query, ql);
708+
qs.setCollection(collection);
709+
res = mapper.readTree(qs.toJSON());
710+
assertEquals("koral:doc", res.at("/collection/@type").asText());
711+
assertEquals("nTok", res.at("/collection/key").asText());
712+
assertEquals("2000", res.at("/collection/value").asText());
713+
assertEquals("type:integer", res.at("/collection/type").asText());
714+
assertEquals("match:lt", res.at("/collection/match").asText());
715+
716+
collection = "nTok = 2000";
717+
qs.setQuery(query, ql);
718+
qs.setCollection(collection);
719+
res = mapper.readTree(qs.toJSON());
720+
assertEquals("koral:doc", res.at("/collection/@type").asText());
721+
assertEquals("nTok", res.at("/collection/key").asText());
722+
assertEquals("2000", res.at("/collection/value").asText());
723+
assertEquals("type:integer", res.at("/collection/type").asText());
724+
assertEquals("match:eq", res.at("/collection/match").asText());
725+
726+
collection = "nTok >= 2000";
727+
qs.setQuery(query, ql);
728+
qs.setCollection(collection);
729+
res = mapper.readTree(qs.toJSON());
730+
assertEquals("koral:doc", res.at("/collection/@type").asText());
731+
assertEquals("nTok", res.at("/collection/key").asText());
732+
assertEquals("2000", res.at("/collection/value").asText());
733+
assertEquals("type:integer", res.at("/collection/type").asText());
734+
assertEquals("match:geq", res.at("/collection/match").asText());
735+
736+
collection = "nTok <= 2000";
737+
qs.setQuery(query, ql);
738+
qs.setCollection(collection);
739+
res = mapper.readTree(qs.toJSON());
740+
assertEquals("koral:doc", res.at("/collection/@type").asText());
741+
assertEquals("nTok", res.at("/collection/key").asText());
742+
assertEquals("2000", res.at("/collection/value").asText());
743+
assertEquals("type:integer", res.at("/collection/type").asText());
744+
assertEquals("match:leq", res.at("/collection/match").asText());
745+
}
746+
674747
@Test
675748
public void testDateYear () throws JsonProcessingException, IOException {
676749
collection = "pubDate in 2000";
@@ -690,7 +763,7 @@ public void testDateYear () throws JsonProcessingException, IOException {
690763
assertEquals("koral:doc", res.at("/collection/@type").asText());
691764
assertEquals("pubDate", res.at("/collection/key").asText());
692765
assertEquals("2000", res.at("/collection/value").asText());
693-
assertTrue(res.at("/collection/type").isMissingNode());
766+
assertEquals("type:integer", res.at("/collection/type").asText());
694767
assertEquals("match:eq", res.at("/collection/match").asText());
695768

696769
collection = "pubDate since 2000";

0 commit comments

Comments
 (0)