fix(athena): project the column names the Glue table declares (#3414)

Views built from the Glue catalog selected *, so the column names came from
whatever the underlying files happened to spell rather than from the table.
For a JSON table whose bodies use camelCase keys, a view over a table
declaring lowercase columns still reported camelCase, and a client reading
the declared names found nothing.

Athena reports a table's declared column names: the catalog owns the schema
and the SerDe maps the data onto it. A client written against that contract
reads `tenantid` where the file says `tenantId`, gets no match, and sees an
empty result with no error anywhere - the query succeeds, the rows are
there, and every field is blank.

Project the declared columns explicitly instead. Identifier resolution is
case-insensitive here, so a declared name still binds to an inferred one
differing from it only by case. Partition keys are projected alongside the
data columns: they are part of the table's schema and are what partition
predicates filter on. A partition key repeating a data column is projected
once, since binding it twice would make the view ambiguous.

A table that declares no columns keeps *, leaving inference in charge, so
catalogs that never declared a schema are unaffected.
This commit is contained in:
Ovidiu authored and GitHub committed 2026-09-11 14:04:22 -04:00
1 parent c48e81d956
commit ebe9cf2264
2 files changed
+136 -1

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.athena;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.glue.GlueService;
import io.github.hectorvent.floci.services.glue.model.Column;
import io.github.hectorvent.floci.services.glue.model.Database;
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
import io.github.hectorvent.floci.services.glue.model.Table;
@@ -50,6 +51,82 @@ class GlueViewDdlBuilderTest {
return table;
}
private static Column column(String name) {
Column c = new Column();
c.setName(name);
c.setType("string");
return c;
}
private Table tableWithColumns(String name, String location, List<Column> columns, List<Column> partitionKeys) {
Table table = createTable(name, location, "org.openx.data.jsonserde.JsonSerDe", null);
table.getStorageDescriptor().setColumns(columns);
table.setPartitionKeys(partitionKeys);
return table;
}
private String ddlFor(Table table) {
Database db = createDatabase("audit");
when(glueService.getDatabases()).thenReturn(List.of(db));
when(glueService.getTables("audit")).thenReturn(List.of(table));
return builder.build(null);
}
/**
* Athena reports the column names the catalog declares. The files here spell them camelCase, so a
* view built by inference alone would expose the file's spelling and a client reading the declared
* name would find nothing.
*/
@Test
void testDeclaredColumnsAreProjectedInsteadOfStar() {
String ddl = ddlFor(tableWithColumns("audit_events", "s3://audit/",
List.of(column("eventid"), column("tenantid")),
List.of(column("tenant"), column("year"))));
assertTrue(ddl.contains("\"eventid\" AS \"eventid\""), ddl);
assertTrue(ddl.contains("\"tenantid\" AS \"tenantid\""), ddl);
assertFalse(ddl.contains("SELECT * FROM"), ddl);
}
/** Partition keys are part of the schema and are what partition predicates filter on. */
@Test
void testPartitionKeysAreProjectedAlongsideDataColumns() {
String ddl = ddlFor(tableWithColumns("audit_events", "s3://audit/",
List.of(column("eventid")),
List.of(column("tenant"), column("year"), column("month"), column("day"))));
for (String name : List.of("eventid", "tenant", "year", "month", "day")) {
assertTrue(ddl.contains("\"" + name + "\" AS \"" + name + "\""), name + " missing from: " + ddl);
}
}
/** A partition key repeating a data column would bind twice and make the view ambiguous. */
@Test
void testDuplicateColumnAndPartitionKeyIsProjectedOnce() {
String ddl = ddlFor(tableWithColumns("t", "s3://b/",
List.of(column("tenant")), List.of(column("TENANT"))));
int first = ddl.indexOf("AS \"tenant\"");
assertTrue(first >= 0, ddl);
assertEquals(-1, ddl.indexOf("AS \"tenant\"", first + 1), "projected twice: " + ddl);
}
/** A table that declares no columns leaves inference in charge, as before. */
@Test
void testTableWithoutDeclaredColumnsStillUsesStar() {
String ddl = ddlFor(tableWithColumns("t", "s3://b/", null, null));
assertTrue(ddl.contains("SELECT * FROM"), ddl);
}
/** Column identifiers go through the same quoting as table and schema names. */
@Test
void testColumnIdentifierQuotingEscapesDoubleQuotes() {
String ddl = ddlFor(tableWithColumns("t", "s3://b/", List.of(column("we\"ird")), null));
assertTrue(ddl.contains("\"we\"\"ird\" AS \"we\"\"ird\""), ddl);
}
@Test
void testTwoDatabasesQualifiedViewsAndSchemaPerDb() {
Database shopDb = createDatabase("shop");