mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(athena): read a projecting table's partition paths, not its whole location (#3415)
* fix(athena): read a projecting table's partition paths, not its whole location A table that projects its partitions was read by globbing its location, so anything else stored beside the data was read as though it were table data. Athena does not do this: it builds the partition paths from the projection configuration and reads only those, which is the point of projection. The common way this bites is a workgroup whose results live under the table's own root. The first query succeeds, its output CSV lands inside the read path, and every query after it fails parsing that CSV as table data - a data source that works once and then breaks, on a file the engine wrote itself. Derive the read path from storage.location.template when it is set, and otherwise from the default column=value layout over the partition keys. Values stay wildcards: narrowing them to the partitions a query actually needs requires that query's predicates, which are not available where the view is built, so the read stays wide and the query does the filtering. A table that does not project its partitions is read exactly as before. Also reject a query that names a projecting table but leaves an injected partition column unmentioned. An injected column has no generatable range - its values come from the query - so Athena requires an equality condition on it. Deciding whether a condition is a static equality one needs the query's predicate tree, so this checks only whether the column is mentioned at all: strictly narrower than the real rule, never rejecting a query Athena would accept, but catching the case that silently reads every partition. * fix(athena): scope the injected-column check to WHERE clauses The check matched the column name anywhere in the query text, so a query that names the column without filtering on it passed. `SELECT tenant, count(*) FROM audit_events GROUP BY tenant` has no WHERE clause at all and still satisfied it, on the strength of the SELECT list and the GROUP BY - which is the case the check exists to catch. Look only inside WHERE clauses, a subquery's included. A clause runs from the keyword to whatever ends it: the next clause keyword, the paren closing the subquery that holds it, a statement terminator, or the end of the query. Parentheses opened inside the clause are part of it, so a predicate carrying its own subquery is not cut short. Single-quoted literals and comments are blanked first. A column name spelled inside either constrains nothing, and reading them as code made the same false pass by another route. Double-quoted and backquoted identifiers stay: those are references to the column. Still narrower than Athena's rule, which wants a static equality condition rather than a mention, so it continues to reject nothing Athena accepts.
This commit is contained in:
1 parent
c12779416d
commit
6d6940bb8f
4 files changed
+487
-1
No files matched your search
+210
@@ -0,0 +1,210 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.glue.model.Column;
|
||||
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
|
||||
import io.github.hectorvent.floci.services.glue.model.Table;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PartitionProjectionTest {
|
||||
|
||||
private static final String BUCKET = "s3://audit-logs";
|
||||
|
||||
private static Column column(String name) {
|
||||
Column c = new Column();
|
||||
c.setName(name);
|
||||
c.setType("string");
|
||||
return c;
|
||||
}
|
||||
|
||||
private static Table table(String name, Map<String, String> params, List<Column> partitionKeys) {
|
||||
Table t = new Table();
|
||||
t.setName(name);
|
||||
StorageDescriptor sd = new StorageDescriptor();
|
||||
sd.setLocation(BUCKET + "/");
|
||||
t.setStorageDescriptor(sd);
|
||||
t.setParameters(params);
|
||||
t.setPartitionKeys(partitionKeys);
|
||||
return t;
|
||||
}
|
||||
|
||||
private static Map<String, String> projecting(String... extra) {
|
||||
Map<String, String> m = new LinkedHashMap<>();
|
||||
m.put("projection.enabled", "true");
|
||||
for (int i = 0; i + 1 < extra.length; i += 2) {
|
||||
m.put(extra[i], extra[i + 1]);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// ---- read path ----
|
||||
|
||||
/**
|
||||
* A workgroup writing results under the table's root is the case that breaks: reading the root
|
||||
* wholesale picks the results back up as though they were table data.
|
||||
*/
|
||||
@Test
|
||||
void templateGivesAPartitionPathThatExcludesSiblingPrefixes() {
|
||||
Table t = table("audit_events", projecting(
|
||||
"storage.location.template", BUCKET + "/tenant=${tenant}/year=${year}/month=${month}/day=${day}"),
|
||||
List.of(column("tenant"), column("year"), column("month"), column("day")));
|
||||
|
||||
String path = PartitionProjection.readPath(t, BUCKET);
|
||||
|
||||
assertEquals(BUCKET + "/tenant=*/year=*/month=*/day=*", path);
|
||||
assertTrue(path.startsWith(BUCKET + "/tenant="), path);
|
||||
}
|
||||
|
||||
/** Without a template the default column=value layout is built from the partition keys. */
|
||||
@Test
|
||||
void partitionKeysGiveTheDefaultLayoutWhenNoTemplateIsSet() {
|
||||
Table t = table("t", projecting(), List.of(column("tenant"), column("year")));
|
||||
|
||||
assertEquals(BUCKET + "/tenant=*/year=*", PartitionProjection.readPath(t, BUCKET));
|
||||
}
|
||||
|
||||
/** Projection off means the location is read as before. */
|
||||
@Test
|
||||
void withoutProjectionTheLocationIsUnchanged() {
|
||||
Table t = table("t", Map.of(), List.of(column("tenant")));
|
||||
|
||||
assertEquals(BUCKET, PartitionProjection.readPath(t, BUCKET));
|
||||
}
|
||||
|
||||
/** A projecting table declaring no partitions has no layout to build. */
|
||||
@Test
|
||||
void projectionWithoutPartitionKeysLeavesTheLocationUnchanged() {
|
||||
assertEquals(BUCKET, PartitionProjection.readPath(table("t", projecting(), null), BUCKET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void trailingSlashOnTheTemplateIsDropped() {
|
||||
Table t = table("t", projecting("storage.location.template", BUCKET + "/tenant=${tenant}/"),
|
||||
List.of(column("tenant")));
|
||||
|
||||
assertEquals(BUCKET + "/tenant=*", PartitionProjection.readPath(t, BUCKET));
|
||||
}
|
||||
|
||||
// ---- injected columns ----
|
||||
|
||||
private Table injectedTenantTable() {
|
||||
return table("audit_events", projecting("projection.tenant.type", "injected"),
|
||||
List.of(column("tenant")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryWithoutAnyMentionOfAnInjectedColumnIsRejected() {
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events", List.of(injectedTenantTable())));
|
||||
|
||||
assertTrue(e.getMessage().startsWith("CONSTRAINT_VIOLATION:"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("tenant"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryConstrainingTheInjectedColumnIsAccepted() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE tenant = 'abc'", List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A table the query never names must not fail it. */
|
||||
@Test
|
||||
void unrelatedQueryAgainstAnotherTableIsUnaffected() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM other_table", List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A non-injected projected column generates its own values and needs no filter. */
|
||||
@Test
|
||||
void nonInjectedPartitionColumnsNeedNoFilter() {
|
||||
Table t = table("t", projecting("projection.year.type", "integer"), List.of(column("year")));
|
||||
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered("SELECT * FROM t", List.of(t)));
|
||||
}
|
||||
|
||||
/** Identifiers match on word boundaries, so a longer name that contains one does not count. */
|
||||
@Test
|
||||
void aLongerIdentifierContainingTheColumnNameDoesNotCountAsAMention() {
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE tenant_id = 'abc'", List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/**
|
||||
* The column names itself in the SELECT list and the GROUP BY, and filters nothing - the case
|
||||
* the check exists to catch, and the one a query-wide text match lets through.
|
||||
*/
|
||||
@Test
|
||||
void mentionOutsideAWhereClauseDoesNotCount() {
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT tenant, count(*) FROM audit_events GROUP BY tenant",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A WHERE clause ends where the next one starts, so an ORDER BY mention is outside it. */
|
||||
@Test
|
||||
void mentionAfterTheWhereClauseEndsDoesNotCount() {
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE year = 2026 ORDER BY tenant",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A string that happens to spell the column name constrains nothing. */
|
||||
@Test
|
||||
void mentionInsideAStringLiteralDoesNotCount() {
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE note = 'tenant'", List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** Nor does one in a comment. */
|
||||
@Test
|
||||
void mentionInsideACommentDoesNotCount() {
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events -- filter by tenant one day\n WHERE year = 2026",
|
||||
List.of(injectedTenantTable())));
|
||||
assertThrows(AwsException.class, () -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events /* tenant */ WHERE year = 2026",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A subquery's WHERE is still a WHERE, and still constrains the read. */
|
||||
@Test
|
||||
void filterInASubqueryCounts() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT tenant, count(*) FROM (SELECT * FROM audit_events WHERE tenant = 'abc') "
|
||||
+ "GROUP BY tenant",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A predicate carrying its own subquery is not cut short at the inner parentheses. */
|
||||
@Test
|
||||
void aPredicateWithItsOwnSubqueryIsReadWhole() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE year IN (SELECT y FROM years) AND tenant = 'abc'",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** Double quotes mark an identifier in Athena, so a quoted column reference counts. */
|
||||
@Test
|
||||
void aQuotedColumnReferenceCounts() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE \"tenant\" = 'abc'", List.of(injectedTenantTable())));
|
||||
}
|
||||
|
||||
/** A column named after a clause keyword is quoted, and must not end the clause it sits in. */
|
||||
@Test
|
||||
void aQuotedIdentifierSpellingAClauseKeywordDoesNotEndTheClause() {
|
||||
assertDoesNotThrow(() -> PartitionProjection.assertInjectedColumnsFiltered(
|
||||
"SELECT * FROM audit_events WHERE \"limit\" = 10 AND tenant = 'abc'",
|
||||
List.of(injectedTenantTable())));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user