Skip to content

Commit c1abb66

Browse files
committed
SQL: Verify Full-Text Search functions not allowed in SELECT
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
1 parent 9d2c579 commit c1abb66

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.xpack.ql.expression.function.aggregate.AggregateFunction;
2323
import org.elasticsearch.xpack.ql.expression.function.grouping.GroupingFunction;
2424
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
25+
import org.elasticsearch.xpack.ql.expression.predicate.fulltext.FullTextPredicate;
2526
import org.elasticsearch.xpack.ql.plan.logical.Aggregate;
2627
import org.elasticsearch.xpack.ql.plan.logical.Filter;
2728
import org.elasticsearch.xpack.ql.plan.logical.Limit;
@@ -215,8 +216,16 @@ Collection<Failure> verify(LogicalPlan plan) {
215216
// if there are no (major) unresolved failures, do more in-depth analysis
216217

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

222+
// Full-Text search function are not allowed in the SELECT clause
223+
plan.forEachUp(p -> p.forEachExpressionsUp(e -> {
224+
if (e instanceof FullTextPredicate) {
225+
localFailures.add(fail(e, "Cannot use a Full-Text search functions in the SELECT clause"));
226+
}
227+
}), Project.class);
228+
220229
// collect Attribute sources
221230
// only Aliases are interesting since these are the only ones that hide expressions
222231
// FieldAttribute for example are self replicating.
@@ -242,8 +251,6 @@ Collection<Failure> verify(LogicalPlan plan) {
242251
return;
243252
}
244253

245-
Set<Failure> localFailures = new LinkedHashSet<>();
246-
247254
checkGroupingFunctionInGroupBy(p, localFailures);
248255
checkFilterOnAggs(p, localFailures, attributeRefs);
249256
checkFilterOnGrouping(p, localFailures, attributeRefs);

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public void testUnsupportedTypeInHierarchy() {
511511
assertEquals("1:8: Cannot use field [x.y] with unsupported type [foobar]", error("SELECT x.y FROM test"));
512512
}
513513

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

715+
public void testFullTextFunctionsNotAllowedInSelect() {
716+
assertEquals("1:8: Cannot use a Full-Text search functions in the SELECT clause",
717+
error("SELECT MATCH(text, 'foo') FROM test"));
718+
assertEquals("1:38: Cannot use a Full-Text search functions in the SELECT clause",
719+
error("SELECT int > 10 AND (bool = false OR QUERY('foo*')) FROM test"));
720+
}
721+
715722
public void testAllowCorrectFieldsInIncompatibleMappings() {
716723
assertNotNull(incompatibleAccept("SELECT languages FROM \"*\""));
717724
}

0 commit comments

Comments
 (0)