mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(athena): register every Glue database as a DuckDB schema so <db>.<table> resolves (#2993)
* feat(athena): extract GlueViewDdlBuilder with schema qualification and aliases * feat(athena): wire GlueViewDdlBuilder into AthenaService * docs(athena): document Glue database schema registration and aliases * fix(athena): propagate getDatabases failure and harden DDL table quoting * fix(athena): skip Glue tables with nonexistent S3 bucket and cleanup AthenaTest * fix(athena): avoid redundant table fetch and add qualified query compatibility test * fix(athena): do not skip views for tables with nonexistent S3 bucket
This commit is contained in:
1 parent
2e303e2a0b
commit
0ab9979edf
5 files changed
+545
-101
No files matched your search
@@ -61,6 +61,7 @@ public class AthenaService {
|
||||
private final S3Service s3Service;
|
||||
private final EmulatorConfig config;
|
||||
private final Vertx vertx;
|
||||
private final GlueViewDdlBuilder ddlBuilder;
|
||||
|
||||
@Inject
|
||||
public AthenaService(StorageFactory storageFactory,
|
||||
@@ -68,7 +69,8 @@ public class AthenaService {
|
||||
GlueService glueService,
|
||||
S3Service s3Service,
|
||||
EmulatorConfig config,
|
||||
Vertx vertx) {
|
||||
Vertx vertx,
|
||||
GlueViewDdlBuilder ddlBuilder) {
|
||||
this.queryStore = storageFactory.create("athena", "queries.json",
|
||||
new TypeReference<>() {});
|
||||
this.workGroupStore = storageFactory.create("athena", "workgroups.json",
|
||||
@@ -78,6 +80,7 @@ public class AthenaService {
|
||||
this.s3Service = s3Service;
|
||||
this.config = config;
|
||||
this.vertx = vertx;
|
||||
this.ddlBuilder = ddlBuilder;
|
||||
}
|
||||
|
||||
public String startQueryExecution(String query,
|
||||
@@ -132,7 +135,7 @@ public class AthenaService {
|
||||
|
||||
// Submit async — caller gets the ID immediately while execution runs in background
|
||||
vertx.executeBlocking(() -> {
|
||||
String setupDdl = buildGlueDdl(database);
|
||||
String setupDdl = ddlBuilder.build(database);
|
||||
if (outputLocation != null) {
|
||||
ensureOutputBucket(outputLocation);
|
||||
}
|
||||
@@ -367,62 +370,6 @@ public class AthenaService {
|
||||
String location, Map<String, String> properties) {
|
||||
}
|
||||
|
||||
private String buildGlueDdl(String database) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
List<Table> tables = glueService.getTables(database);
|
||||
for (Table table : tables) {
|
||||
String location = table.getStorageDescriptor() != null
|
||||
? table.getStorageDescriptor().getLocation()
|
||||
: null;
|
||||
if (location == null || location.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String readFn = inferReadFunction(table);
|
||||
String normalizedLocation = location.endsWith("/")
|
||||
? location.substring(0, location.length() - 1) : location;
|
||||
sb.append("CREATE OR REPLACE VIEW \"")
|
||||
.append(table.getName())
|
||||
.append("\" AS SELECT * FROM ")
|
||||
.append(readExpression(readFn, normalizedLocation))
|
||||
.append(";\n");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not inject Glue DDL for database {0}: {1}", database, e.getMessage());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String readExpression(String readFn, String normalizedLocation) {
|
||||
String glob = normalizedLocation + "/**";
|
||||
if ("read_parquet".equals(readFn)) {
|
||||
return "read_parquet('" + glob + "', union_by_name = true)";
|
||||
}
|
||||
return readFn + "('" + glob + "')";
|
||||
}
|
||||
|
||||
private String inferReadFunction(Table table) {
|
||||
if (table.getStorageDescriptor() == null) {
|
||||
return "read_csv_auto";
|
||||
}
|
||||
String format = table.getStorageDescriptor().getInputFormat();
|
||||
String serde = table.getStorageDescriptor().getSerdeInfo() != null
|
||||
? table.getStorageDescriptor().getSerdeInfo().getSerializationLibrary()
|
||||
: null;
|
||||
if (containsIgnoreCase(format, "parquet") || containsIgnoreCase(serde, "parquet")) {
|
||||
return "read_parquet";
|
||||
}
|
||||
if (containsIgnoreCase(format, "json") || containsIgnoreCase(serde, "json")
|
||||
|| containsIgnoreCase(format, "hive")) {
|
||||
return "read_json_auto";
|
||||
}
|
||||
return "read_csv_auto";
|
||||
}
|
||||
|
||||
private static boolean containsIgnoreCase(String str, String sub) {
|
||||
return str != null && str.toLowerCase().contains(sub);
|
||||
}
|
||||
|
||||
private String resolveOutputLocation(ResultConfiguration rc, String queryId) {
|
||||
String base = (rc != null && rc.getOutputLocation() != null && !rc.getOutputLocation().isBlank())
|
||||
? rc.getOutputLocation()
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
import io.github.hectorvent.floci.services.glue.GlueService;
|
||||
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.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@ApplicationScoped
|
||||
public class GlueViewDdlBuilder {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(GlueViewDdlBuilder.class);
|
||||
|
||||
private final GlueService glueService;
|
||||
|
||||
@Inject
|
||||
public GlueViewDdlBuilder(GlueService glueService) {
|
||||
this.glueService = glueService;
|
||||
}
|
||||
|
||||
public String build(String contextDatabase) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean contextDbHandled = false;
|
||||
List<Database> databases = glueService.getDatabases();
|
||||
if (databases != null) {
|
||||
for (Database db : databases) {
|
||||
if (db == null) {
|
||||
continue;
|
||||
}
|
||||
String schema = db.getName();
|
||||
if (schema == null || schema.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
sb.append("CREATE SCHEMA IF NOT EXISTS ").append(quote(schema)).append(";\n");
|
||||
try {
|
||||
List<Table> tables = glueService.getTables(schema);
|
||||
appendViews(sb, schema, tables, true);
|
||||
if (schema.equals(contextDatabase)) {
|
||||
appendViews(sb, schema, tables, false);
|
||||
contextDbHandled = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not fetch tables for Glue database {0}: {1}", schema, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!contextDbHandled && contextDatabase != null && !contextDatabase.isBlank()) {
|
||||
try {
|
||||
List<Table> tables = glueService.getTables(contextDatabase);
|
||||
appendViews(sb, contextDatabase, tables, false);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Could not fetch tables for context database {0}: {1}", contextDatabase, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void appendViews(StringBuilder sb, String schemaOrNull, List<Table> tables, boolean qualified) {
|
||||
if (tables == null) {
|
||||
return;
|
||||
}
|
||||
for (Table t : tables) {
|
||||
try {
|
||||
if (t == null) {
|
||||
continue;
|
||||
}
|
||||
if (t.getName() == null || t.getName().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (t.getStorageDescriptor() == null
|
||||
|| t.getStorageDescriptor().getLocation() == null
|
||||
|| t.getStorageDescriptor().getLocation().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String location = t.getStorageDescriptor().getLocation();
|
||||
String normalizedLocation = location.endsWith("/")
|
||||
? location.substring(0, location.length() - 1)
|
||||
: location;
|
||||
String readFn = inferReadFunction(t);
|
||||
String target = qualified
|
||||
? quote(schemaOrNull) + "." + quote(t.getName())
|
||||
: quote(t.getName());
|
||||
sb.append("CREATE OR REPLACE VIEW ")
|
||||
.append(target)
|
||||
.append(" AS SELECT * FROM ")
|
||||
.append(readExpression(readFn, normalizedLocation))
|
||||
.append(";\n");
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("skip Glue table {0}.{1}: {2}", schemaOrNull, t != null ? t.getName() : "unknown", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static String inferReadFunction(Table table) {
|
||||
if (table == null || table.getStorageDescriptor() == null) {
|
||||
return "read_csv_auto";
|
||||
}
|
||||
String format = table.getStorageDescriptor().getInputFormat();
|
||||
String serde = table.getStorageDescriptor().getSerdeInfo() != null
|
||||
? table.getStorageDescriptor().getSerdeInfo().getSerializationLibrary()
|
||||
: null;
|
||||
if (containsIgnoreCase(format, "parquet") || containsIgnoreCase(serde, "parquet")) {
|
||||
return "read_parquet";
|
||||
}
|
||||
if (containsIgnoreCase(format, "json") || containsIgnoreCase(serde, "json")
|
||||
|| containsIgnoreCase(format, "hive")) {
|
||||
return "read_json_auto";
|
||||
}
|
||||
return "read_csv_auto";
|
||||
}
|
||||
|
||||
static String readExpression(String readFn, String normalizedLocation) {
|
||||
String escapedLocation = normalizedLocation.replace("'", "''");
|
||||
String glob = escapedLocation + "/**";
|
||||
if ("read_parquet".equals(readFn)) {
|
||||
return "read_parquet('" + glob + "', union_by_name = true)";
|
||||
}
|
||||
return readFn + "('" + glob + "')";
|
||||
}
|
||||
|
||||
static boolean containsIgnoreCase(String str, String sub) {
|
||||
return str != null && str.toLowerCase(Locale.ROOT).contains(sub);
|
||||
}
|
||||
|
||||
static String quote(String id) {
|
||||
return "\"" + id.replace("\"", "\"\"") + "\"";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user