Retrofit Like to be foldable but LikePattern not

Original commit: elastic/x-pack-elasticsearch@0438482def
This commit is contained in:
Costin Leau 2018-01-17 15:16:21 +02:00
parent a80e5b73c9
commit dcb71af151
3 changed files with 16 additions and 12 deletions

View File

@ -24,9 +24,14 @@ public class Like extends BinaryExpression {
return (LikePattern) super.right(); return (LikePattern) super.right();
} }
@Override
public boolean foldable() {
return left().foldable();
}
@Override @Override
public Object fold() { public Object fold() {
Pattern p = Pattern.compile(right().fold().toString()); Pattern p = Pattern.compile(right().asJavaRegex());
return p.matcher(left().fold().toString()).matches(); return p.matcher(left().fold().toString()).matches();
} }

View File

@ -69,17 +69,6 @@ public class LikePattern extends LeafExpression {
return indexNameWildcard; return indexNameWildcard;
} }
@Override
public boolean foldable() {
return false;
}
@Override
public Object fold() {
// being in Java, the Java regex is returned
return asJavaRegex();
}
@Override @Override
public boolean nullable() { public boolean nullable() {
return false; return false;

View File

@ -26,6 +26,9 @@ import org.elasticsearch.xpack.sql.expression.predicate.LessThanOrEqual;
import org.elasticsearch.xpack.sql.expression.predicate.Not; import org.elasticsearch.xpack.sql.expression.predicate.Not;
import org.elasticsearch.xpack.sql.expression.predicate.Or; import org.elasticsearch.xpack.sql.expression.predicate.Or;
import org.elasticsearch.xpack.sql.expression.predicate.Range; import org.elasticsearch.xpack.sql.expression.predicate.Range;
import org.elasticsearch.xpack.sql.expression.regex.Like;
import org.elasticsearch.xpack.sql.expression.regex.LikePattern;
import org.elasticsearch.xpack.sql.expression.regex.RLike;
import org.elasticsearch.xpack.sql.optimizer.Optimizer.BinaryComparisonSimplification; import org.elasticsearch.xpack.sql.optimizer.Optimizer.BinaryComparisonSimplification;
import org.elasticsearch.xpack.sql.optimizer.Optimizer.BooleanLiteralsOnTheRight; import org.elasticsearch.xpack.sql.optimizer.Optimizer.BooleanLiteralsOnTheRight;
import org.elasticsearch.xpack.sql.optimizer.Optimizer.BooleanSimplification; import org.elasticsearch.xpack.sql.optimizer.Optimizer.BooleanSimplification;
@ -236,6 +239,13 @@ public class OptimizerTests extends ESTestCase {
assertEquals(Literal.TRUE, new ConstantFolding().rule(new Not(EMPTY, Literal.FALSE))); assertEquals(Literal.TRUE, new ConstantFolding().rule(new Not(EMPTY, Literal.FALSE)));
} }
public void testConstantFoldingLikes() {
assertEquals(Literal.TRUE,
new ConstantFolding().rule(new Like(EMPTY, Literal.of(EMPTY, "test_emp"), new LikePattern(EMPTY, "test%", (char) 0))));
assertEquals(Literal.TRUE,
new ConstantFolding().rule(new RLike(EMPTY, Literal.of(EMPTY, "test_emp"), Literal.of(EMPTY, "test.emp"))));
}
public void testBinaryComparisonSimplification() { public void testBinaryComparisonSimplification() {
assertEquals(Literal.TRUE, new BinaryComparisonSimplification().rule(new Equals(EMPTY, L(5), L(5)))); assertEquals(Literal.TRUE, new BinaryComparisonSimplification().rule(new Equals(EMPTY, L(5), L(5))));
assertEquals(Literal.TRUE, new BinaryComparisonSimplification().rule(new GreaterThanOrEqual(EMPTY, L(5), L(5)))); assertEquals(Literal.TRUE, new BinaryComparisonSimplification().rule(new GreaterThanOrEqual(EMPTY, L(5), L(5))));