mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 17:58:05 -05:00
fix(athena): read headerless delimited text by its Glue columns and keep one bad view from failing every query (#8427)
A LazySimpleSerDe table over headerless TSV came back as column00, column01 and the view's projection failed to bind: Athena's views were built with readExpression, which ignored the table's CSV options (field.delim, skip.header.line.count) that only Redshift Spectrum's read plan applied. Views now read such a table with read_csv and those options, naming the file columns after the declared data columns, and give each data column its declared type with TRY_CAST, NULL for a value the type cannot hold, as LazySimpleSerDe does. Every query also built a view for every Glue table, and DuckDB binds each view before running the query, so one table whose view could not be built failed all queries. Views are now built only for the tables the query names, or for every table when it reads information_schema or is a SHOW statement.
This commit is contained in:
1 parent
53ebf2a18c
commit
31c86c05e9
7 files changed
+531
-22
No files matched your search
+184
@@ -0,0 +1,184 @@
|
||||
package io.floci.aws.services.athena;
|
||||
|
||||
import io.floci.aws.services.floci.duck.FlociDuckClient;
|
||||
import io.floci.aws.services.glue.GlueService;
|
||||
import io.floci.aws.services.glue.model.Column;
|
||||
import io.floci.aws.services.glue.model.Database;
|
||||
import io.floci.aws.services.glue.model.StorageDescriptor;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.testing.SidecarContainersProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Headerless delimited text, the layout Spark writes with {@code partitionBy} and a ZSTD codec, read
|
||||
* through the real floci-duck sidecar with the views Athena builds: rows come back under the Glue
|
||||
* column names and types, and a table whose view cannot be built leaves queries on other tables
|
||||
* alone.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(SidecarContainersProfile.class)
|
||||
class AthenaDelimitedTextSidecarIntegrationTest {
|
||||
|
||||
/** {@code 3<TAB>carol<LF>}, ZSTD-compressed. */
|
||||
private static final byte[] ZSTD_ROW = Base64.getDecoder().decode("KLUv/QRoQQAAMwljYXJvbAo4rOon");
|
||||
|
||||
@Inject
|
||||
FlociDuckClient duck;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
private String bucket;
|
||||
private String schema;
|
||||
private final List<String> keys = new ArrayList<>();
|
||||
private final List<Table> tables = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void requireDocker() {
|
||||
SidecarContainersProfile.requireDocker();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createBucket() {
|
||||
bucket = "athena-tsv-" + System.nanoTime();
|
||||
schema = "tsv_" + System.nanoTime();
|
||||
s3Service.createBucket(bucket, "us-east-1");
|
||||
put("events/dt=2026-11-14/part-00000.tsv", "1\talice\n2\tbob\n".getBytes(StandardCharsets.UTF_8));
|
||||
put("events/dt=2026-11-15/part-00001.tsv.zst", ZSTD_ROW);
|
||||
tables.add(tsvTable("events", "events"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteBucket() {
|
||||
for (String key : keys) {
|
||||
s3Service.deleteObject(bucket, key);
|
||||
}
|
||||
s3Service.deleteBucket(bucket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerlessTsvReturnsRowsUnderTheGlueColumnNamesAndTypes() {
|
||||
List<Map<String, String>> rows = query("SELECT id, name, dt, typeof(id) AS id_type FROM " + view("events")
|
||||
+ " ORDER BY id");
|
||||
|
||||
assertEquals(List.of(
|
||||
Map.of("id", "1", "name", "alice", "dt", "2026-11-14", "id_type", "BIGINT"),
|
||||
Map.of("id", "2", "name", "bob", "dt", "2026-11-14", "id_type", "BIGINT"),
|
||||
Map.of("id", "3", "name", "carol", "dt", "2026-11-15", "id_type", "BIGINT")), rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* A table declared as Parquet over the same text files cannot have its view built, since DuckDB
|
||||
* reads a Parquet footer when it binds the view; a query on another table must still run.
|
||||
*/
|
||||
@Test
|
||||
void aTableWhoseViewCannotBeBuiltFailsOnlyQueriesThatReadIt() {
|
||||
Table broken = tsvTable("not_parquet", "events");
|
||||
broken.getStorageDescriptor().setInputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat");
|
||||
broken.getStorageDescriptor().getSerdeInfo()
|
||||
.setSerializationLibrary("org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe");
|
||||
tables.add(broken);
|
||||
String sql = "SELECT count(*) AS n FROM " + view("events");
|
||||
|
||||
assertThrows(RuntimeException.class, () -> duck.query(sql, builder().build(null)),
|
||||
"with every table's view in the batch, the broken one fails the query");
|
||||
assertEquals(List.of(Map.of("n", "3")), query(sql));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows read as Hive's LazySimpleSerDe reads them: a row written before a column was added gets NULL for it, a
|
||||
* field past the declared columns is dropped, {@code \\N} is NULL and an empty field is an empty string.
|
||||
*/
|
||||
@Test
|
||||
void delimitedTextRowsReadAsLazySimpleSerDeReadsThem() {
|
||||
put("evolved/dt=2026-11-16/part-00000.tsv",
|
||||
"1\talice\n2\t\\N\tx\n3\t\tx\n4\tdave\tn\textra\n".getBytes(StandardCharsets.UTF_8));
|
||||
Table evolved = tsvTable("evolved", "evolved");
|
||||
evolved.getStorageDescriptor().setColumns(List.of(column("id", "bigint"), column("name", "string"),
|
||||
column("note", "string")));
|
||||
tables.add(evolved);
|
||||
|
||||
List<Map<String, String>> rows = query("SELECT id, name, name IS NULL AS name_null, note FROM "
|
||||
+ view("evolved") + " ORDER BY id");
|
||||
|
||||
assertEquals(List.of(
|
||||
Map.of("id", "1", "name", "alice", "name_null", "false", "note", "null"),
|
||||
Map.of("id", "2", "name", "null", "name_null", "true", "note", "x"),
|
||||
Map.of("id", "3", "name", "", "name_null", "false", "note", "x"),
|
||||
Map.of("id", "4", "name", "dave", "name_null", "false", "note", "n")), rows);
|
||||
}
|
||||
|
||||
// ---- helpers ----
|
||||
|
||||
private void put(String key, byte[] content) {
|
||||
s3Service.putObject(bucket, key, content, "application/octet-stream", null);
|
||||
keys.add(key);
|
||||
}
|
||||
|
||||
private Table tsvTable(String name, String root) {
|
||||
StorageDescriptor descriptor = new StorageDescriptor();
|
||||
descriptor.setLocation("s3://" + bucket + "/" + root + "/");
|
||||
descriptor.setInputFormat("org.apache.hadoop.mapred.TextInputFormat");
|
||||
StorageDescriptor.SerDeInfo serde = new StorageDescriptor.SerDeInfo();
|
||||
serde.setSerializationLibrary("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe");
|
||||
serde.setParameters(Map.of("field.delim", "\t"));
|
||||
descriptor.setSerdeInfo(serde);
|
||||
descriptor.setColumns(List.of(column("id", "bigint"), column("name", "string")));
|
||||
Table table = new Table();
|
||||
table.setName(name);
|
||||
table.setStorageDescriptor(descriptor);
|
||||
table.setPartitionKeys(List.of(column("dt", "string")));
|
||||
return table;
|
||||
}
|
||||
|
||||
private GlueViewDdlBuilder builder() {
|
||||
GlueService glue = Mockito.mock(GlueService.class);
|
||||
Database database = new Database();
|
||||
database.setName(schema);
|
||||
when(glue.getDatabases()).thenReturn(List.of(database));
|
||||
when(glue.getTables(schema)).thenReturn(tables);
|
||||
return new GlueViewDdlBuilder(glue);
|
||||
}
|
||||
|
||||
private List<Map<String, String>> query(String sql) {
|
||||
String views = builder().build(null, sql);
|
||||
|
||||
List<Map<String, String>> rows = new ArrayList<>();
|
||||
for (Map<String, Object> row : duck.query(sql, views)) {
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
row.forEach((column, value) -> values.put(column, String.valueOf(value)));
|
||||
rows.add(values);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private String view(String table) {
|
||||
return "\"" + schema + "\".\"" + table + "\"";
|
||||
}
|
||||
|
||||
private static Column column(String name, String type) {
|
||||
Column column = new Column();
|
||||
column.setName(name);
|
||||
column.setType(type);
|
||||
return column;
|
||||
}
|
||||
}
|
||||
@@ -228,6 +228,118 @@ class GlueViewDdlBuilderTest {
|
||||
+ "FROM read_parquet('s3://b/hive/region=*/dt=*/**', union_by_name = true);\n", ddl);
|
||||
}
|
||||
|
||||
private Table headerlessTsvTable(String name, String location) {
|
||||
Table table = createTable(name, location, "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
|
||||
"org.apache.hadoop.mapred.TextInputFormat");
|
||||
table.getStorageDescriptor().getSerdeInfo().setParameters(Map.of("field.delim", "\t"));
|
||||
table.getStorageDescriptor().setColumns(List.of(column("id", "bigint"), column("name", "string")));
|
||||
table.setPartitionKeys(List.of(column("dt", "string")));
|
||||
return table;
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerlessDelimitedTextIsReadWithItsDelimiterColumnNamesAndTypes() {
|
||||
String ddl = ddlFor(headerlessTsvTable("events", "s3://lake/events/"));
|
||||
|
||||
assertTrue(ddl.contains("read_csv('s3://lake/events/**', header = false, delim = '\t', quote = '', "
|
||||
+ "nullstr = '\\N', auto_detect = false, null_padding = true, strict_mode = false, "
|
||||
+ "columns = {'id': 'VARCHAR', 'name': 'VARCHAR'}, hive_types_autocast = false)"), ddl);
|
||||
assertTrue(ddl.contains("SELECT TRY_CAST(\"id\" AS BIGINT) AS \"id\", \"name\" AS \"name\", \"dt\" AS \"dt\" FROM"), ddl);
|
||||
assertFalse(ddl.contains("read_csv_auto"), ddl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void delimitedTextPartitionKeysTakeTheirDeclaredTypesToo() {
|
||||
Table table = headerlessTsvTable("events", "s3://lake/events/");
|
||||
table.setPartitionKeys(List.of(column("year", "int")));
|
||||
|
||||
String ddl = ddlFor(table);
|
||||
|
||||
assertTrue(ddl.contains("TRY_CAST(\"year\" AS INTEGER) AS \"year\""), ddl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void delimitedTextWithALocationTemplateAlsoReadsItsObjectPaths() {
|
||||
Table table = headerlessTsvTable("events", "s3://lake/events/");
|
||||
table.setParameters(Map.of("projection.enabled", "true", "projection.dt.type", "injected",
|
||||
"storage.location.template", "s3://lake/events/${dt}/"));
|
||||
|
||||
String ddl = ddlFor(table);
|
||||
|
||||
assertTrue(ddl.contains("columns = {'id': 'VARCHAR', 'name': 'VARCHAR'}, hive_partitioning = false, filename = '"),
|
||||
ddl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyTheTablesAQueryNamesGetViews() {
|
||||
Database db = createDatabase("lake");
|
||||
when(glueService.getDatabases()).thenReturn(List.of(db));
|
||||
when(glueService.getTables("lake")).thenReturn(List.of(
|
||||
headerlessTsvTable("events", "s3://lake/events/"),
|
||||
createTable("broken", "s3://lake/broken/", "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe",
|
||||
null),
|
||||
createTable("events_daily", "s3://lake/daily/", "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe",
|
||||
null)));
|
||||
|
||||
String ddl = builder.build("lake", "SELECT count(*) FROM \"lake\".\"EVENTS\" WHERE name = 'broken_x'");
|
||||
|
||||
assertTrue(ddl.contains("VIEW \"lake\".\"events\" AS"), ddl);
|
||||
// A qualified reference resolves in its database, not through the context's unqualified names.
|
||||
assertFalse(ddl.contains("VIEW \"events\" AS"), ddl);
|
||||
assertFalse(ddl.contains("\"broken\""), ddl);
|
||||
assertFalse(ddl.contains("events_daily"), ddl);
|
||||
assertTrue(ddl.contains("CREATE SCHEMA IF NOT EXISTS \"lake\""), ddl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queriesThatListTablesStillGetEveryView() {
|
||||
Database db = createDatabase("lake");
|
||||
when(glueService.getDatabases()).thenReturn(List.of(db));
|
||||
when(glueService.getTables("lake")).thenReturn(List.of(headerlessTsvTable("events", "s3://lake/events/")));
|
||||
|
||||
assertTrue(builder.build("lake", "SELECT table_name FROM information_schema.tables").contains("\"events\""));
|
||||
assertTrue(builder.build("lake", " show tables").contains("\"events\""));
|
||||
assertTrue(builder.build("lake", "-- listing\nSHOW TABLES").contains("\"events\""));
|
||||
assertTrue(builder.build("lake", "/* listing */ SHOW COLUMNS IN other").contains("\"events\""));
|
||||
assertTrue(builder.build("lake", null).contains("\"events\""));
|
||||
}
|
||||
|
||||
private static final String PARQUET_SERDE = "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe";
|
||||
|
||||
@Test
|
||||
void aTableIsNamedByItsDatabaseAndNameNotByTextThatOnlyLooksLikeIt() {
|
||||
when(glueService.getDatabases()).thenReturn(List.of(createDatabase("good"), createDatabase("bad")));
|
||||
when(glueService.getTables("good")).thenReturn(List.of(headerlessTsvTable("events", "s3://lake/events/")));
|
||||
when(glueService.getTables("bad")).thenReturn(List.of(
|
||||
createTable("events", "s3://lake/bad/", PARQUET_SERDE, null),
|
||||
createTable("broken", "s3://lake/broken/", PARQUET_SERDE, null)));
|
||||
|
||||
// The namesake in another database, and a name only a literal, a comment or a qualified column holds.
|
||||
String ddl = builder.build("good", "SELECT e.broken, 'bad.broken' FROM good.events e -- bad.events\n"
|
||||
+ "/* broken */ WHERE e.name <> 'x'");
|
||||
|
||||
assertTrue(ddl.contains("VIEW \"good\".\"events\" AS"), ddl);
|
||||
assertFalse(ddl.contains("VIEW \"bad\".\"events\""), ddl);
|
||||
assertFalse(ddl.contains("\"broken\""), ddl);
|
||||
// Unqualified, a table resolves in the context database alone.
|
||||
String unqualified = builder.build("good", "SELECT * FROM events");
|
||||
assertTrue(unqualified.contains("VIEW \"events\" AS"), unqualified);
|
||||
assertFalse(unqualified.contains("VIEW \"bad\".\"events\""), unqualified);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aQuotedReferenceIsDecodedBeforeItNamesATable() {
|
||||
when(glueService.getDatabases()).thenReturn(List.of(createDatabase("lake")));
|
||||
when(glueService.getTables("lake")).thenReturn(List.of(headerlessTsvTable("a\"b", "s3://lake/ab/")));
|
||||
|
||||
String ddl = builder.build("lake", "SELECT * FROM \"lake\" . \"A\"\"B\"");
|
||||
|
||||
assertTrue(ddl.contains("VIEW \"lake\".\"a\"\"b\" AS"), ddl);
|
||||
assertEquals(List.of(List.of("select"), List.of("lake", "a\"b")),
|
||||
GlueViewDdlBuilder.references("SELECT * FROM \"lake\" . \"A\"\"B\"").stream()
|
||||
.filter(name -> !name.getFirst().equals("from")).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTwoDatabasesQualifiedViewsAndSchemaPerDb() {
|
||||
Database shopDb = createDatabase("shop");
|
||||
|
||||
@@ -223,7 +223,8 @@ class GlueTableResolverTest {
|
||||
String from = GlueTableResolver.readPlan(table).fromClause();
|
||||
|
||||
assertThat(from, equalTo("read_csv('s3://bucket/events/**', header = false, delim = '|', quote = '''', "
|
||||
+ "escape = '\\', nullstr = 'NA', columns = {'id': 'VARCHAR', 'name': 'VARCHAR'})"));
|
||||
+ "escape = '\\', nullstr = 'NA', auto_detect = false, null_padding = true, strict_mode = false, "
|
||||
+ "columns = {'id': 'VARCHAR', 'name': 'VARCHAR'})"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -234,7 +235,9 @@ class GlueTableResolverTest {
|
||||
|
||||
String from = GlueTableResolver.readPlan(table).fromClause();
|
||||
|
||||
assertThat(from, equalTo("read_csv('s3://bucket/events/**', header = false, delim = ',', "
|
||||
// A table naming no SerDe is read as LazySimpleSerDe reads it: a quote is text and \\N is NULL.
|
||||
assertThat(from, equalTo("read_csv('s3://bucket/events/**', header = false, delim = ',', quote = '', "
|
||||
+ "nullstr = '\\N', auto_detect = false, null_padding = true, strict_mode = false, "
|
||||
+ "columns = {'id': 'VARCHAR'})"));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user