Skip to content

Commit

Permalink
SQL: Verify Full-Text Search functions not allowed in SELECT
Browse files Browse the repository at this point in the history
Add a verification that full-text search functions are not
allowed in the SELECT clause, so that a nice error message is
returned to the user early instead of an "ugly" exception.

Fixes: elastic#47446
  • Loading branch information
matriv committed Jan 28, 2020
1 parent 9d2c579 commit c1abb66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.xpack.ql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.ql.expression.function.grouping.GroupingFunction;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.ql.expression.predicate.fulltext.FullTextPredicate;
import org.elasticsearch.xpack.ql.plan.logical.Aggregate;
import org.elasticsearch.xpack.ql.plan.logical.Filter;
import org.elasticsearch.xpack.ql.plan.logical.Limit;
Expand Down Expand Up @@ -215,8 +216,16 @@ Collection<Failure> verify(LogicalPlan plan) {
// if there are no (major) unresolved failures, do more in-depth analysis

if (failures.isEmpty()) {
Set<Failure> localFailures = new LinkedHashSet<>();
final Map<Attribute, Expression> collectRefs = new LinkedHashMap<>();

// Full-Text search function are not allowed in the SELECT clause
plan.forEachUp(p -> p.forEachExpressionsUp(e -> {
if (e instanceof FullTextPredicate) {
localFailures.add(fail(e, "Cannot use a Full-Text search functions in the SELECT clause"));
}
}), Project.class);

// collect Attribute sources
// only Aliases are interesting since these are the only ones that hide expressions
// FieldAttribute for example are self replicating.
Expand All @@ -242,8 +251,6 @@ Collection<Failure> verify(LogicalPlan plan) {
return;
}

Set<Failure> localFailures = new LinkedHashSet<>();

checkGroupingFunctionInGroupBy(p, localFailures);
checkFilterOnAggs(p, localFailures, attributeRefs);
checkFilterOnGrouping(p, localFailures, attributeRefs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void testUnsupportedTypeInHierarchy() {
assertEquals("1:8: Cannot use field [x.y] with unsupported type [foobar]", error("SELECT x.y FROM test"));
}

public void testTermEqualitOnInexact() {
public void testTermEqualityOnInexact() {
assertEquals("1:26: [text = 'value'] cannot operate on first argument field of data type [text]: " +
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
error("SELECT * FROM test WHERE text = 'value'"));
Expand Down Expand Up @@ -712,6 +712,13 @@ public void testInvalidTypeForRLikeMatch() {
error("SELECT * FROM test WHERE text RLIKE 'foo'"));
}

public void testFullTextFunctionsNotAllowedInSelect() {
assertEquals("1:8: Cannot use a Full-Text search functions in the SELECT clause",
error("SELECT MATCH(text, 'foo') FROM test"));
assertEquals("1:38: Cannot use a Full-Text search functions in the SELECT clause",
error("SELECT int > 10 AND (bool = false OR QUERY('foo*')) FROM test"));
}

public void testAllowCorrectFieldsInIncompatibleMappings() {
assertNotNull(incompatibleAccept("SELECT languages FROM \"*\""));
}
Expand Down

0 comments on commit c1abb66

Please sign in to comment.