|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.expression.function.fulltext; |
| 9 | + |
| 10 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 11 | +import org.elasticsearch.xpack.esql.core.expression.TypeResolutions; |
| 12 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 13 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 14 | +import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase; |
| 15 | +import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase; |
| 16 | +import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; |
| 17 | +import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.EsqlBinaryComparison; |
| 18 | +import org.hamcrest.Matcher; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import static org.hamcrest.Matchers.equalTo; |
| 25 | + |
| 26 | +public class MatchOperatorErrorTests extends ErrorsForCasesWithoutExamplesTestCase { |
| 27 | + |
| 28 | + @Override |
| 29 | + protected List<TestCaseSupplier> cases() { |
| 30 | + return paramsToSuppliers(MatchOperatorTests.parameters()); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected Expression build(Source source, List<Expression> args) { |
| 35 | + return new MatchOperator(source, args.get(0), args.get(1), null); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) { |
| 40 | + return equalTo( |
| 41 | + errorMessageStringForMatch(validPerPosition, signature, (l, p) -> p == 0 ? FIELD_TYPE_ERROR_STRING : QUERY_TYPE_ERROR_STRING) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + private static String errorMessageStringForMatch( |
| 46 | + List<Set<DataType>> validPerPosition, |
| 47 | + List<DataType> signature, |
| 48 | + AbstractFunctionTestCase.PositionalErrorMessageSupplier positionalErrorMessageSupplier |
| 49 | + ) { |
| 50 | + boolean invalid = false; |
| 51 | + for (int i = 0; i < signature.size() && invalid == false; i++) { |
| 52 | + // Need to check for nulls and bad parameters in order |
| 53 | + if (signature.get(i) == DataType.NULL) { |
| 54 | + return TypeResolutions.ParamOrdinal.fromIndex(i).name().toLowerCase(Locale.ROOT) |
| 55 | + + " argument of [" |
| 56 | + + sourceForSignature(signature) |
| 57 | + + "] cannot be null, received []"; |
| 58 | + } |
| 59 | + if (validPerPosition.get(i).contains(signature.get(i)) == false) { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + return typeErrorMessage(true, validPerPosition, signature, positionalErrorMessageSupplier); |
| 66 | + } catch (IllegalStateException e) { |
| 67 | + // This means all the positional args were okay, so the expected error is for nulls or from the combination |
| 68 | + return EsqlBinaryComparison.formatIncompatibleTypesMessage(signature.get(0), signature.get(1), sourceForSignature(signature)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static final String FIELD_TYPE_ERROR_STRING = |
| 73 | + "keyword, text, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version"; |
| 74 | + |
| 75 | + private static final String QUERY_TYPE_ERROR_STRING = |
| 76 | + "keyword, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version"; |
| 77 | +} |
0 commit comments