Skip to content

Commit

Permalink
Improved tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Oct 24, 2024
1 parent bc61c99 commit f4e6e68
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 183 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugin/kql/src/main/antlr/KqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ topLevelQuery
;

query
: <assoc=right> query operator=(AND | OR) query #booleanQuery
: <assoc=right> query operator=(AND | OR) query? #booleanQuery
| NOT subQuery=simpleQuery #notQuery
| simpleQuery #defaultQuery
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public QueryBuilder visitAndBooleanQuery(KqlBaseParser.BooleanQueryContext ctx)
}
}

return builder;
return rewriteConjonctionQuery(builder);
}

public QueryBuilder visitOrBooleanQuery(KqlBaseParser.BooleanQueryContext ctx) {
Expand All @@ -79,7 +79,7 @@ public QueryBuilder visitOrBooleanQuery(KqlBaseParser.BooleanQueryContext ctx) {
}
}

return builder;
return rewriteDisjunctionQuery(builder);
}

@Override
Expand Down Expand Up @@ -207,6 +207,16 @@ private QueryBuilder rewriteDisjunctionQuery(BoolQueryBuilder boolQueryBuilder)
return boolQueryBuilder.should().size() == 1 ? boolQueryBuilder.should().getFirst() : boolQueryBuilder;
}

private QueryBuilder rewriteConjonctionQuery(BoolQueryBuilder boolQueryBuilder) {
assert boolQueryBuilder.should().isEmpty() && boolQueryBuilder.filter().isEmpty() && boolQueryBuilder.mustNot().isEmpty();

if (boolQueryBuilder.must().isEmpty()) {
return new MatchNoneQueryBuilder();
}

return boolQueryBuilder.must().size() == 1 ? boolQueryBuilder.must().getFirst() : boolQueryBuilder;
}

