|
8 | 8 | package org.elasticsearch.xpack.esql.optimizer.rules.logical; |
9 | 9 |
|
10 | 10 | import org.elasticsearch.index.IndexMode; |
| 11 | +import org.elasticsearch.index.query.QueryBuilder; |
11 | 12 | import org.elasticsearch.test.ESTestCase; |
12 | 13 | import org.elasticsearch.xpack.esql.core.expression.Alias; |
13 | 14 | import org.elasticsearch.xpack.esql.core.expression.Attribute; |
14 | 15 | import org.elasticsearch.xpack.esql.core.expression.Expression; |
15 | 16 | import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; |
| 17 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
16 | 18 | import org.elasticsearch.xpack.esql.expression.function.aggregate.Count; |
| 19 | +import org.elasticsearch.xpack.esql.expression.function.fulltext.Match; |
17 | 20 | import org.elasticsearch.xpack.esql.expression.function.scalar.math.Pow; |
18 | 21 | import org.elasticsearch.xpack.esql.expression.function.scalar.string.RLike; |
19 | 22 | import org.elasticsearch.xpack.esql.expression.function.scalar.string.WildcardLike; |
|
28 | 31 | import org.elasticsearch.xpack.esql.plan.logical.Filter; |
29 | 32 | import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
30 | 33 | import org.elasticsearch.xpack.esql.plan.logical.Project; |
| 34 | +import org.elasticsearch.xpack.esql.plan.logical.inference.Completion; |
31 | 35 | import org.elasticsearch.xpack.esql.plan.logical.local.EsqlProject; |
32 | 36 |
|
33 | 37 | import java.util.ArrayList; |
|
45 | 49 | import static org.elasticsearch.xpack.esql.EsqlTestUtils.greaterThanOf; |
46 | 50 | import static org.elasticsearch.xpack.esql.EsqlTestUtils.greaterThanOrEqualOf; |
47 | 51 | import static org.elasticsearch.xpack.esql.EsqlTestUtils.lessThanOf; |
| 52 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.randomLiteral; |
| 53 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.referenceAttribute; |
48 | 54 | import static org.elasticsearch.xpack.esql.EsqlTestUtils.rlike; |
49 | 55 | import static org.elasticsearch.xpack.esql.EsqlTestUtils.wildcardLike; |
50 | 56 | import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY; |
| 57 | +import static org.mockito.Mockito.mock; |
51 | 58 |
|
52 | 59 | public class PushDownAndCombineFiltersTests extends ESTestCase { |
53 | 60 |
|
@@ -238,6 +245,39 @@ public void testSelectivelyPushDownFilterPastFunctionAgg() { |
238 | 245 | assertEquals(expected, new PushDownAndCombineFilters().apply(fb)); |
239 | 246 | } |
240 | 247 |
|
| 248 | + public void testPushDownFilterPastCompletion() { |
| 249 | + FieldAttribute a = getFieldAttribute("a"); |
| 250 | + FieldAttribute b = getFieldAttribute("b"); |
| 251 | + EsRelation relation = relation(List.of(a, b)); |
| 252 | + |
| 253 | + GreaterThan conditionA = greaterThanOf(getFieldAttribute("a"), ONE); |
| 254 | + Filter filterA = new Filter(EMPTY, relation, conditionA); |
| 255 | + |
| 256 | + Completion completion = completion(filterA); |
| 257 | + |
| 258 | + LessThan conditionB = lessThanOf(getFieldAttribute("b"), TWO); |
| 259 | + Match conditionCompletion = new Match(EMPTY, completion.targetField(), randomLiteral(DataType.TEXT), mock(Expression.class), mock(QueryBuilder.class)); |
| 260 | + Filter filterB = new Filter(EMPTY, completion, new And(EMPTY, conditionB, conditionCompletion)); |
| 261 | + |
| 262 | + LogicalPlan expectedOptimizedPlan = new Filter(EMPTY, |
| 263 | + new Completion( |
| 264 | + EMPTY, |
| 265 | + new Filter(EMPTY, relation, new And(EMPTY, conditionA, conditionB)), |
| 266 | + completion.inferenceId(), |
| 267 | + completion.prompt(), |
| 268 | + completion.targetField() |
| 269 | + ), |
| 270 | + conditionCompletion |
| 271 | + ); |
| 272 | + |
| 273 | + assertEquals(expectedOptimizedPlan, new PushDownAndCombineFilters().apply(filterB)); |
| 274 | + } |
| 275 | + |
| 276 | + private static Completion completion(LogicalPlan child) { |
| 277 | + return new Completion(EMPTY, child, randomLiteral(DataType.TEXT), randomLiteral(DataType.TEXT), referenceAttribute(randomIdentifier(), DataType.TEXT)); |
| 278 | + } |
| 279 | + |
| 280 | + |
241 | 281 | private static EsRelation relation() { |
242 | 282 | return relation(List.of()); |
243 | 283 | } |
|
0 commit comments