1010import com .carrotsearch .randomizedtesting .annotations .Name ;
1111import com .carrotsearch .randomizedtesting .annotations .ParametersFactory ;
1212
13+ import org .elasticsearch .index .query .QueryBuilder ;
1314import org .elasticsearch .xpack .esql .core .expression .Expression ;
15+ import org .elasticsearch .xpack .esql .core .expression .Literal ;
16+ import org .elasticsearch .xpack .esql .core .expression .MapExpression ;
1417import org .elasticsearch .xpack .esql .core .tree .Source ;
18+ import org .elasticsearch .xpack .esql .core .type .DataType ;
1519import org .elasticsearch .xpack .esql .expression .function .FunctionName ;
1620import org .elasticsearch .xpack .esql .expression .function .TestCaseSupplier ;
1721
22+ import java .util .ArrayList ;
1823import java .util .List ;
1924import java .util .function .Supplier ;
2025
26+ import static org .elasticsearch .xpack .esql .core .type .DataType .BOOLEAN ;
27+ import static org .elasticsearch .xpack .esql .core .type .DataType .KEYWORD ;
28+ import static org .elasticsearch .xpack .esql .core .type .DataType .UNSUPPORTED ;
29+ import static org .elasticsearch .xpack .esql .planner .TranslatorHandler .TRANSLATOR_HANDLER ;
30+ import static org .hamcrest .Matchers .equalTo ;
31+
2132@ FunctionName ("qstr" )
2233public class QueryStringTests extends NoneFieldFullTextFunctionTestCase {
2334
@@ -27,11 +38,48 @@ public QueryStringTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> te
2738
2839 @ ParametersFactory
2940 public static Iterable <Object []> parameters () {
30- return generateParameters ();
41+ return parameterSuppliersFromTypedData (addFunctionNamedParams (getStringTestSupplier ()));
42+ }
43+
44+ /**
45+ * Adds function named parameters to all the test case suppliers provided
46+ */
47+ private static List <TestCaseSupplier > addFunctionNamedParams (List <TestCaseSupplier > suppliers ) {
48+ List <TestCaseSupplier > result = new ArrayList <>();
49+ for (TestCaseSupplier supplier : suppliers ) {
50+ List <DataType > dataTypes = new ArrayList <>(supplier .types ());
51+ dataTypes .add (UNSUPPORTED );
52+ result .add (new TestCaseSupplier (supplier .name () + ", options" , dataTypes , () -> {
53+ List <TestCaseSupplier .TypedData > values = new ArrayList <>(supplier .get ().getData ());
54+ values .add (
55+ new TestCaseSupplier .TypedData (
56+ new MapExpression (
57+ Source .EMPTY ,
58+ List .of (
59+ new Literal (Source .EMPTY , "default_field" , KEYWORD ),
60+ new Literal (Source .EMPTY , randomAlphaOfLength (10 ), KEYWORD )
61+ )
62+ ),
63+ UNSUPPORTED ,
64+ "options"
65+ ).forceLiteral ()
66+ );
67+
68+ return new TestCaseSupplier .TestCase (values , equalTo ("MatchEvaluator" ), BOOLEAN , equalTo (true ));
69+ }));
70+ }
71+ return result ;
3172 }
3273
3374 @ Override
3475 protected Expression build (Source source , List <Expression > args ) {
35- return new QueryString (source , args .getFirst (), null );
76+ var qstr = new QueryString (source , args .get (0 ), args .size () > 1 ? args .get (1 ) : null );
77+ // We need to add the QueryBuilder to the match expression, as it is used to implement equals() and hashCode() and
78+ // thus test the serialization methods. But we can only do this if the parameters make sense .
79+ if (args .get (0 ).foldable ()) {
80+ QueryBuilder queryBuilder = TRANSLATOR_HANDLER .asQuery (qstr ).asBuilder ();
81+ qstr .replaceQueryBuilder (queryBuilder );
82+ }
83+ return qstr ;
3684 }
3785}
0 commit comments