77
88package org .elasticsearch .xpack .esql .expression .function .fulltext ;
99
10+ import org .elasticsearch .index .query .QueryBuilder ;
1011import org .elasticsearch .xpack .esql .core .expression .Expression ;
12+ import org .elasticsearch .xpack .esql .core .expression .FieldAttribute ;
1113import org .elasticsearch .xpack .esql .core .expression .TypeResolutions ;
1214import org .elasticsearch .xpack .esql .core .tree .Source ;
1315import org .elasticsearch .xpack .esql .core .type .DataType ;
2123import java .util .Locale ;
2224import java .util .Set ;
2325
26+ import static org .elasticsearch .common .logging .LoggerMessageFormat .format ;
27+ import static org .elasticsearch .xpack .esql .planner .TranslatorHandler .TRANSLATOR_HANDLER ;
2428import static org .hamcrest .Matchers .equalTo ;
2529
2630public class MatchErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@@ -31,7 +35,14 @@ protected List<TestCaseSupplier> cases() {
3135
3236 @ Override
3337 protected Expression build (Source source , List <Expression > args ) {
34- return new Match (source , args .get (0 ), args .get (1 ), null );
38+ Match match = new Match (source , args .get (0 ), args .get (1 ), args .size () > 2 ? args .get (2 ) : null );
39+ // We need to add the QueryBuilder to the match expression, as it is used to implement equals() and hashCode() and
40+ // thus test the serialization methods. But we can only do this if the parameters make sense .
41+ if (args .get (0 ) instanceof FieldAttribute && args .get (1 ).foldable ()) {
42+ QueryBuilder queryBuilder = TRANSLATOR_HANDLER .asQuery (match ).asBuilder ();
43+ match .replaceQueryBuilder (queryBuilder );
44+ }
45+ return match ;
3546 }
3647
3748 @ Override
@@ -46,7 +57,8 @@ private static String errorMessageStringForMatch(
4657 List <DataType > signature ,
4758 AbstractFunctionTestCase .PositionalErrorMessageSupplier positionalErrorMessageSupplier
4859 ) {
49- for (int i = 0 ; i < signature .size (); i ++) {
60+ boolean invalid = false ;
61+ for (int i = 0 ; i < signature .size () && invalid == false ; i ++) {
5062 // Need to check for nulls and bad parameters in order
5163 if (signature .get (i ) == DataType .NULL ) {
5264 return TypeResolutions .ParamOrdinal .fromIndex (i ).name ().toLowerCase (Locale .ROOT )
@@ -55,6 +67,10 @@ private static String errorMessageStringForMatch(
5567 + "] cannot be null, received []" ;
5668 }
5769 if (validPerPosition .get (i ).contains (signature .get (i )) == false ) {
70+ // Map expressions have different error messages
71+ if (i == 2 ) {
72+ return format (null , "third argument of [{}] must be a map expression, received []" , sourceForSignature (signature ));
73+ }
5874 break ;
5975 }
6076 }
0 commit comments