From d58da2c1056733df335a43beb4f0ca1078b808eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Cea=20Fontenla?= Date: Thu, 13 Jun 2024 11:01:42 +0200 Subject: [PATCH] ESQL: Added BytesRef offset randomization to function tests (#109207) * Added BytesRef offset randomization to ESQL function tests * Added extra array length and randomized paddings --- .../function/AbstractFunctionTestCase.java | 40 +++++++++++++++++++ .../function/scalar/ip/IpPrefixTests.java | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java index 20c2b6df9710a..f27438de6df6b 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java @@ -641,6 +641,46 @@ public interface ExpectedEvaluatorToString { Matcher evaluatorToString(int nullPosition, TestCaseSupplier.TypedData nullData, Matcher original); } + /** + * Modifies suppliers to generate BytesRefs with random offsets. + */ + protected static List randomizeBytesRefsOffset(List testCaseSuppliers) { + return testCaseSuppliers.stream().map(supplier -> new TestCaseSupplier(supplier.name(), supplier.types(), () -> { + var testCase = supplier.supplier().get(); + + var newData = testCase.getData().stream().map(typedData -> { + if (typedData.data() instanceof BytesRef bytesRef) { + var offset = randomIntBetween(0, 10); + var extraLength = randomIntBetween(0, 10); + var newBytesArray = randomByteArrayOfLength(bytesRef.length + offset + extraLength); + + System.arraycopy(bytesRef.bytes, bytesRef.offset, newBytesArray, offset, bytesRef.length); + + var newBytesRef = new BytesRef(newBytesArray, offset, bytesRef.length); + var newTypedData = new TestCaseSupplier.TypedData(newBytesRef, typedData.type(), typedData.name()); + + if (typedData.isForceLiteral()) { + newTypedData.forceLiteral(); + } + + return newTypedData; + } + return typedData; + }).toList(); + + return new TestCaseSupplier.TestCase( + newData, + testCase.evaluatorToString(), + testCase.expectedType(), + testCase.getMatcher(), + testCase.getExpectedWarnings(), + testCase.getExpectedTypeError(), + testCase.foldingExceptionClass(), + testCase.foldingExceptionMessage() + ); + })).toList(); + } + protected static List anyNullIsNull( List testCaseSuppliers, ExpectedType expectedType, diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/IpPrefixTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/IpPrefixTests.java index 063a057134d7e..d2b5e0a455229 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/IpPrefixTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/IpPrefixTests.java @@ -106,7 +106,7 @@ public static Iterable parameters() { }) ); - return parameterSuppliersFromTypedData(errorsForCasesWithoutExamples(anyNullIsNull(true, suppliers))); + return parameterSuppliersFromTypedData(errorsForCasesWithoutExamples(anyNullIsNull(true, randomizeBytesRefsOffset(suppliers)))); } @Override