fix(athena): type Hive partition values as their keys declare (#8590)

Athena types a partition value as its key declares. Floci left the
values DuckDB's Hive partitioning reads from column=value folders to
DuckDB's autocast, so a string key holding 2026-11-14 came back as a
DATE, and a date key's folder holding text no date is read as rows with
the guessed type. Delimited-text tables already read their values as
text and cast them; every other format did not.

- A partitioned table without a location template reads with
  hive_types_autocast = false, so each Hive partition value arrives as
  text, and buildProjection casts it to its key's declared type
  (GlueTableResolver.hivePartitionKeys), as it already did for
  delimited text.
- A projecting table in the default layout keeps only rows whose typed
  keys parse as their types (hiveValueFilter): Athena builds each path
  from values its projection generates, so a folder like dt=latest for
  a date key is one no query addresses, as the template layout already
  treats it.
- A date or timestamp key whose date projection sets a format is parsed
  with it in either layout (typedKey, strptimeFormat translating yyyy,
  yy, MM, dd, HH, mm, ss and literal text, a doubled quote being one
  quote, for try_strptime), so dt=20261116
  under yyyyMMdd reads as a date; a format with other pattern letters is
  cast as an ISO value. This closes the template layout's TODO too.
- Iceberg tables keep reading their own typed values.

AthenaPartitionTemplateSidecarIntegrationTest reads a string key
holding a date as VARCHAR, a date key as DATE, and skips a dt=latest
folder, and a yyyyMMdd date projection, against the real engine.
GlueViewDdlBuilderTest and GlueTableResolverTest cover the view and the
pattern translation.
athena.md says so.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 11:08:45 -04:00
1 parent bba2ac901e
commit 6fd20879d5
5 files changed
+212 -15

No files matched your search

@@ -92,6 +92,37 @@ class AthenaPartitionTemplateSidecarIntegrationTest {
assertEquals(EXPECTED_ROW, queryEuInMarch("sales_hive"));
}
/**
* In the default layout each value takes its key's declared type, not the one DuckDB would guess from the folder
* name: a string key holding a date stays a string, and a date key's folder holding a value no date is is one
* projection never generates, so it is not read.
*/
@Test
void defaultLayoutTypesEachValueAsItsKeyDeclares() {
table("sales_typed", "typed", null, "typed/region=2026-03-14/dt=2026-03-14/part.parquet",
"typed/region=eu/dt=latest/part.parquet");
assertEquals(List.of(Map.of("region", "2026-03-14", "region_type", "VARCHAR", "dt_type", "DATE")),
query("SELECT region, typeof(region) AS region_type, typeof(dt) AS dt_type FROM "
+ view("sales_typed")));
}
/** A date projected with a format in the default layout is read through that format: dt=20260314 is March 14. */
@Test
void defaultLayoutParsesAFormattedDateProjection() {
Map<String, String> parameters = new LinkedHashMap<>();
parameters.put("classification", "parquet");
parameters.put("projection.enabled", "true");
parameters.put("projection.region.type", "injected");
parameters.put("projection.dt.type", "date");
parameters.put("projection.dt.format", "yyyyMMdd");
parameters.put("projection.dt.range", "20260101,NOW");
register("sales_compact", "compact", parameters, List.of(column("region", "string"), column("dt", "date")),
"compact/region=eu/dt=20260314/part.parquet");
assertEquals(EXPECTED_ROW, queryEuInMarch("sales_compact"));
}
@Test
void templateWithBareValuesMapsEachFolderToItsKey() {
table("sales_bare", "bare", "bare/${region}/${dt}/", "bare/eu/2026-03-14/part.parquet");
@@ -246,15 +246,39 @@ class GlueViewDdlBuilderTest {
ddl);
}
/** The default column=value layout is left to DuckDB's Hive partitioning, as before. */
/**
* The default column=value layout takes its values from DuckDB's Hive partitioning, read as text and given each
* key's declared type rather than the one DuckDB would guess; a folder whose value the type cannot hold is one
* no projected query addresses, so its rows are not read.
*/
@Test
void defaultLayoutStillReadsPartitionValuesThroughHivePartitioning() {
void defaultLayoutTypesEachPartitionValueAsItsKeyDeclares() {
String ddl = ddlFor(projectingParquetTable("s3://b/hive/", Map.of("projection.enabled", "true")));
assertEquals("CREATE SCHEMA IF NOT EXISTS \"audit\";\n"
+ "CREATE OR REPLACE VIEW \"audit\".\"events\" AS SELECT \"id\" AS \"id\", "
+ "\"region\" AS \"region\", \"dt\" AS \"dt\" "
+ "FROM read_parquet('s3://b/hive/region=*/dt=*/**', union_by_name = true);\n", ddl);
+ "\"region\" AS \"region\", TRY_CAST(\"dt\" AS DATE) AS \"dt\" "
+ "FROM read_parquet('s3://b/hive/region=*/dt=*/**', union_by_name = true, hive_types_autocast = false) "
+ "WHERE TRY_CAST(\"dt\" AS DATE) IS NOT NULL;\n", ddl);
}
/**
* A date key projected with a format is parsed with that format, in either layout, as Athena reads the value it
* generated: {@code dt=20261116} for {@code yyyyMMdd} is a date, not a value the key's type cannot hold.
*/
@Test
void aFormattedDateProjectionParsesItsValuesWithTheFormat() {
String parsed = "TRY_CAST(try_strptime(\"dt\", '%Y%m%d') AS DATE)";
String hive = ddlFor(projectingParquetTable("s3://b/hive/", Map.of("projection.enabled", "true",
"projection.dt.type", "date", "projection.dt.format", "yyyyMMdd")));
assertTrue(hive.contains(parsed + " AS \"dt\""), hive);
assertTrue(hive.contains("WHERE " + parsed + " IS NOT NULL"), hive);
String templated = ddlFor(projectingParquetTable("s3://b/data/", Map.of("projection.enabled", "true",
"projection.dt.type", "date", "projection.dt.format", "yyyyMMdd",
"storage.location.template", "s3://b/data/${region}/${dt}/")));
assertTrue(templated.contains("TRY_CAST(try_strptime(regexp_extract(\"$path\", "
+ "'^s3://b/data/([^/]+)/([^/]+)/', 2), '%Y%m%d') AS DATE) AS \"dt\""), templated);
}
/**
@@ -261,4 +261,17 @@ class GlueTableResolverTest {
assertThat(GlueTableResolver.readPlan(table).fromClause(), equalTo("read_csv_auto('s3://bucket/events/**')"));
}
@Test
void aJavaDatePatternTranslatesToAStrptimeFormat() {
assertThat(GlueTableResolver.strptimeFormat("yyyy/MM/dd/HH"), is("%Y/%m/%d/%H"));
assertThat(GlueTableResolver.strptimeFormat("yyyyMMdd'T'HHmmss"), is("%Y%m%dT%H%M%S"));
assertThat(GlueTableResolver.strptimeFormat("yy-MM-dd 100%"), is("%y-%m-%d 100%%"));
// A doubled quote is one quote, inside a quoted section or outside one, as Java's formatter reads it.
assertThat(GlueTableResolver.strptimeFormat("yyyy'foo''bar'MMdd"), is("%Yfoo'bar%m%d"));
assertThat(GlueTableResolver.strptimeFormat("yyyy''MM"), is("%Y'%m"));
assertThat(GlueTableResolver.strptimeFormat("yyyy'unclosed"), nullValue());
assertThat(GlueTableResolver.strptimeFormat("EEE, dd MMM yyyy"), nullValue());
assertThat(GlueTableResolver.strptimeFormat(" "), nullValue());
}
}