From 21e89fe7ffd9ed9b7312e7ca3e6a5a9f26bc3434 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Fri, 4 Dec 2026 13:13:35 +0900 Subject: [PATCH] test(athena): share the Trino query helpers across the sidecar tests (#10131) The date-time and JSON function tests each had the same private row(sql) helper, and jsonResultColumnsAreTypedAsAthenaTypesThem ran the same translated DESCRIBE and athenaType mapping as the date-time tests' types(sql). Both helpers move to TrinoSidecarQueries, which the two classes call. --- ...teTimeFunctionsSidecarIntegrationTest.java | 55 ++++++++----------- ...noJsonFunctionsSidecarIntegrationTest.java | 27 ++++----- .../services/athena/TrinoSidecarQueries.java | 29 ++++++++++ 3 files changed, 64 insertions(+), 47 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/athena/TrinoSidecarQueries.java diff --git a/src/test/java/io/floci/aws/services/athena/AthenaTrinoDateTimeFunctionsSidecarIntegrationTest.java b/src/test/java/io/floci/aws/services/athena/AthenaTrinoDateTimeFunctionsSidecarIntegrationTest.java index 8b1faa1dd5..0029975c55 100644 --- a/src/test/java/io/floci/aws/services/athena/AthenaTrinoDateTimeFunctionsSidecarIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/athena/AthenaTrinoDateTimeFunctionsSidecarIntegrationTest.java @@ -11,6 +11,8 @@ import org.junit.jupiter.api.Test; import java.util.List; import java.util.Map; +import static io.floci.aws.services.athena.TrinoSidecarQueries.row; +import static io.floci.aws.services.athena.TrinoSidecarQueries.types; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -32,20 +34,9 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { SidecarContainersProfile.requireDocker(); } - private Map row(String trinoSql) { - return duck.query(TrinoSqlCompatibility.translate(trinoSql), TrinoSqlCompatibility.FUNCTION_MACROS).getFirst(); - } - - private List types(String trinoSql) { - return duck.query("DESCRIBE " + TrinoSqlCompatibility.translate(trinoSql), TrinoSqlCompatibility.FUNCTION_MACROS) - .stream() - .map(column -> AthenaService.athenaType(String.valueOf(column.get("column_type")))) - .toList(); - } - @Test void fromIso8601TimestampReadsADateTimeWithAnOffsetAsThatInstant() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "from_iso8601_timestamp('2026-12-02T08:22:50Z') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS utc, " + "from_iso8601_timestamp('2026-12-02T17:22:50+09:00') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS ahead, " + "from_iso8601_timestamp('2026-12-02T03:22:50-05:00') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS behind, " @@ -61,7 +52,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void fromIso8601TimestampReadsAValueWithoutAnOffsetAsUtc() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "from_iso8601_timestamp('2026-12-02T08:22:50') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS date_time, " + "from_iso8601_timestamp('2026-12-02') = TIMESTAMP '2026-12-02 00:00:00 UTC' AS date_only"); @@ -83,7 +74,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void fromIso8601TimestampReadsATimeWrittenToTheMinute() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "from_iso8601_timestamp('2026-12-02T08:22Z') = TIMESTAMP '2026-12-02 08:22:00 UTC' AS utc, " + "from_iso8601_timestamp('2026-12-02T17:22+09:00') = TIMESTAMP '2026-12-02 08:22:00 UTC' AS ahead, " + "from_iso8601_timestamp('2026-12-02T03:22-05:00') = TIMESTAMP '2026-12-02 08:22:00 UTC' AS behind, " @@ -98,7 +89,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { /** Trino's result is a {@code timestamp(3) with time zone}: Athena returns {@code .946} for {@code .946123}. */ @Test void fromIso8601TimestampTruncatesAFractionToMilliseconds() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "from_iso8601_timestamp('2026-12-02T08:22:50.946123Z') = from_iso8601_timestamp('2026-12-02T08:22:50.946Z') AS micros, " + "from_iso8601_timestamp('2026-12-02T08:22:50.9469Z') = from_iso8601_timestamp('2026-12-02T08:22:50.946Z') AS not_rounded, " + "from_iso8601_timestamp('1969-12-31T23:59:59.9995Z') = from_iso8601_timestamp('1969-12-31T23:59:59.999Z') AS before_epoch, " @@ -112,12 +103,12 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void fromIso8601TimestampIsNullForANullArgument() { - assertNull(row("SELECT from_iso8601_timestamp(NULL) AS t").get("t")); + assertNull(row(duck, "SELECT from_iso8601_timestamp(NULL) AS t").get("t")); } @Test void fromIso8601TimestampRejectsATextThatIsNotATimestamp() { - assertThrows(RuntimeException.class, () -> row("SELECT from_iso8601_timestamp('not a timestamp') AS t")); + assertThrows(RuntimeException.class, () -> row(duck, "SELECT from_iso8601_timestamp('not a timestamp') AS t")); } @Test @@ -135,12 +126,13 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void fromIso8601TimestampIsTypedTimestampWithTimeZone() { - assertEquals(List.of("timestamp with time zone"), types("SELECT from_iso8601_timestamp('2026-12-02T08:22:50Z') AS t")); + assertEquals(List.of("timestamp with time zone"), + types(duck, "SELECT from_iso8601_timestamp('2026-12-02T08:22:50Z') AS t")); } @Test void withTimezoneReadsATimestampAsTheWallTimeOfTheZone() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "with_timezone(TIMESTAMP '2026-12-02 08:22:50.946', 'UTC') = TIMESTAMP '2026-12-02 08:22:50.946 UTC' AS utc, " + "with_timezone(TIMESTAMP '2026-12-02 17:22:50', 'Asia/Tokyo') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS ahead, " + "with_timezone(TIMESTAMP '2026-12-02 03:22:50', 'America/New_York') = TIMESTAMP '2026-12-02 08:22:50 UTC' AS behind"); @@ -152,7 +144,8 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void withTimezoneIsTypedTimestampWithTimeZone() { - assertEquals(List.of("timestamp with time zone"), types("SELECT with_timezone(TIMESTAMP '2026-12-02 08:22:50', 'UTC') AS t")); + assertEquals(List.of("timestamp with time zone"), + types(duck, "SELECT with_timezone(TIMESTAMP '2026-12-02 08:22:50', 'UTC') AS t")); } /** @@ -162,7 +155,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { */ @Test void withTimezoneReadsAWallTimeAClockChangeRepeatsOrSkipsAsAthenaDoes() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "to_iso8601(with_timezone(TIMESTAMP '2026-11-01 00:30:00', 'America/New_York')) AS before_repeat, " + "to_iso8601(with_timezone(TIMESTAMP '2026-11-01 01:30:00', 'America/New_York')) AS repeated, " + "to_iso8601(with_timezone(TIMESTAMP '2026-11-01 02:30:00', 'America/New_York')) AS after_repeat, " @@ -181,7 +174,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { /** Athena coerces a date to a timestamp at midnight. */ @Test void withTimezoneReadsADateAsItsMidnight() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "with_timezone(DATE '2026-12-02', 'UTC') = TIMESTAMP '2026-12-02 00:00:00 UTC' AS utc, " + "with_timezone(DATE '2026-12-02', 'Asia/Tokyo') = TIMESTAMP '2026-12-01 15:00:00 UTC' AS ahead"); @@ -193,29 +186,29 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void withTimezoneRejectsATextAndAZonedTimestamp() { RuntimeException text = assertThrows(RuntimeException.class, - () -> row("SELECT with_timezone('2026-12-02 08:22:50', 'UTC') AS t")); + () -> row(duck, "SELECT with_timezone('2026-12-02 08:22:50', 'UTC') AS t")); assertTrue(text.getMessage().contains("FUNCTION_NOT_FOUND: Unexpected parameters (varchar, varchar) for" + " function with_timezone. Expected: with_timezone(timestamp(p), varchar(x))"), text.getMessage()); RuntimeException zoned = assertThrows(RuntimeException.class, - () -> row("SELECT with_timezone(from_iso8601_timestamp('2026-12-02T08:22:50Z'), 'Asia/Tokyo') AS t")); + () -> row(duck, "SELECT with_timezone(from_iso8601_timestamp('2026-12-02T08:22:50Z'), 'Asia/Tokyo') AS t")); assertTrue(zoned.getMessage().contains("Unexpected parameters (timestamp with time zone, varchar) for" + " function with_timezone."), zoned.getMessage()); } @Test void withTimezoneIsNullForANullTimestamp() { - assertNull(row("SELECT with_timezone(CAST(NULL AS TIMESTAMP), 'UTC') AS t").get("t")); + assertNull(row(duck, "SELECT with_timezone(CAST(NULL AS TIMESTAMP), 'UTC') AS t").get("t")); } @Test void withTimezoneRejectsAZoneThatDoesNotExist() { assertThrows(RuntimeException.class, - () -> row("SELECT with_timezone(TIMESTAMP '2026-12-02 08:22:50', 'Nowhere/Zone') AS t")); + () -> row(duck, "SELECT with_timezone(TIMESTAMP '2026-12-02 08:22:50', 'Nowhere/Zone') AS t")); } @Test void toIso8601WritesAZonedTimestampInUtcWithMilliseconds() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "to_iso8601(with_timezone(TIMESTAMP '2026-12-02 08:22:50', 'UTC')) AS seconds, " + "to_iso8601(with_timezone(TIMESTAMP '2026-12-02 08:22:50.946', 'UTC')) AS millis, " + "to_iso8601(with_timezone(TIMESTAMP '2026-12-02 17:22:50', 'Asia/Tokyo')) AS named, " @@ -229,7 +222,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void toIso8601WritesATimestampWithoutAZoneAndADate() { - Map row = row("SELECT " + Map row = row(duck, "SELECT " + "to_iso8601(TIMESTAMP '2026-12-02 08:22:50.946') AS plain, " + "to_iso8601(TIMESTAMP '1969-12-31 23:59:59.9995') AS before_epoch, " + "to_iso8601(DATE '2026-12-02') AS date_only"); @@ -241,7 +234,7 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { @Test void toIso8601IsNullForANullArgument() { - Map row = row("SELECT to_iso8601(NULL) AS untyped, " + Map row = row(duck, "SELECT to_iso8601(NULL) AS untyped, " + "to_iso8601(with_timezone(CAST(NULL AS TIMESTAMP), 'UTC')) AS zoned"); assertNull(row.get("untyped")); @@ -254,10 +247,10 @@ class AthenaTrinoDateTimeFunctionsSidecarIntegrationTest { String expected = "FUNCTION_NOT_FOUND: Unexpected parameters (varchar) for function to_iso8601. Expected:" + " to_iso8601(date), to_iso8601(timestamp(p) with time zone), to_iso8601(timestamp(p))"; RuntimeException literal = assertThrows(RuntimeException.class, - () -> row("SELECT to_iso8601('2026-12-02') AS t")); + () -> row(duck, "SELECT to_iso8601('2026-12-02') AS t")); assertTrue(literal.getMessage().contains(expected), literal.getMessage()); RuntimeException column = assertThrows(RuntimeException.class, - () -> row("SELECT to_iso8601(c) AS t FROM (VALUES ('2026-12-02')) AS v(c)")); + () -> row(duck, "SELECT to_iso8601(c) AS t FROM (VALUES ('2026-12-02')) AS v(c)")); assertTrue(column.getMessage().contains(expected), column.getMessage()); } diff --git a/src/test/java/io/floci/aws/services/athena/AthenaTrinoJsonFunctionsSidecarIntegrationTest.java b/src/test/java/io/floci/aws/services/athena/AthenaTrinoJsonFunctionsSidecarIntegrationTest.java index 64ba9700df..ab9b8c1392 100644 --- a/src/test/java/io/floci/aws/services/athena/AthenaTrinoJsonFunctionsSidecarIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/athena/AthenaTrinoJsonFunctionsSidecarIntegrationTest.java @@ -11,6 +11,8 @@ import org.junit.jupiter.api.Test; import java.util.List; import java.util.Map; +import static io.floci.aws.services.athena.TrinoSidecarQueries.row; +import static io.floci.aws.services.athena.TrinoSidecarQueries.types; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -30,13 +32,9 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { SidecarContainersProfile.requireDocker(); } - private Map row(String trinoSql) { - return duck.query(TrinoSqlCompatibility.translate(trinoSql), TrinoSqlCompatibility.FUNCTION_MACROS).getFirst(); - } - @Test void jsonExtractScalarReturnsScalarsAsTextAndNullForAnythingElse() { - Map row = row("SELECT json_extract_scalar('{\"a\":\"x\"}', '$.a') AS s, " + Map row = row(duck, "SELECT json_extract_scalar('{\"a\":\"x\"}', '$.a') AS s, " + "json_extract_scalar('{\"a\":2}', '$.a') AS n, json_extract_scalar('{\"a\":true}', '$.a') AS b, " + "json_extract_scalar('{\"a\":{\"b\":1}}', '$.a') AS o, json_extract_scalar('{\"a\":[1]}', '$.a') AS arr, " + "json_extract_scalar('{\"a\":null}', '$.a') AS nul, json_extract_scalar('{\"a\":1}', '$.b') AS missing, " @@ -54,7 +52,7 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { @Test void jsonParseFormatSizeAndScalarCheckAnswerAsTrinoDoes() { - Map row = row("SELECT json_format(json_parse('{ \"a\" : [1, 2] }')) AS f, " + Map row = row(duck, "SELECT json_format(json_parse('{ \"a\" : [1, 2] }')) AS f, " + "json_size('{\"x\":[1,2,3]}', '$.x') AS arr, json_size('{\"x\":{\"a\":1}}', '$.x') AS obj, " + "json_size('{\"x\":1}', '$.x') AS scalar, is_json_scalar('1') AS yes, is_json_scalar('[1]') AS no, " + "json_array_length('[1,2,3]') AS len, json_extract_scalar(json_extract('{\"a\":{\"b\":\"c\"}}', '$.a'), '$.b') AS e"); @@ -71,7 +69,7 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { @Test void jsonSizeIsNullForANullArgumentOrAMissingPathAndZeroForAJsonNull() { - Map row = row("SELECT json_size(NULL, '$.x') AS null_json, json_size('{\"x\":1}', NULL) AS null_path, " + Map row = row(duck, "SELECT json_size(NULL, '$.x') AS null_json, json_size('{\"x\":1}', NULL) AS null_path, " + "json_size('{\"x\":1}', '$.y') AS missing, json_size('{\"x\":null}', '$.x') AS json_null"); assertNull(row.get("null_json")); @@ -82,7 +80,7 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { @Test void aBracketedMemberNameCanHoldAnEscapedQuoteOrASlash() { - Map row = row("SELECT json_extract_scalar('{\"a\\\"b\":7}', '$[\"a\\\"b\"]') AS quote, " + Map row = row(duck, "SELECT json_extract_scalar('{\"a\\\"b\":7}', '$[\"a\\\"b\"]') AS quote, " + "json_extract_scalar('{\"a/b\":{\"c~d\":8}}', '$[\"a/b\"][\"c~d\"]') AS slash"); assertEquals("7", row.get("quote")); @@ -101,7 +99,7 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { @Test void aCastToAnUnnamedRowTypeRuns() { - Map row = row("SELECT typeof(CAST(ROW(1, 'x') AS ROW(BIGINT, VARCHAR))) AS t"); + Map row = row(duck, "SELECT typeof(CAST(ROW(1, 'x') AS ROW(BIGINT, VARCHAR))) AS t"); assertEquals("STRUCT(field0 BIGINT, field1 VARCHAR)", row.get("t")); } @@ -119,13 +117,10 @@ class AthenaTrinoJsonFunctionsSidecarIntegrationTest { @Test void jsonResultColumnsAreTypedAsAthenaTypesThem() { - List types = duck.query("DESCRIBE " + TrinoSqlCompatibility.translate( - "SELECT json_parse('[1]') AS p, json_extract_scalar('{\"a\":1}', '$.a') AS s, " - + "json_array_length('[1]') AS n, json_size('[1]', '$') AS z, " - + "CAST(json_parse('[1]') AS ARRAY) AS a"), TrinoSqlCompatibility.FUNCTION_MACROS) - .stream() - .map(column -> AthenaService.athenaType(String.valueOf(column.get("column_type")))) - .toList(); + List types = types(duck, + "SELECT json_parse('[1]') AS p, json_extract_scalar('{\"a\":1}', '$.a') AS s, " + + "json_array_length('[1]') AS n, json_size('[1]', '$') AS z, " + + "CAST(json_parse('[1]') AS ARRAY) AS a"); assertEquals(List.of("json", "varchar", "bigint", "bigint", "array"), types); } diff --git a/src/test/java/io/floci/aws/services/athena/TrinoSidecarQueries.java b/src/test/java/io/floci/aws/services/athena/TrinoSidecarQueries.java new file mode 100644 index 0000000000..9391ded8a9 --- /dev/null +++ b/src/test/java/io/floci/aws/services/athena/TrinoSidecarQueries.java @@ -0,0 +1,29 @@ +package io.floci.aws.services.athena; + +import io.floci.aws.services.floci.duck.FlociDuckClient; + +import java.util.List; +import java.util.Map; + +/** + * Trino SQL run on the floci-duck sidecar the way Athena runs a query that reads no table: translated by + * {@link TrinoSqlCompatibility} and answered with its function macros. + */ +final class TrinoSidecarQueries { + + private TrinoSidecarQueries() { + } + + /** The first row {@code trinoSql} returns. */ + static Map row(FlociDuckClient duck, String trinoSql) { + return duck.query(TrinoSqlCompatibility.translate(trinoSql), TrinoSqlCompatibility.FUNCTION_MACROS).getFirst(); + } + + /** The Athena type of each column {@code trinoSql} returns. */ + static List types(FlociDuckClient duck, String trinoSql) { + return duck.query("DESCRIBE " + TrinoSqlCompatibility.translate(trinoSql), TrinoSqlCompatibility.FUNCTION_MACROS) + .stream() + .map(column -> AthenaService.athenaType(String.valueOf(column.get("column_type")))) + .toList(); + } +}