mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(athena): answer no rows for a table whose location holds no data (#8575)
Athena answers a table whose S3 location holds no data with no rows. Floci's view read the location with DuckDB's read_* glob, which refuses to bind when nothing matches, so every query on such a table failed. - GlueViewDdlBuilder checks the location a table is read from (its read path, so a projected table's partition folders) before building its view. One that holds no data gets a view of no rows with the table's declared columns as their declared types (GlueTableResolver .emptyProjection), nested and binary columns as the engine reads them (columnType: array<T> as T[], map, struct, binary as BLOB). - TableLocation.holdsNoData lists the location in one scan: folder markers (keys ending in a slash) hold no data, and a read path with `*` segments is listed from its literal prefix, an object counting when its key matches the glob. A location holding another glob character, or whose bucket does not exist, is read as before. GlueViewDdlBuilderTest covers an empty location, one holding only folder markers, a projected location holding only objects its glob does not reach, nested and binary column types, an unmodeled glob and a missing bucket. AthenaDelimitedTextSidecarIntegrationTest queries an empty location and a folder marker against the real engine. athena.md says so.
This commit is contained in:
1 parent
f94d5e9e58
commit
f0ee5d44a0
7 files changed
+223
-4
No files matched your search
@@ -5,6 +5,7 @@ import io.floci.aws.services.glue.GlueTableResolver;
|
||||
import io.floci.aws.services.glue.PartitionProjection;
|
||||
import io.floci.aws.services.glue.model.Database;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -26,10 +27,12 @@ public class GlueViewDdlBuilder {
|
||||
private static final String ICEBERG_EXTENSION_SETUP = "INSTALL iceberg; LOAD iceberg;\n";
|
||||
|
||||
private final GlueService glueService;
|
||||
private final S3Service s3Service;
|
||||
|
||||
@Inject
|
||||
public GlueViewDdlBuilder(GlueService glueService) {
|
||||
public GlueViewDdlBuilder(GlueService glueService, S3Service s3Service) {
|
||||
this.glueService = glueService;
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
public String build(String contextDatabase) {
|
||||
@@ -132,6 +135,13 @@ public class GlueViewDdlBuilder {
|
||||
} else {
|
||||
String readFn = GlueTableResolver.inferReadFunction(t);
|
||||
String readPath = GlueTableResolver.readPath(t, normalizedLocation);
|
||||
// Reading a location that holds no data fails to bind, where Athena answers no rows.
|
||||
String empty = holdsNoData(readPath) ? GlueTableResolver.emptyProjection(t) : null;
|
||||
if (empty != null) {
|
||||
sb.append("CREATE OR REPLACE VIEW ").append(target).append(" AS SELECT ").append(empty)
|
||||
.append(";\n");
|
||||
continue;
|
||||
}
|
||||
fromClause = GlueTableResolver.readExpression(t, readFn, readPath);
|
||||
}
|
||||
sb.append("CREATE OR REPLACE VIEW ")
|
||||
@@ -152,6 +162,12 @@ public class GlueViewDdlBuilder {
|
||||
return usesIceberg;
|
||||
}
|
||||
|
||||
/** Whether the S3 location a table is read from holds no data; false for one that is not in S3. */
|
||||
private boolean holdsNoData(String readPath) {
|
||||
TableLocation location = TableLocation.of(readPath);
|
||||
return location != null && location.holdsNoData(s3Service);
|
||||
}
|
||||
|
||||
/** Which views a query needs: a qualified view of a database's table, or an unqualified one of the context's. */
|
||||
@FunctionalInterface
|
||||
interface ViewFilter {
|
||||
|
||||
@@ -4,12 +4,17 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3.model.S3Object;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** A table's {@code s3://bucket/prefix/} location split into its bucket and its key prefix, ending in a slash. */
|
||||
record TableLocation(String bucket, String prefix) {
|
||||
|
||||
private static final int LIST_PAGE_SIZE = 1000;
|
||||
/** The glob characters DuckDB reads in a path besides {@code *}. */
|
||||
private static final Pattern UNMODELED_GLOB = Pattern.compile("[?\\[\\]{}\\\\]");
|
||||
|
||||
/** The location {@code location} names, or null when it is not an {@code s3://} location. */
|
||||
static TableLocation of(String location) {
|
||||
@@ -39,6 +44,34 @@ record TableLocation(String bucket, String prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether no object the read of {@code <location>/**} reaches holds data: there is none, or only the zero-byte
|
||||
* folder markers (keys ending in a slash) the S3 console creates. A location with {@code *} in it, a projected
|
||||
* table's read path, is listed from the literal prefix before its first {@code *}, an object counting when its
|
||||
* key matches, each {@code *} taking part or all of one folder name. False when the location holds a glob
|
||||
* character this does not model ({@code ?}, a bracket or brace expression, an escape), which the read keeps, and
|
||||
* when the bucket does not exist, which reading the location reports as it did. The location is listed in one
|
||||
* scan, as every page of a listing scans the whole prefix.
|
||||
*/
|
||||
boolean holdsNoData(S3Service s3) {
|
||||
if (UNMODELED_GLOB.matcher(prefix).find()) {
|
||||
return false;
|
||||
}
|
||||
int wildcard = prefix.indexOf('*');
|
||||
String listed = wildcard < 0 ? prefix : prefix.substring(0, prefix.lastIndexOf('/', wildcard) + 1);
|
||||
Pattern reached = wildcard < 0 ? null : Pattern.compile(Arrays.stream(prefix.split("\\*", -1))
|
||||
.map(Pattern::quote).collect(Collectors.joining("[^/]*")) + ".+");
|
||||
try {
|
||||
return s3.listObjects(bucket, listed, null, Integer.MAX_VALUE).stream().map(S3Object::getKey)
|
||||
.noneMatch(key -> !key.endsWith("/") && (reached == null || reached.matcher(key).matches()));
|
||||
} catch (AwsException e) {
|
||||
if ("NoSuchBucket".equals(e.getErrorCode())) {
|
||||
return false;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/** Hands each object under the location to {@code action}, a page at a time; the action may delete it. */
|
||||
void forEachObject(S3Service s3, Consumer<S3Object> action) {
|
||||
String continuation = null;
|
||||
|
||||
@@ -289,6 +289,76 @@ public final class GlueTableResolver {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A projection of no rows with the table's declared columns, each as its declared type, for a table whose
|
||||
* location holds no data: Athena answers it with no rows, where a read of the empty location fails. Null for a
|
||||
* table that declares no columns.
|
||||
*/
|
||||
public static String emptyProjection(Table table) {
|
||||
List<Column> declared = declaredColumns(table);
|
||||
if (declared.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return declared.stream()
|
||||
.map(column -> "CAST(NULL AS " + columnType(column.getType()) + ") AS " + quote(column.getName()))
|
||||
.collect(Collectors.joining(", ")) + " LIMIT 0";
|
||||
}
|
||||
|
||||
/**
|
||||
* The DuckDB type a Glue (Hive) column type reads as, {@code array<T>}, {@code map<K,V>} and
|
||||
* {@code struct<name:T,...>} nested as far as they go, {@code binary} as a BLOB; a scalar type as
|
||||
* {@link #scalarType} gives it, and VARCHAR for one this does not know.
|
||||
*/
|
||||
static String columnType(String glueType) {
|
||||
String type = glueType == null ? "" : glueType.trim();
|
||||
String lower = type.toLowerCase(Locale.ROOT);
|
||||
if (lower.endsWith(">")) {
|
||||
if (lower.startsWith("array<")) {
|
||||
return columnType(type.substring("array<".length(), type.length() - 1)) + "[]";
|
||||
}
|
||||
if (lower.startsWith("map<")) {
|
||||
List<String> keyAndValue = topLevelParts(type.substring("map<".length(), type.length() - 1));
|
||||
if (keyAndValue.size() == 2) {
|
||||
return "MAP(" + columnType(keyAndValue.get(0)) + ", " + columnType(keyAndValue.get(1)) + ")";
|
||||
}
|
||||
}
|
||||
if (lower.startsWith("struct<")) {
|
||||
List<String> fields = new ArrayList<>();
|
||||
for (String field : topLevelParts(type.substring("struct<".length(), type.length() - 1))) {
|
||||
int colon = field.indexOf(':');
|
||||
if (colon <= 0) {
|
||||
return "VARCHAR";
|
||||
}
|
||||
fields.add(quote(field.substring(0, colon).trim().replace("`", "")) + " "
|
||||
+ columnType(field.substring(colon + 1)));
|
||||
}
|
||||
return fields.isEmpty() ? "VARCHAR" : "STRUCT(" + String.join(", ", fields) + ")";
|
||||
}
|
||||
return "VARCHAR";
|
||||
}
|
||||
return "binary".equals(lower) ? "BLOB" : scalarType(type);
|
||||
}
|
||||
|
||||
/** The comma-separated parts of a type list, a comma inside {@code <>} or {@code ()} not separating two. */
|
||||
private static List<String> topLevelParts(String list) {
|
||||
List<String> parts = new ArrayList<>();
|
||||
int depth = 0;
|
||||
int start = 0;
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
char c = list.charAt(i);
|
||||
if (c == '<' || c == '(') {
|
||||
depth++;
|
||||
} else if (c == '>' || c == ')') {
|
||||
depth--;
|
||||
} else if (c == ',' && depth == 0) {
|
||||
parts.add(list.substring(start, i));
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
parts.add(list.substring(start));
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the table is read as text with its own CSV options, which leaves every data column and
|
||||
* Hive partition value a VARCHAR for {@link #buildProjection} to give its declared type.
|
||||
|
||||
Reference in new issue
Block a user