Skip to content

Commit 52107ed

Browse files
authored
SQL: Verify Full-Text Search functions not allowed in SELECT (#51568) (#51673)
Add a verification that full-text search functions `MATCH()` and `QUERY()` 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: #47446 (cherry picked from commit 65ad269)
1 parent fe254a2 commit 52107ed

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xpack.sql.expression.Expression;
1414
import org.elasticsearch.xpack.sql.expression.Expressions;
1515
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
16+
import org.elasticsearch.xpack.sql.expression.NamedExpression;
1617
import org.elasticsearch.xpack.sql.expression.UnresolvedAttribute;
1718
import org.elasticsearch.xpack.sql.expression.function.Function;
1819
import org.elasticsearch.xpack.sql.expression.function.FunctionAttribute;
@@ -27,6 +28,7 @@
2728
import org.elasticsearch.xpack.sql.expression.function.grouping.GroupingFunctionAttribute;
2829
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
2930
import org.elasticsearch.xpack.sql.expression.predicate.conditional.ConditionalFunction;
31+
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.FullTextPredicate;
3032
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.In;
3133
import org.elasticsearch.xpack.sql.plan.logical.Aggregate;
3234
import org.elasticsearch.xpack.sql.plan.logical.Distinct;
@@ -231,6 +233,8 @@ Collection<Failure> verify(LogicalPlan plan) {
231233
validateInExpression(p, localFailures);
232234
validateConditional(p, localFailures);
233235

236+
checkFullTextSearchInSelect(plan, localFailures);
237+
234238
checkGroupingFunctionInGroupBy(p, localFailures);
235239
checkFilterOnAggs(p, localFailures);
236240
checkFilterOnGrouping(p, localFailures);
@@ -282,6 +286,17 @@ Collection<Failure> verify(LogicalPlan plan) {
282286
return failures;
283287
}
284288

289+
private void checkFullTextSearchInSelect(LogicalPlan plan, Set<Failure> localFailures) {
290+
plan.forEachUp(p -> {
291+
for (NamedExpression ne : p.projections()) {
292+
ne.forEachUp((e) ->
293+
localFailures.add(fail(e, "Cannot use MATCH() or QUERY() full-text search " +
294+
"functions in the SELECT clause")),
295+
FullTextPredicate.class);
296+
}
297+
}, Project.class);
298+
}
299+
285300
/**
286301
* Check validity of Aggregate/GroupBy.
287302
* This rule is needed for multiple reasons:

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void testUnsupportedTypeInFilter() {
356356
error("SELECT * FROM test WHERE unsupported > 1"));
357357
}
358358

359-
public void testTermEqualitOnInexact() {
359+
public void testTermEqualityOnInexact() {
360360
assertEquals("1:26: [text = 'value'] cannot operate on first argument field of data type [text]: " +
361361
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
362362
error("SELECT * FROM test WHERE text = 'value'"));
@@ -586,7 +586,20 @@ public void testInvalidTypeForRLikeMatch() {
586586
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
587587
error("SELECT * FROM test WHERE text RLIKE 'foo'"));
588588
}
589-
589+
590+
public void testMatchAndQueryFunctionsNotAllowedInSelect() {
591+
assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause",
592+
error("SELECT MATCH(text, 'foo') FROM test"));
593+
assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause",
594+
error("SELECT MATCH(text, 'foo') AS fullTextSearch FROM test"));
595+
assertEquals("1:38: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause",
596+
error("SELECT int > 10 AND (bool = false OR QUERY('foo*')) AS fullTextSearch FROM test"));
597+
assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause\n" +
598+
"line 1:28: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause",
599+
error("SELECT MATCH(text, 'foo'), MATCH(text, 'bar') FROM test"));
600+
accept("SELECT * FROM test WHERE MATCH(text, 'foo')");
601+
}
602+
590603
public void testAllowCorrectFieldsInIncompatibleMappings() {
591604
assertNotNull(incompatibleAccept("SELECT languages FROM \"*\""));
592605
}

0 commit comments

Comments
 (0)