Skip to content

Commit

Permalink
Refactor detection method for full text functions
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdelest committed Dec 12, 2024
1 parent c198d91 commit c91cf7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,14 +780,14 @@ private static void checkFullTextSearchDisjunctions(
Set<Failure> failures
) {
condition.forEachUp(Or.class, or -> {
boolean left = checkFullTextSearchInDisjunctions(or.left());
boolean right = checkFullTextSearchInDisjunctions(or.right());
boolean left = onlyFullTextFunctionsInExpression(or.left());
boolean right = onlyFullTextFunctionsInExpression(or.right());
if (left ^ right) {
Holder<String> elementName = new Holder<>();
if (left) {
or.right().forEachDown(FullTextFunction.class, ftf -> elementName.set(typeNameProvider.apply(ftf)));
} else {
or.left().forEachDown(FullTextFunction.class, ftf -> elementName.set(typeNameProvider.apply(ftf)));
} else {
or.right().forEachDown(FullTextFunction.class, ftf -> elementName.set(typeNameProvider.apply(ftf)));
}
failures.add(
fail(
Expand All @@ -805,22 +805,22 @@ private static void checkFullTextSearchDisjunctions(
/**
* Checks whether an expression contains just full text functions or negations (NOT) and combinations (AND, OR) of full text functions
*
* @param expression parent expression to add to the failure message
* @param expression expression to check
* @return true if all children are full text functions or negations of full text functions, false otherwise
*/
private static boolean checkFullTextSearchInDisjunctions(Expression expression) {
private static boolean onlyFullTextFunctionsInExpression(Expression expression) {
if (expression instanceof FullTextFunction) {
return false;
return true;
} else if (expression instanceof Not || expression instanceof BinaryLogic) {
for (Expression child : expression.children()) {
if (checkFullTextSearchInDisjunctions(child)) {
return true;
if (onlyFullTextFunctionsInExpression(child) == false) {
return false;
}
}
return false;
return true;
}

return true;
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1168,14 +1168,14 @@ public void testMatchInsideEval() throws Exception {
public void testMatchFilter() throws Exception {
assertEquals(
"1:19: Invalid condition [first_name:\"Anna\" or starts_with(first_name, \"Anne\")]. "
+ "[:] operator can be used as part of an OR condition, " +
"but only if other full text functions are used as part of the condition",
+ "[:] operator can be used as part of an OR condition, "
+ "but only if other full text functions are used as part of the condition",
error("from test | where first_name:\"Anna\" or starts_with(first_name, \"Anne\")")
);

assertEquals(
"1:51: Invalid condition [first_name:\"Anna\" OR new_salary > 100]. [:] operator can be used as part of an OR " +
"condition, but only if other full text functions are used as part of the condition",
"1:51: Invalid condition [first_name:\"Anna\" OR new_salary > 100]. [:] operator can be used as part of an OR "
+ "condition, but only if other full text functions are used as part of the condition",
error("from test | eval new_salary = salary + 10 | where first_name:\"Anna\" OR new_salary > 100")
);
}
Expand Down

0 comments on commit c91cf7c

Please sign in to comment.