mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
c48e81d956
commit
ebe9cf2264
2 files changed
+136
-1
No files matched your search
@@ -1,14 +1,18 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
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.Table;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class GlueViewDdlBuilder {
|
||||
@@ -88,7 +92,9 @@ public class GlueViewDdlBuilder {
|
||||
: quote(t.getName());
|
||||
sb.append("CREATE OR REPLACE VIEW ")
|
||||
.append(target)
|
||||
.append(" AS SELECT * FROM ")
|
||||
.append(" AS SELECT ")
|
||||
.append(buildProjection(t))
|
||||
.append(" FROM ")
|
||||
.append(readExpression(readFn, normalizedLocation))
|
||||
.append(";\n");
|
||||
} catch (Exception e) {
|
||||
@@ -97,6 +103,58 @@ public class GlueViewDdlBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects the columns the catalog declares, rather than whatever the underlying files spell.
|
||||
*
|
||||
* <p>Athena reports a table's declared column names: a JSON body written with {@code tenantId}
|
||||
* surfaces as {@code tenantid} when the table declares it that way, because the catalog owns the
|
||||
* schema and the SerDe maps onto it. Inference sees only the data, so without this the view
|
||||
* exposes the file's spelling and a client reading the declared name finds nothing. Identifier
|
||||
* resolution is case-insensitive here, so a declared name still binds to an inferred one that
|
||||
* differs from it only by case.
|
||||
*
|
||||
* <p>Partition keys are part of the table's schema and are what partition predicates filter on,
|
||||
* so they are projected too. A table that declares no columns keeps {@code *}, leaving inference
|
||||
* in charge.
|
||||
*/
|
||||
static String buildProjection(Table table) {
|
||||
List<Column> declared = new ArrayList<>();
|
||||
if (table != null) {
|
||||
if (table.getStorageDescriptor() != null && table.getStorageDescriptor().getColumns() != null) {
|
||||
declared.addAll(table.getStorageDescriptor().getColumns());
|
||||
}
|
||||
if (table.getPartitionKeys() != null) {
|
||||
declared.addAll(table.getPartitionKeys());
|
||||
}
|
||||
}
|
||||
|
||||
List<String> names = new ArrayList<>();
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (Column c : declared) {
|
||||
if (c == null || c.getName() == null || c.getName().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String name = c.getName();
|
||||
// A partition key repeating a data column would bind twice and make the view ambiguous.
|
||||
if (seen.add(name.toLowerCase(Locale.ROOT))) {
|
||||
names.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
if (names.isEmpty()) {
|
||||
return "*";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
if (i > 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(quote(names.get(i))).append(" AS ").append(quote(names.get(i)));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String inferReadFunction(Table table) {
|
||||
if (table == null || table.getStorageDescriptor() == null) {
|
||||
return "read_csv_auto";
|
||||
|
||||
Reference in new issue
Block a user