Skip to content

Commit

Permalink
Rest of string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9000 committed Jan 7, 2025
1 parent 30f6f6b commit 480d273
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.stream.Stream;

import static org.elasticsearch.xpack.esql.EsqlTestUtils.randomLiteral;
import static org.hamcrest.Matchers.greaterThan;

public abstract class ErrorsForCasesWithoutExamplesTestCase extends ESTestCase {
protected abstract List<TestCaseSupplier> cases();
Expand Down Expand Up @@ -68,6 +69,7 @@ public final void test() {
checked++;
}
logger.info("checked {} signatures", checked);
assertThat(checked, greaterThan(0));
}

private Stream<List<DataType>> missingSignatures(List<TestCaseSupplier> cases, Set<List<DataType>> valid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class LeftErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(LeftTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Left(source, args.get(0), args.get(1));
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(true, validPerPosition, signature, (v, p) -> switch (p) {
case 0 -> "string";
case 1 -> "integer";
default -> "";
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ public static Iterable<Object[]> parameters() {
);
}));

return parameterSuppliersFromTypedDataWithDefaultChecks(true, suppliers, (v, p) -> switch (p) {
case 0 -> "string";
case 1 -> "integer";
default -> "";
});
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
}

private static String unicodeLeftSubstring(String str, int length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class LengthErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(LengthTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Length(source, args.get(0));
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(false, validPerPosition, signature, (v, p) -> "string"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Iterable<Object[]> parameters() {
cases.addAll(makeTestCases("6 bytes, 2 code points", () -> "❗️", 2));
cases.addAll(makeTestCases("100 random alpha", () -> randomAlphaOfLength(100), 100));
cases.addAll(makeTestCases("100 random code points", () -> randomUnicodeOfCodepointLength(100), 100));
return parameterSuppliersFromTypedDataWithDefaultChecks(true, cases, (v, p) -> "string");
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, cases);
}

private static List<TestCaseSupplier> makeTestCases(String title, Supplier<String> text, int expectedLength) {
Expand Down Expand Up @@ -87,10 +87,6 @@ private static List<TestCaseSupplier> makeTestCases(String title, Supplier<Strin
);
}

private Matcher<Object> resultsMatcher(List<TestCaseSupplier.TypedData> typedData) {
return equalTo(UnicodeUtil.codePointCount((BytesRef) typedData.get(0).data()));
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Length(source, args.get(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class RightErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(RightTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Right(source, args.get(0), args.get(1));
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(true, validPerPosition, signature, (v, p) -> switch (p) {
case 0 -> "string";
case 1 -> "integer";
default -> "";
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ public static Iterable<Object[]> parameters() {
equalTo(new BytesRef(unicodeRightSubstring(text, length)))
);
}));
return parameterSuppliersFromTypedDataWithDefaultChecks(true, suppliers, (v, p) -> switch (p) {
case 0 -> "string";
case 1 -> "integer";
default -> throw new IllegalStateException("bad parameter number");
});
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
}

private static String unicodeRightSubstring(String str, int length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class SpaceErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(SpaceTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Space(source, args.get(0));
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(false, validPerPosition, signature,(v, p) -> "integer"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static Iterable<Object[]> parameters() {
.withFoldingException(IllegalArgumentException.class, "Creating strings longer than [" + max + "] bytes is not supported");
}));

return parameterSuppliersFromTypedDataWithDefaultChecks(true, cases, (v, p) -> "integer");
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, cases);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.EsqlTestUtils;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class ToLowerErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(ToLowerTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new ToLower(source, args.get(0), EsqlTestUtils.TEST_CFG);
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(false, validPerPosition, signature,(v, p) -> "string"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public static Iterable<Object[]> parameters() {
suppliers.add(supplier("text unicode", DataType.TEXT, () -> randomUnicodeOfLengthBetween(1, 10)));
suppliers.add(supplier("semantic_text ascii", DataType.SEMANTIC_TEXT, () -> randomAlphaOfLengthBetween(1, 10)));
suppliers.add(supplier("semantic_text unicode", DataType.SEMANTIC_TEXT, () -> randomUnicodeOfLengthBetween(1, 10)));

// add null as parameter
return parameterSuppliersFromTypedDataWithDefaultChecks(true, suppliers, (v, p) -> "string");
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
}

public void testRandomLocale() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.string;

import org.elasticsearch.xpack.esql.EsqlTestUtils;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class ToUpperErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
@Override
protected List<TestCaseSupplier> cases() {
return paramsToSuppliers(ToUpperTests.parameters());
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new ToUpper(source, args.get(0), EsqlTestUtils.TEST_CFG);
}

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(false, validPerPosition, signature,(v, p) -> "string"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public static Iterable<Object[]> parameters() {
suppliers.add(supplier("text unicode", DataType.TEXT, () -> randomUnicodeOfLengthBetween(1, 10)));
suppliers.add(supplier("semantic_text ascii", DataType.SEMANTIC_TEXT, () -> randomAlphaOfLengthBetween(1, 10)));
suppliers.add(supplier("semantic_text unicode", DataType.SEMANTIC_TEXT, () -> randomUnicodeOfLengthBetween(1, 10)));

// add null as parameter
return parameterSuppliersFromTypedDataWithDefaultChecks(true, suppliers, (v, p) -> "string");
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
}

public void testRandomLocale() {
Expand Down

0 comments on commit 480d273

Please sign in to comment.