|
| 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; |
| 9 | + |
| 10 | +import org.elasticsearch.test.ESTestCase; |
| 11 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 12 | +import org.elasticsearch.xpack.esql.core.expression.TypeResolutions; |
| 13 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 14 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 15 | +import org.hamcrest.Matcher; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.randomLiteral; |
| 25 | +import static org.hamcrest.Matchers.greaterThan; |
| 26 | + |
| 27 | +public abstract class ErrorsForCasesWithoutExamplesTestCase extends ESTestCase { |
| 28 | + protected abstract List<TestCaseSupplier> cases(); |
| 29 | + |
| 30 | + /** |
| 31 | + * Build the expression being tested, for the given source and list of arguments. Test classes need to implement this |
| 32 | + * to have something to test. |
| 33 | + * |
| 34 | + * @param source the source |
| 35 | + * @param args arg list from the test case, should match the length expected |
| 36 | + * @return an expression for evaluating the function being tested on the given arguments |
| 37 | + */ |
| 38 | + protected abstract Expression build(Source source, List<Expression> args); |
| 39 | + |
| 40 | + protected abstract Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature); |
| 41 | + |
| 42 | + protected final List<TestCaseSupplier> paramsToSuppliers(Iterable<Object[]> cases) { |
| 43 | + List<TestCaseSupplier> result = new ArrayList<>(); |
| 44 | + for (Object[] c : cases) { |
| 45 | + if (c.length != 1) { |
| 46 | + throw new IllegalArgumentException("weird layout for test cases"); |
| 47 | + } |
| 48 | + TestCaseSupplier supplier = (TestCaseSupplier) c[0]; |
| 49 | + result.add(supplier); |
| 50 | + } |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + public final void test() { |
| 55 | + int checked = 0; |
| 56 | + List<TestCaseSupplier> cases = cases(); |
| 57 | + Set<List<DataType>> valid = cases.stream().map(TestCaseSupplier::types).collect(Collectors.toSet()); |
| 58 | + List<Set<DataType>> validPerPosition = AbstractFunctionTestCase.validPerPosition(valid); |
| 59 | + Iterable<List<DataType>> missingSignatures = missingSignatures(cases, valid)::iterator; |
| 60 | + for (List<DataType> signature : missingSignatures) { |
| 61 | + logger.debug("checking {}", signature); |
| 62 | + List<Expression> args = new ArrayList<>(signature.size()); |
| 63 | + for (DataType type : signature) { |
| 64 | + args.add(randomLiteral(type)); |
| 65 | + } |
| 66 | + Expression expression = build(Source.synthetic(sourceForSignature(signature)), args); |
| 67 | + assertTrue("expected unresolved " + expression, expression.typeResolved().unresolved()); |
| 68 | + assertThat(expression.typeResolved().message(), expectedTypeErrorMatcher(validPerPosition, signature)); |
| 69 | + checked++; |
| 70 | + } |
| 71 | + logger.info("checked {} signatures", checked); |
| 72 | + assertThat("didn't check any signatures", checked, greaterThan(0)); |
| 73 | + } |
| 74 | + |
| 75 | + private Stream<List<DataType>> missingSignatures(List<TestCaseSupplier> cases, Set<List<DataType>> valid) { |
| 76 | + return cases.stream() |
| 77 | + .map(s -> s.types().size()) |
| 78 | + .collect(Collectors.toSet()) |
| 79 | + .stream() |
| 80 | + .flatMap(AbstractFunctionTestCase::allPermutations) |
| 81 | + .filter(types -> valid.contains(types) == false) |
| 82 | + /* |
| 83 | + * Skip any cases with more than one null. Our tests don't generate |
| 84 | + * the full combinatorial explosions of all nulls - just a single null. |
| 85 | + * Hopefully <null>, <null> cases will function the same as <null>, <valid> |
| 86 | + * cases. |
| 87 | + */ |
| 88 | + .filter(types -> types.stream().filter(t -> t == DataType.NULL).count() <= 1); |
| 89 | + } |
| 90 | + |
| 91 | + protected static String sourceForSignature(List<DataType> signature) { |
| 92 | + StringBuilder source = new StringBuilder(); |
| 93 | + for (DataType type : signature) { |
| 94 | + if (false == source.isEmpty()) { |
| 95 | + source.append(", "); |
| 96 | + } |
| 97 | + source.append(type.typeName()); |
| 98 | + } |
| 99 | + return source.toString(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Build the expected error message for an invalid type signature. |
| 104 | + */ |
| 105 | + protected static String typeErrorMessage( |
| 106 | + boolean includeOrdinal, |
| 107 | + List<Set<DataType>> validPerPosition, |
| 108 | + List<DataType> signature, |
| 109 | + AbstractFunctionTestCase.PositionalErrorMessageSupplier expectedTypeSupplier |
| 110 | + ) { |
| 111 | + int badArgPosition = -1; |
| 112 | + for (int i = 0; i < signature.size(); i++) { |
| 113 | + if (validPerPosition.get(i).contains(signature.get(i)) == false) { |
| 114 | + badArgPosition = i; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + if (badArgPosition == -1) { |
| 119 | + throw new IllegalStateException( |
| 120 | + "Can't generate error message for these types, you probably need a custom error message function" |
| 121 | + ); |
| 122 | + } |
| 123 | + String ordinal = includeOrdinal ? TypeResolutions.ParamOrdinal.fromIndex(badArgPosition).name().toLowerCase(Locale.ROOT) + " " : ""; |
| 124 | + String source = sourceForSignature(signature); |
| 125 | + String expectedTypeString = expectedTypeSupplier.apply(validPerPosition.get(badArgPosition), badArgPosition); |
| 126 | + String name = signature.get(badArgPosition).typeName(); |
| 127 | + return ordinal + "argument of [" + source + "] must be [" + expectedTypeString + "], found value [] type [" + name + "]"; |
| 128 | + } |
| 129 | + |
| 130 | + protected static String errorMessageStringForBinaryOperators( |
| 131 | + List<Set<DataType>> validPerPosition, |
| 132 | + List<DataType> signature, |
| 133 | + AbstractFunctionTestCase.PositionalErrorMessageSupplier positionalErrorMessageSupplier |
| 134 | + ) { |
| 135 | + try { |
| 136 | + return typeErrorMessage(true, validPerPosition, signature, positionalErrorMessageSupplier); |
| 137 | + } catch (IllegalStateException e) { |
| 138 | + String source = sourceForSignature(signature); |
| 139 | + // This means all the positional args were okay, so the expected error is from the combination |
| 140 | + if (signature.get(0).equals(DataType.UNSIGNED_LONG)) { |
| 141 | + return "first argument of [" |
| 142 | + + source |
| 143 | + + "] is [unsigned_long] and second is [" |
| 144 | + + signature.get(1).typeName() |
| 145 | + + "]. [unsigned_long] can only be operated on together with another [unsigned_long]"; |
| 146 | + |
| 147 | + } |
| 148 | + if (signature.get(1).equals(DataType.UNSIGNED_LONG)) { |
| 149 | + return "first argument of [" |
| 150 | + + source |
| 151 | + + "] is [" |
| 152 | + + signature.get(0).typeName() |
| 153 | + + "] and second is [unsigned_long]. [unsigned_long] can only be operated on together with another [unsigned_long]"; |
| 154 | + } |
| 155 | + return "first argument of [" |
| 156 | + + source |
| 157 | + + "] is [" |
| 158 | + + (signature.get(0).isNumeric() ? "numeric" : signature.get(0).typeName()) |
| 159 | + + "] so second argument must also be [" |
| 160 | + + (signature.get(0).isNumeric() ? "numeric" : signature.get(0).typeName()) |
| 161 | + + "] but was [" |
| 162 | + + signature.get(1).typeName() |
| 163 | + + "]"; |
| 164 | + |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments