forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EQL: Convert wildcards to LIKE in analyzer (elastic#51901)
* EQL: Convert wildcard comparisons to Like * EQL: Simplify wildcard handling, update tests * EQL: Lint fixes for Optimizer.java
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/OptimizerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.optimizer; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.eql.analysis.Analyzer; | ||
import org.elasticsearch.xpack.eql.analysis.PreAnalyzer; | ||
import org.elasticsearch.xpack.eql.analysis.Verifier; | ||
import org.elasticsearch.xpack.eql.expression.function.EqlFunctionRegistry; | ||
import org.elasticsearch.xpack.eql.parser.EqlParser; | ||
import org.elasticsearch.xpack.ql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.ql.expression.predicate.logical.And; | ||
import org.elasticsearch.xpack.ql.expression.predicate.logical.Not; | ||
import org.elasticsearch.xpack.ql.expression.predicate.regex.Like; | ||
import org.elasticsearch.xpack.ql.index.EsIndex; | ||
import org.elasticsearch.xpack.ql.index.IndexResolution; | ||
import org.elasticsearch.xpack.ql.plan.logical.Filter; | ||
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.ql.plan.logical.OrderBy; | ||
import org.elasticsearch.xpack.ql.type.EsField; | ||
import org.elasticsearch.xpack.ql.type.TypesTests; | ||
|
||
import java.util.Map; | ||
|
||
public class OptimizerTests extends ESTestCase { | ||
|
||
|
||
private static final String INDEX_NAME = "test"; | ||
private EqlParser parser = new EqlParser(); | ||
private IndexResolution index = loadIndexResolution("mapping-default.json"); | ||
private static Map<String, EsField> loadEqlMapping(String name) { | ||
return TypesTests.loadMapping(name); | ||
} | ||
|
||
private IndexResolution loadIndexResolution(String name) { | ||
return IndexResolution.valid(new EsIndex(INDEX_NAME, loadEqlMapping(name))); | ||
} | ||
|
||
private LogicalPlan accept(IndexResolution resolution, String eql) { | ||
PreAnalyzer preAnalyzer = new PreAnalyzer(); | ||
Analyzer analyzer = new Analyzer(new EqlFunctionRegistry(), new Verifier()); | ||
Optimizer optimizer = new Optimizer(); | ||
return optimizer.optimize(analyzer.analyze(preAnalyzer.preAnalyze(parser.createStatement(eql), resolution))); | ||
} | ||
|
||
private LogicalPlan accept(String eql) { | ||
return accept(index, eql); | ||
} | ||
|
||
|
||
public void testEqualsWildcard() { | ||
for (String q : new String[]{"foo where command_line == '* bar *'", "foo where '* bar *' == command_line"}) { | ||
LogicalPlan plan = accept(q); | ||
assertTrue(plan instanceof OrderBy); | ||
plan = ((OrderBy) plan).child(); | ||
assertTrue(plan instanceof Filter); | ||
|
||
Filter filter = (Filter) plan; | ||
And condition = (And) filter.condition(); | ||
assertTrue(condition.right() instanceof Like); | ||
|
||
Like like = (Like) condition.right(); | ||
assertEquals(((FieldAttribute) like.field()).name(), "command_line"); | ||
assertEquals(like.pattern().asJavaRegex(), "^.* bar .*$"); | ||
assertEquals(like.pattern().asLuceneWildcard(), "* bar *"); | ||
assertEquals(like.pattern().asIndexNameWildcard(), "* bar *"); | ||
} | ||
} | ||
|
||
public void testNotEqualsWildcard() { | ||
for (String q : new String[]{"foo where command_line != '* baz *'", "foo where '* baz *' != command_line"}) { | ||
|
||
LogicalPlan plan = accept(q); | ||
assertTrue(plan instanceof OrderBy); | ||
plan = ((OrderBy) plan).child(); | ||
assertTrue(plan instanceof Filter); | ||
|
||
Filter filter = (Filter) plan; | ||
And condition = (And) filter.condition(); | ||
assertTrue(condition.right() instanceof Not); | ||
|
||
Not not = (Not) condition.right(); | ||
Like like = (Like) not.field(); | ||
assertEquals(((FieldAttribute) like.field()).name(), "command_line"); | ||
assertEquals(like.pattern().asJavaRegex(), "^.* baz .*$"); | ||
assertEquals(like.pattern().asLuceneWildcard(), "* baz *"); | ||
assertEquals(like.pattern().asIndexNameWildcard(), "* baz *"); | ||
} | ||
} | ||
|
||
public void testWildcardEscapes() { | ||
LogicalPlan plan = accept("foo where command_line == '* %bar_ * \\\\ \\n \\r \\t'"); | ||
assertTrue(plan instanceof OrderBy); | ||
plan = ((OrderBy) plan).child(); | ||
assertTrue(plan instanceof Filter); | ||
|
||
Filter filter = (Filter) plan; | ||
And condition = (And) filter.condition(); | ||
assertTrue(condition.right() instanceof Like); | ||
|
||
Like like = (Like) condition.right(); | ||
assertEquals(((FieldAttribute) like.field()).name(), "command_line"); | ||
assertEquals(like.pattern().asJavaRegex(), "^.* %bar_ .* \\\\ \n \r \t$"); | ||
assertEquals(like.pattern().asLuceneWildcard(), "* %bar_ * \\\\ \n \r \t"); | ||
assertEquals(like.pattern().asIndexNameWildcard(), "* %bar_ * \\ \n \r \t"); | ||
} | ||
} |