private BiFunction<RangeQueryBuilder, String, RangeQueryBuilder> rangeOperation(Token operator) {
return switch (operator.getType()) {
case KqlBaseParser.OP_LESS -> RangeQueryBuilder::lt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ fie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 1, 0, 0, 0, 14, 80, 1, 0, 0, 0, 16, 82, 1, 0, 0, 0, 18, 96, 1, 0, 0, 0, 20, 103, 1, 0, 0, 0, 22, 111, 1, 0, 0, 0, 24, 120, 1, 0, 0, 0, 26, 28, 3, 2, 1, 0, 27, 26, 1, 0, 0, 0, 27, 28, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 30, 5, 0, 0, 1, 30, 1, 1, 0, 0, 0, 31, 32, 6, 1, -1, 0, 32, 33, 5, 4, 0, 0, 33, 36, 3, 4, 2, 0, 34, 36, 3, 4, 2, 0, 35, 31, 1, 0, 0, 0, 35, 34, 1, 0, 0, 0, 36, 42, 1, 0, 0, 0, 37, 38, 10, 3, 0, 0, 38, 39, 7, 0, 0, 0, 39, 41, 3, 2, 1, 3, 40, 37, 1, 0, 0, 0, 41, 44, 1, 0, 0, 0, 42, 40, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 3, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 45, 53, 3, 6, 3, 0, 46, 53, 3, 10, 5, 0, 47, 53, 3, 8, 4, 0, 48, 53, 3, 16, 8, 0, 49, 53, 3, 12, 6, 0, 50, 53, 3, 18, 9, 0, 51, 53, 3, 20, 10, 0, 52, 45, 1, 0, 0, 0, 52, 46, 1, 0, 0, 0, 52, 47, 1, 0, 0, 0, 52, 48, 1, 0, 0, 0, 52, 49, 1, 0, 0, 0, 52, 50, 1, 0, 0, 0, 52, 51, 1, 0, 0, 0, 53, 5, 1, 0, 0, 0, 54, 55, 3, 24, 12, 0, 55, 56, 5, 5, 0, 0, 56, 57, 5, 12, 0, 0, 57, 58, 3, 2, 1, 0, 58, 59, 5, 13, 0, 0, 59, 7, 1, 0, 0, 0, 60, 61, 5, 16, 0, 0, 61, 63, 5, 5, 0, 0, 62, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 64, 1, 0, 0, 0, 64, 65, 5, 16, 0, 0, 65, 9, 1, 0, 0, 0, 66, 67, 5, 10, 0, 0, 67, 68, 3, 2, 1, 0, 68, 69, 5, 11, 0, 0, 69, 11, 1, 0, 0, 0, 70, 71, 3, 24, 12, 0, 71, 72, 7, 1, 0, 0, 72, 73, 3, 14, 7, 0, 73, 13, 1, 0, 0, 0, 74, 76, 7, 2, 0, 0, 75, 74, 1, 0, 0, 0, 76, 77, 1, 0, 0, 0, 77, 75, 1, 0, 0, 0, 77, 78, 1, 0, 0, 0, 78, 81, 1, 0, 0, 0, 79, 81, 5, 15, 0, 0, 80, 75, 1, 0, 0, 0, 80, 79, 1, 0, 0, 0, 81, 15, 1, 0, 0, 0, 82, 83, 3, 24, 12, 0, 83, 84, 5, 5, 0, 0, 84, 85, 5, 16, 0, 0, 85, 17, 1, 0, 0, 0, 86, 87, 3, 24, 12, 0, 87, 88, 5, 5, 0, 0, 88, 89, 3, 22, 11, 0, 89, 97, 1, 0, 0, 0, 90, 91, 3, 24, 12, 0, 91, 92, 5, 5, 0, 0, 92, 93, 5, 10, 0, 0, 93, 94, 3, 22, 11, 0, 94, 95, 5, 11, 0, 0, 95, 97, 1, 0, 0, 0, 96, 86, 1, 0, 0, 0, 96, 90, 1, 0, 0, 0, 97, 19, 1, 0, 0, 0, 98, 104, 3, 22, 11, 0, 99, 100, 5, 10, 0, 0, 100, 101, 3, 22, 11, 0, 101, 102, 5, 11, 0, 0, 102, 104, 1, 0, 0, 0, 103, 98, 1, 0, 0, 0, 103, 99, 1, 0, 0, 0, 104, 21, 1, 0, 0, 0, 105, 107, 7, 2, 0, 0, 106, 105, 1, 0, 0, 0, 107, 108, 1, 0, 0, 0, 108, 106, 1, 0, 0, 0, 108, 109, 1, 0, 0, 0, 109, 112, 1, 0, 0, 0, 110, 112, 5, 15, 0, 0, 111, 106, 1, 0, 0, 0, 111, 110, 1, 0, 0, 0, 112, 23, 1, 0, 0, 0, 113, 115, 5, 14, 0, 0, 114, 113, 1, 0, 0, 0, 115, 116, 1, 0, 0, 0, 116, 114, 1, 0, 0, 0, 116, 117, 1, 0, 0, 0, 117, 121, 1, 0, 0, 0, 118, 121, 5, 15, 0, 0, 119, 121, 5, 16, 0, 0, 120, 114, 1, 0, 0, 0, 120, 118, 1, 0, 0, 0, 120, 119, 1, 0, 0, 0, 121, 25, 1, 0, 0, 0, 13, 27, 35, 42, 52, 62, 77, 80, 96, 103, 108, 111, 116, 120]
[4, 1, 16, 125, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 1, 0, 3, 0, 28, 8, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 36, 8, 1, 1, 1, 1, 1, 1, 1, 3, 1, 41, 8, 1, 5, 1, 43, 8, 1, 10, 1, 12, 1, 46, 9, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 55, 8, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 3, 4, 65, 8, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 7, 4, 7, 78, 8, 7, 11, 7, 12, 7, 79, 1, 7, 3, 7, 83, 8, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 99, 8, 9, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 3, 10, 106, 8, 10, 1, 11, 4, 11, 109, 8, 11, 11, 11, 12, 11, 110, 1, 11, 3, 11, 114, 8, 11, 1, 12, 4, 12, 117, 8, 12, 11, 12, 12, 12, 118, 1, 12, 1, 12, 3, 12, 123, 8, 12, 1, 12, 0, 1, 2, 13, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 0, 3, 1, 0, 2, 3, 1, 0, 6, 9, 2, 0, 14, 14, 16, 16, 131, 0, 27, 1, 0, 0, 0, 2, 35, 1, 0, 0, 0, 4, 54, 1, 0, 0, 0, 6, 56, 1, 0, 0, 0, 8, 64, 1, 0, 0, 0, 10, 68, 1, 0, 0, 0, 12, 72, 1, 0, 0, 0, 14, 82, 1, 0, 0, 0, 16, 84, 1, 0, 0, 0, 18, 98, 1, 0, 0, 0, 20, 105, 1, 0, 0, 0, 22, 113, 1, 0, 0, 0, 24, 122, 1, 0, 0, 0, 26, 28, 3, 2, 1, 0, 27, 26, 1, 0, 0, 0, 27, 28, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 30, 5, 0, 0, 1, 30, 1, 1, 0, 0, 0, 31, 32, 6, 1, -1, 0, 32, 33, 5, 4, 0, 0, 33, 36, 3, 4, 2, 0, 34, 36, 3, 4, 2, 0, 35, 31, 1, 0, 0, 0, 35, 34, 1, 0, 0, 0, 36, 44, 1, 0, 0, 0, 37, 38, 10, 3, 0, 0, 38, 40, 7, 0, 0, 0, 39, 41, 3, 2, 1, 0, 40, 39, 1, 0, 0, 0, 40, 41, 1, 0, 0, 0, 41, 43, 1, 0, 0, 0, 42, 37, 1, 0, 0, 0, 43, 46, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 3, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 47, 55, 3, 6, 3, 0, 48, 55, 3, 10, 5, 0, 49, 55, 3, 8, 4, 0, 50, 55, 3, 16, 8, 0, 51, 55, 3, 12, 6, 0, 52, 55, 3, 18, 9, 0, 53, 55, 3, 20, 10, 0, 54, 47, 1, 0, 0, 0, 54, 48, 1, 0, 0, 0, 54, 49, 1, 0, 0, 0, 54, 50, 1, 0, 0, 0, 54, 51, 1, 0, 0, 0, 54, 52, 1, 0, 0, 0, 54, 53, 1, 0, 0, 0, 55, 5, 1, 0, 0, 0, 56, 57, 3, 24, 12, 0, 57, 58, 5, 5, 0, 0, 58, 59, 5, 12, 0, 0, 59, 60, 3, 2, 1, 0, 60, 61, 5, 13, 0, 0, 61, 7, 1, 0, 0, 0, 62, 63, 5, 16, 0, 0, 63, 65, 5, 5, 0, 0, 64, 62, 1, 0, 0, 0, 64, 65, 1, 0, 0, 0, 65, 66, 1, 0, 0, 0, 66, 67, 5, 16, 0, 0, 67, 9, 1, 0, 0, 0, 68, 69, 5, 10, 0, 0, 69, 70, 3, 2, 1, 0, 70, 71, 5, 11, 0, 0, 71, 11, 1, 0, 0, 0, 72, 73, 3, 24, 12, 0, 73, 74, 7, 1, 0, 0, 74, 75, 3, 14, 7, 0, 75, 13, 1, 0, 0, 0, 76, 78, 7, 2, 0, 0, 77, 76, 1, 0, 0, 0, 78, 79, 1, 0, 0, 0, 79, 77, 1, 0, 0, 0, 79, 80, 1, 0, 0, 0, 80, 83, 1, 0, 0, 0, 81, 83, 5, 15, 0, 0, 82, 77, 1, 0, 0, 0, 82, 81, 1, 0, 0, 0, 83, 15, 1, 0, 0, 0, 84, 85, 3, 24, 12, 0, 85, 86, 5, 5, 0, 0, 86, 87, 5, 16, 0, 0, 87, 17, 1, 0, 0, 0, 88, 89, 3, 24, 12, 0, 89, 90, 5, 5, 0, 0, 90, 91, 3, 22, 11, 0, 91, 99, 1, 0, 0, 0, 92, 93, 3, 24, 12, 0, 93, 94, 5, 5, 0, 0, 94, 95, 5, 10, 0, 0, 95, 96, 3, 22, 11, 0, 96, 97, 5, 11, 0, 0, 97, 99, 1, 0, 0, 0, 98, 88, 1, 0, 0, 0, 98, 92, 1, 0, 0, 0, 99, 19, 1, 0, 0, 0, 100, 106, 3, 22, 11, 0, 101, 102, 5, 10, 0, 0, 102, 103, 3, 22, 11, 0, 103, 104, 5, 11, 0, 0, 104, 106, 1, 0, 0, 0, 105, 100, 1, 0, 0, 0, 105, 101, 1, 0, 0, 0, 106, 21, 1, 0, 0, 0, 107, 109, 7, 2, 0, 0, 108, 107, 1, 0, 0, 0, 109, 110, 1, 0, 0, 0, 110, 108, 1, 0, 0, 0, 110, 111, 1, 0, 0, 0, 111, 114, 1, 0, 0, 0, 112, 114, 5, 15, 0, 0, 113, 108, 1, 0, 0, 0, 113, 112, 1, 0, 0, 0, 114, 23, 1, 0, 0, 0, 115, 117, 5, 14, 0, 0, 116, 115, 1, 0, 0, 0, 117, 118, 1, 0, 0, 0, 118, 116, 1, 0, 0, 0, 118, 119, 1, 0, 0, 0, 119, 123, 1, 0, 0, 0, 120, 123, 5, 15, 0, 0, 121, 123, 5, 16, 0, 0, 122, 116, 1, 0, 0, 0, 122, 120, 1, 0, 0, 0, 122, 121, 1, 0, 0, 0, 123, 25, 1, 0, 0, 0, 14, 27, 35, 40, 44, 54, 64, 79, 82, 98, 105, 110, 113, 118, 122]
Loading

0 comments on commit f4e6e68

Please sign in to comment.