|
12 | 12 |
|
13 | 13 | import org.apache.lucene.util.BytesRef; |
14 | 14 | import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 15 | +import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; |
| 16 | +import org.elasticsearch.xpack.esql.core.expression.Literal; |
| 17 | +import org.elasticsearch.xpack.esql.core.querydsl.query.WildcardQuery; |
15 | 18 | import org.elasticsearch.xpack.esql.core.tree.Source; |
16 | 19 | import org.elasticsearch.xpack.esql.core.type.DataType; |
| 20 | +import org.elasticsearch.xpack.esql.core.type.EsField; |
17 | 21 | import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase; |
18 | 22 | import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; |
| 23 | +import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates; |
| 24 | +import org.elasticsearch.xpack.esql.planner.TranslatorHandler; |
19 | 25 | import org.hamcrest.Matcher; |
20 | 26 |
|
21 | 27 | import java.util.LinkedList; |
22 | 28 | import java.util.List; |
| 29 | +import java.util.Map; |
23 | 30 | import java.util.function.Supplier; |
24 | 31 |
|
25 | 32 | import static org.hamcrest.Matchers.equalTo; |
@@ -98,4 +105,38 @@ private static TestCaseSupplier.TestCase testCase( |
98 | 105 | protected Expression build(Source source, List<Expression> args) { |
99 | 106 | return new EndsWith(source, args.get(0), args.get(1)); |
100 | 107 | } |
| 108 | + |
| 109 | + public void testLuceneQuery_AllLiterals_NonTranslatable() { |
| 110 | + var function = new EndsWith( |
| 111 | + Source.EMPTY, |
| 112 | + new Literal(Source.EMPTY, "test", DataType.KEYWORD), |
| 113 | + new Literal(Source.EMPTY, "test", DataType.KEYWORD) |
| 114 | + ); |
| 115 | + |
| 116 | + assertThat(function.translatable(LucenePushdownPredicates.DEFAULT), equalTo(false)); |
| 117 | + } |
| 118 | + |
| 119 | + public void testLuceneQuery_NonFoldableSuffix_NonTranslatable() { |
| 120 | + var function = new EndsWith( |
| 121 | + Source.EMPTY, |
| 122 | + new FieldAttribute(Source.EMPTY, "field", new EsField("field", DataType.KEYWORD, Map.of(), true)), |
| 123 | + new FieldAttribute(Source.EMPTY, "field", new EsField("suffix", DataType.KEYWORD, Map.of(), true)) |
| 124 | + ); |
| 125 | + |
| 126 | + assertThat(function.translatable(LucenePushdownPredicates.DEFAULT), equalTo(false)); |
| 127 | + } |
| 128 | + |
| 129 | + public void testLuceneQuery_NonFoldableSuffix_Translatable() { |
| 130 | + var function = new EndsWith( |
| 131 | + Source.EMPTY, |
| 132 | + new FieldAttribute(Source.EMPTY, "field", new EsField("suffix", DataType.KEYWORD, Map.of(), true)), |
| 133 | + new Literal(Source.EMPTY, "a*b?c\\", DataType.KEYWORD) |
| 134 | + ); |
| 135 | + |
| 136 | + assertThat(function.translatable(LucenePushdownPredicates.DEFAULT), equalTo(true)); |
| 137 | + |
| 138 | + var query = function.asQuery(TranslatorHandler.TRANSLATOR_HANDLER); |
| 139 | + |
| 140 | + assertThat(query, equalTo(new WildcardQuery(Source.EMPTY, "field", "*a\\*b\\?c\\\\"))); |
| 141 | + } |
101 | 142 | } |
0 commit comments