Skip to content

Commit 8901591

Browse files
committed
Simplify disjunction algorithm
1 parent 6449d22 commit 8901591

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/MatchFunctionIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void testWhereMatchWithFunctions() {
240240
error.getMessage(),
241241
containsString(
242242
"Invalid condition [match(content, \"fox\") OR to_upper(content) == \"FOX\"]. "
243-
+ "[MATCH] function can be used in an OR condition, but only if just full text functions are used in the OR condition"
243+
+ "Full text functions can be used in an OR condition, but only if just full text functions are used in the OR condition"
244244
)
245245
);
246246
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Verifier.java

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Or;
2828
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.comparison.BinaryComparison;
2929
import org.elasticsearch.xpack.esql.core.type.DataType;
30-
import org.elasticsearch.xpack.esql.core.util.Holder;
3130
import org.elasticsearch.xpack.esql.expression.function.UnsupportedAttribute;
3231
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
3332
import org.elasticsearch.xpack.esql.expression.function.aggregate.FilteredExpression;
@@ -786,50 +785,23 @@ private static void checkFullTextSearchDisjunctions(
786785
// Exit early if we already have a failures
787786
return;
788787
}
789-
Expression left = or.left();
790-
boolean leftHasFullText = left.anyMatch(FullTextFunction.class::isInstance);
791-
boolean leftHasOnlyFullText = onlyFullTextFunctionsInExpression(left);
792-
Expression right = or.right();
793-
boolean rightHasFullText = right.anyMatch(FullTextFunction.class::isInstance);
794-
boolean rightHasOnlyFullText = onlyFullTextFunctionsInExpression(right);
795-
796-
if (leftHasFullText && leftHasOnlyFullText == false) {
797-
disjunctionFailure(or, left, typeNameProvider, failures);
798-
} else if (rightHasFullText && rightHasOnlyFullText == false) {
799-
disjunctionFailure(or, right, typeNameProvider, failures);
800-
} else if (leftHasFullText ^ rightHasFullText) {
801-
disjunctionFailure(or, leftHasFullText ? left : right, typeNameProvider, failures);
788+
boolean hasFullText = or.anyMatch(FullTextFunction.class::isInstance);
789+
if (hasFullText) {
790+
boolean hasOnlyFullText = onlyFullTextFunctionsInExpression(or);
791+
if (hasOnlyFullText == false) {
792+
failures.add(
793+
fail(
794+
or,
795+
"Invalid condition [{}]. Full text functions can be used in an OR condition, "
796+
+ "but only if just full text functions are used in the OR condition",
797+
or.sourceText()
798+
)
799+
);
800+
}
802801
}
803802
});
804803
}
805804

806-
/**
807-
* Add a disjunction failure to the failures collection.
808-
* @param parentExpression parent expression to include in the failure
809-
* @param expressionWithError expression that provoked the failure
810-
* @param typeNameProvider provider for the type name to add in the failure message
811-
* @param failures failures collection to add to
812-
*/
813-
private static void disjunctionFailure(
814-
Expression parentExpression,
815-
Expression expressionWithError,
816-
java.util.function.Function<FullTextFunction, String> typeNameProvider,
817-
Set<Failure> failures
818-
) {
819-
Holder<String> elementName = new Holder<>();
820-
// Get first function name to add to the failure message
821-
expressionWithError.forEachDown(FullTextFunction.class, ftf -> elementName.set(typeNameProvider.apply(ftf)));
822-
failures.add(
823-
fail(
824-
parentExpression,
825-
"Invalid condition [{}]. {} can be used in an OR condition, "
826-
+ "but only if just full text functions are used in the OR condition",
827-
parentExpression.sourceText(),
828-
elementName.get()
829-
)
830-
);
831-
}
832-
833805
/**
834806
* Checks whether an expression contains just full text functions or negations (NOT) and combinations (AND, OR) of full text functions
835807
*

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,14 +1166,14 @@ public void testMatchInsideEval() throws Exception {
11661166
public void testMatchFilter() throws Exception {
11671167
assertEquals(
11681168
"1:19: Invalid condition [first_name:\"Anna\" or starts_with(first_name, \"Anne\")]. "
1169-
+ "[:] operator can be used in an OR condition, "
1169+
+ "Full text functions can be used in an OR condition, "
11701170
+ "but only if just full text functions are used in the OR condition",
11711171
error("from test | where first_name:\"Anna\" or starts_with(first_name, \"Anne\")")
11721172
);
11731173

11741174
assertEquals(
1175-
"1:51: Invalid condition [first_name:\"Anna\" OR new_salary > 100]. [:] operator can be used in an OR "
1176-
+ "condition, but only if just full text functions are used in the OR condition",
1175+
"1:51: Invalid condition [first_name:\"Anna\" OR new_salary > 100]. Full text functions can be"
1176+
+ " used in an OR condition, but only if just full text functions are used in the OR condition",
11771177
error("from test | eval new_salary = salary + 10 | where first_name:\"Anna\" OR new_salary > 100")
11781178
);
11791179
}
@@ -1423,12 +1423,10 @@ private void checkdisjunctionError(String position, String expression, String fu
14231423
assertEquals(
14241424
LoggerMessageFormat.format(
14251425
null,
1426-
"{}: Invalid condition [{}]. [{}] {} can be used in an OR condition, "
1426+
"{}: Invalid condition [{}]. Full text functions can be used in an OR condition, "
14271427
+ "but only if just full text functions are used in the OR condition",
14281428
position,
1429-
expression,
1430-
functionName,
1431-
functionType
1429+
expression
14321430
),
14331431
error("from test | where " + expression)
14341432
);

0 commit comments

Comments
 (0)