fix(athena): read Iceberg-format Glue tables via iceberg_scan (#3738)

* feat(athena): read Iceberg-format Glue tables via iceberg_scan

Glue tables written by pyiceberg's GlueCatalog (Parameters.table_type=
ICEBERG) never populate StorageDescriptor.InputFormat/SerializationLibrary,
so GlueViewDdlBuilder's format-sniffing heuristic always fell through to
read_csv_auto and failed parsing the table's binary Parquet data files.

Detect Iceberg tables via Parameters.table_type and build the view from
iceberg_scan(Parameters.metadata_location) instead, following the table's
real manifest list rather than globbing every Parquet file physically
present under its location -- which also fixes silently wrong results for
any table with more than one snapshot (updates, deletes, repeated
appends), not just the CSV-parse failure on a single-snapshot table.

The iceberg extension is installed/loaded once, only when the DDL batch
actually contains an Iceberg table. A table flagged ICEBERG but missing
metadata_location falls back to the existing format-sniffed heuristic.

Verified against a live build: pyiceberg write -> Athena SELECT returns
correct rows for both a single-snapshot and a two-snapshot table; full
athena + glue test suites (352 tests) pass with no regressions.

Fixes #3737

This change was implemented by Claude (Anthropic's AI coding assistant),
working on behalf of @maurice1979 who filed the linked issue.

* docs(athena): fix formatting and clarify Iceberg handling explanation

Corrected formatting and clarified the explanation regarding Iceberg
tables and their handling in DuckDB.

Signed-off-by: Jordi Vidal de LLobatera <is.maurice@protonmail.com>

* docs(glue): cross-reference Iceberg handling from glue.md; fix em dash

docs/services/glue.md still described only the InputFormat/SerdeInfo
format-sniffing heuristic for choosing a DuckDB read function, without
mentioning that Iceberg-format tables (Parameters.table_type=ICEBERG)
bypass it entirely via iceberg_scan, per the change in the previous
commit. Added a short cross-reference to Athena's format-inference docs
so glue.md doesn't describe stale, pre-fix behavior.

Also removed an em dash from GlueViewDdlBuilder's new Javadoc, per this
repo's own AGENTS.md style rule (no em dashes; colons, commas, or
periods instead). The matching instance in docs/services/athena.md was
already fixed via the Copilot autofix suggestion.

Both findings came from the Copilot pull request review on #3738.

This change was implemented by Claude (Anthropic's AI coding assistant),
working on behalf of @maurice1979.

---------

Signed-off-by: Jordi Vidal de LLobatera <is.maurice@protonmail.com>
This commit is contained in:
Jordi Vidal de LLobatera authored and GitHub committed 2026-09-17 10:54:20 -04:00
1 parent dfbb0ee756
commit 9740b32b51
4 files changed
+136 -10

No files matched your search

@@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -315,5 +316,73 @@ class GlueViewDdlBuilderTest {
assertTrue(ddl.contains("CREATE OR REPLACE VIEW \"orders\""));
Mockito.verify(glueService, Mockito.times(1)).getTables("shop");
}
private Table icebergTable(String name, String location, String metadataLocation) {
Table table = createTable(name, location, null, null);
table.setParameters(metadataLocation == null
? Map.of("table_type", "ICEBERG")
: Map.of("table_type", "ICEBERG", "metadata_location", metadataLocation));
return table;
}
/**
* pyiceberg's GlueCatalog (and AWS's own Glue-Iceberg integration) never populate
* InputFormat/SerializationLibrary, so an Iceberg table must be detected from
* Parameters.table_type rather than format-sniffed like every other table kind.
*/
@Test
void testIcebergTableUsesIcebergScanOnMetadataLocation() {
Table table = icebergTable("orders", "s3://bucket/warehouse/orders",
"s3://bucket/warehouse/orders/metadata/00001-abc.metadata.json");
String ddl = ddlFor(table);
assertTrue(ddl.contains(
"CREATE OR REPLACE VIEW \"audit\".\"orders\" AS SELECT * FROM "
+ "iceberg_scan('s3://bucket/warehouse/orders/metadata/00001-abc.metadata.json');\n"),
ddl);
}
/** The iceberg extension is expensive to install, so it's only requested when actually needed. */
@Test
void testIcebergExtensionSetupOnlyEmittedWhenTableIsIceberg() {
Table icebergTable = icebergTable("orders", "s3://bucket/orders",
"s3://bucket/orders/metadata/00000.metadata.json");
String ddlWithIceberg = ddlFor(icebergTable);
assertTrue(ddlWithIceberg.startsWith("INSTALL iceberg; LOAD iceberg;\n"), ddlWithIceberg);
Table plainTable = createTable("orders", "s3://bucket/orders", null, null);
String ddlWithoutIceberg = ddlFor(plainTable);
assertFalse(ddlWithoutIceberg.contains("iceberg"), ddlWithoutIceberg);
}
/** A malformed Iceberg table (flagged as Iceberg but missing metadata_location) falls back
* to format inference rather than emitting an unusable iceberg_scan('') call. */
@Test
void testIcebergTableWithoutMetadataLocationFallsBackToFormatInference() {
Table table = icebergTable("orders", "s3://bucket/orders", null);
String ddl = ddlFor(table);
assertTrue(ddl.contains("FROM read_csv_auto('s3://bucket/orders/**');\n"), ddl);
assertFalse(ddl.contains("iceberg_scan"), ddl);
}
@Test
void testIsIcebergTableDetection() {
assertFalse(GlueViewDdlBuilder.isIcebergTable(null));
assertFalse(GlueViewDdlBuilder.isIcebergTable(createTable("t", "s3://b/t", null, null)));
Table table = createTable("t", "s3://b/t", null, null);
table.setParameters(Map.of("table_type", "iceberg"));
assertTrue(GlueViewDdlBuilder.isIcebergTable(table), "table_type comparison should be case-insensitive");
table.setParameters(Map.of("table_type", "EXTERNAL_TABLE"));
assertFalse(GlueViewDdlBuilder.isIcebergTable(table));
}
@Test
void testIcebergReadExpressionEscapesSingleQuotes() {
assertEquals("iceberg_scan('s3://bucket/it''s-a-table/metadata/x.json')",
GlueViewDdlBuilder.icebergReadExpression("s3://bucket/it's-a-table/metadata/x.json"));
}
}