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 f2ca17e2743bb..52e7d8cdd09da 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 @@ -94,28 +94,28 @@ public abstract class AbstractFunctionTestCase extends ESTestCase { * Generate a random value of the appropriate type to fit into blocks of {@code e}. */ public static Literal randomLiteral(DataType type) { - return new Literal(Source.EMPTY, switch (type.typeName()) { - case "boolean" -> randomBoolean(); - case "byte" -> randomByte(); - case "short" -> randomShort(); - case "integer", "counter_integer" -> randomInt(); - case "unsigned_long", "long", "counter_long" -> randomLong(); - case "date_period" -> Period.of(randomIntBetween(-1000, 1000), randomIntBetween(-13, 13), randomIntBetween(-32, 32)); - case "datetime" -> randomMillisUpToYear9999(); - case "double", "scaled_float", "counter_double" -> randomDouble(); - case "float" -> randomFloat(); - case "half_float" -> HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat())); - case "keyword" -> new BytesRef(randomAlphaOfLength(5)); - case "ip" -> new BytesRef(InetAddressPoint.encode(randomIp(randomBoolean()))); - case "time_duration" -> Duration.ofMillis(randomLongBetween(-604800000L, 604800000L)); // plus/minus 7 days - case "text" -> new BytesRef(randomAlphaOfLength(50)); - case "version" -> randomVersion().toBytesRef(); - case "geo_point" -> GEO.asWkb(GeometryTestUtils.randomPoint()); - case "cartesian_point" -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint()); - case "geo_shape" -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean())); - case "cartesian_shape" -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean())); - case "null" -> null; - case "_source" -> { + return new Literal(Source.EMPTY, switch (type) { + case BOOLEAN -> randomBoolean(); + case BYTE -> randomByte(); + case SHORT -> randomShort(); + case INTEGER, COUNTER_INTEGER -> randomInt(); + case UNSIGNED_LONG, LONG, COUNTER_LONG -> randomLong(); + case DATE_PERIOD -> Period.of(randomIntBetween(-1000, 1000), randomIntBetween(-13, 13), randomIntBetween(-32, 32)); + case DATETIME -> randomMillisUpToYear9999(); + case DOUBLE, SCALED_FLOAT, COUNTER_DOUBLE -> randomDouble(); + case FLOAT -> randomFloat(); + case HALF_FLOAT -> HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat())); + case KEYWORD -> new BytesRef(randomAlphaOfLength(5)); + case IP -> new BytesRef(InetAddressPoint.encode(randomIp(randomBoolean()))); + case TIME_DURATION -> Duration.ofMillis(randomLongBetween(-604800000L, 604800000L)); // plus/minus 7 days + case TEXT -> new BytesRef(randomAlphaOfLength(50)); + case VERSION -> randomVersion().toBytesRef(); + case GEO_POINT -> GEO.asWkb(GeometryTestUtils.randomPoint()); + case CARTESIAN_POINT -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint()); + case GEO_SHAPE -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean())); + case CARTESIAN_SHAPE -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean())); + case NULL -> null; + case SOURCE -> { try { yield BytesReference.bytes( JsonXContent.contentBuilder().startObject().field(randomAlphaOfLength(3), randomAlphaOfLength(10)).endObject() @@ -124,7 +124,9 @@ public static Literal randomLiteral(DataType type) { throw new UncheckedIOException(e); } } - default -> throw new IllegalArgumentException("can't make random values for [" + type.typeName() + "]"); + case UNSUPPORTED, OBJECT, NESTED, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG -> throw new IllegalArgumentException( + "can't make random values for [" + type.typeName() + "]" + ); }, type); }