|
8 | 8 |
|
9 | 9 | import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
10 | 10 | import org.elasticsearch.common.io.stream.StreamInput; |
| 11 | +import org.elasticsearch.xpack.esql.capabilities.PostAnalysisPlanVerificationAware; |
| 12 | +import org.elasticsearch.xpack.esql.common.Failures; |
11 | 13 | import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 14 | +import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute; |
12 | 15 | import org.elasticsearch.xpack.esql.core.expression.predicate.Negatable; |
13 | 16 | import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
14 | 17 | import org.elasticsearch.xpack.esql.core.tree.Source; |
| 18 | +import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction; |
15 | 19 | import org.elasticsearch.xpack.esql.expression.predicate.Predicates; |
| 20 | +import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates; |
| 21 | +import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
16 | 22 |
|
17 | 23 | import java.io.IOException; |
| 24 | +import java.util.function.BiConsumer; |
18 | 25 |
|
19 | | -public class Or extends BinaryLogic implements Negatable<BinaryLogic> { |
| 26 | +import static org.elasticsearch.xpack.esql.common.Failure.fail; |
| 27 | + |
| 28 | +public class Or extends BinaryLogic implements Negatable<BinaryLogic>, PostAnalysisPlanVerificationAware { |
20 | 29 | public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Or", Or::new); |
21 | 30 |
|
22 | 31 | public Or(Source source, Expression left, Expression right) { |
@@ -57,4 +66,51 @@ protected Expression canonicalize() { |
57 | 66 | // NB: this add a circular dependency between Predicates / Logical package |
58 | 67 | return Predicates.combineOr(Predicates.splitOr(super.canonicalize())); |
59 | 68 | } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean translatable(LucenePushdownPredicates pushdownPredicates) { |
| 72 | + return super.translatable(pushdownPredicates) && checkPushableFullTextSearchFunctions(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() { |
| 77 | + return (plan, failures) -> { |
| 78 | + boolean usesScore = plan.output() |
| 79 | + .stream() |
| 80 | + .anyMatch(attr -> attr instanceof MetadataAttribute ma && ma.name().equals(MetadataAttribute.SCORE)); |
| 81 | + if (usesScore && checkPushableFullTextSearchFunctions() == false) { |
| 82 | + failures.add( |
| 83 | + fail( |
| 84 | + this, |
| 85 | + "Invalid condition when using METADATA _score [{}]. Full text functions can be used in an OR condition, " |
| 86 | + + "but only if just full text functions are used in the OR condition", |
| 87 | + sourceText() |
| 88 | + ) |
| 89 | + ); |
| 90 | + } |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + private boolean checkPushableFullTextSearchFunctions() { |
| 95 | + boolean hasFullText = anyMatch(FullTextFunction.class::isInstance); |
| 96 | + return hasFullText == false || onlyFullTextFunctionsInExpression(this); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Checks whether an expression contains just full text functions or negations (NOT) and combinations (AND, OR) of full text functions |
| 101 | + * |
| 102 | + * @param expression expression to check |
| 103 | + * @return true if all children are full text functions or negations of full text functions, false otherwise |
| 104 | + */ |
| 105 | + private static boolean onlyFullTextFunctionsInExpression(Expression expression) { |
| 106 | + if (expression instanceof FullTextFunction) { |
| 107 | + return true; |
| 108 | + } else if (expression instanceof Not) { |
| 109 | + return onlyFullTextFunctionsInExpression(expression.children().get(0)); |
| 110 | + } else if (expression instanceof BinaryLogic binaryLogic) { |
| 111 | + return onlyFullTextFunctionsInExpression(binaryLogic.left()) && onlyFullTextFunctionsInExpression(binaryLogic.right()); |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
60 | 116 | } |
0 commit comments