mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat: support Glue user-defined functions (#1187)
This commit is contained in:
1 parent
86be48ece1
commit
6089d8ddc7
9 files changed
+554
-4
No files matched your search
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.glue.model.Database;
|
||||
import io.github.hectorvent.floci.services.glue.model.Partition;
|
||||
import io.github.hectorvent.floci.services.glue.model.Table;
|
||||
import io.github.hectorvent.floci.services.glue.model.UserDefinedFunction;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.model.Registry;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.model.RegistryId;
|
||||
@@ -97,6 +98,11 @@ public class GlueJsonHandler {
|
||||
String tableName = request.get("TableName").asText();
|
||||
yield Response.ok(Map.of("Partitions", glueService.getPartitions(dbName, tableName))).build();
|
||||
}
|
||||
case "CreateUserDefinedFunction" -> handleCreateUserDefinedFunction(request);
|
||||
case "GetUserDefinedFunction" -> handleGetUserDefinedFunction(request);
|
||||
case "GetUserDefinedFunctions" -> handleGetUserDefinedFunctions(request);
|
||||
case "UpdateUserDefinedFunction" -> handleUpdateUserDefinedFunction(request);
|
||||
case "DeleteUserDefinedFunction" -> handleDeleteUserDefinedFunction(request);
|
||||
case "CreateRegistry" -> handleCreateRegistry(request, region);
|
||||
case "GetRegistry" -> handleGetRegistry(request, region);
|
||||
case "ListRegistries" -> handleListRegistries(request);
|
||||
@@ -124,6 +130,46 @@ public class GlueJsonHandler {
|
||||
};
|
||||
}
|
||||
|
||||
private Response handleCreateUserDefinedFunction(JsonNode request) throws Exception {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
UserDefinedFunction function = mapper.treeToValue(request.get("FunctionInput"), UserDefinedFunction.class);
|
||||
glueService.createUserDefinedFunction(dbName, function);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
private Response handleGetUserDefinedFunction(JsonNode request) {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
String functionName = request.get("FunctionName").asText();
|
||||
return Response.ok(Map.of(
|
||||
"UserDefinedFunction",
|
||||
glueService.getUserDefinedFunction(dbName, functionName)))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response handleGetUserDefinedFunctions(JsonNode request) {
|
||||
String dbName = request.path("DatabaseName").asText(null);
|
||||
String pattern = request.path("Pattern").asText(null);
|
||||
String functionType = request.path("FunctionType").asText(null);
|
||||
var page = glueService.getUserDefinedFunctions(
|
||||
dbName, pattern, functionType, readMaxResults(request), readNextToken(request));
|
||||
return Response.ok(pageResponse("UserDefinedFunctions", page.functions(), page.nextToken())).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateUserDefinedFunction(JsonNode request) throws Exception {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
String functionName = request.get("FunctionName").asText();
|
||||
UserDefinedFunction function = mapper.treeToValue(request.get("FunctionInput"), UserDefinedFunction.class);
|
||||
glueService.updateUserDefinedFunction(dbName, functionName, function);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
private Response handleDeleteUserDefinedFunction(JsonNode request) {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
String functionName = request.get("FunctionName").asText();
|
||||
glueService.deleteUserDefinedFunction(dbName, functionName);
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
private Response handleCreateRegistry(JsonNode request, String region) {
|
||||
String name = request.path("RegistryName").asText(null);
|
||||
String description = request.path("Description").asText(null);
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.glue.model.Partition;
|
||||
import io.github.hectorvent.floci.services.glue.model.SchemaReference;
|
||||
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
|
||||
import io.github.hectorvent.floci.services.glue.model.Table;
|
||||
import io.github.hectorvent.floci.services.glue.model.UserDefinedFunction;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.SchemaToColumnsConverter;
|
||||
import io.github.hectorvent.floci.services.glue.schemaregistry.model.SchemaId;
|
||||
@@ -21,18 +22,24 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
@ApplicationScoped
|
||||
public class GlueService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(GlueService.class);
|
||||
private static final int MAX_FUNCTION_PATTERN_LENGTH = 255;
|
||||
private static final int MAX_FUNCTION_RESULTS = 100;
|
||||
|
||||
private final StorageBackend<String, Database> databaseStore;
|
||||
private final StorageBackend<String, Table> tableStore;
|
||||
private final StorageBackend<String, Partition> partitionStore;
|
||||
private final StorageBackend<String, UserDefinedFunction> functionStore;
|
||||
private final GlueSchemaRegistryService schemaRegistryService;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@@ -40,9 +47,10 @@ public class GlueService {
|
||||
public GlueService(StorageFactory storageFactory,
|
||||
GlueSchemaRegistryService schemaRegistryService,
|
||||
RegionResolver regionResolver) {
|
||||
this.databaseStore = storageFactory.create("glue", "databases.json", new TypeReference<Map<String, Database>>() {});
|
||||
this.tableStore = storageFactory.create("glue", "tables.json", new TypeReference<Map<String, Table>>() {});
|
||||
this.partitionStore = storageFactory.create("glue", "partitions.json", new TypeReference<Map<String, Partition>>() {});
|
||||
this.databaseStore = storageFactory.create("glue", "databases.json", new TypeReference<>() {});
|
||||
this.tableStore = storageFactory.create("glue", "tables.json", new TypeReference<>() {});
|
||||
this.partitionStore = storageFactory.create("glue", "partitions.json", new TypeReference<>() {});
|
||||
this.functionStore = storageFactory.create("glue", "functions.json", new TypeReference<>() {});
|
||||
this.schemaRegistryService = schemaRegistryService;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
@@ -50,11 +58,13 @@ public class GlueService {
|
||||
GlueService(StorageBackend<String, Database> databaseStore,
|
||||
StorageBackend<String, Table> tableStore,
|
||||
StorageBackend<String, Partition> partitionStore,
|
||||
StorageBackend<String, UserDefinedFunction> functionStore,
|
||||
GlueSchemaRegistryService schemaRegistryService,
|
||||
RegionResolver regionResolver) {
|
||||
this.databaseStore = databaseStore;
|
||||
this.tableStore = tableStore;
|
||||
this.partitionStore = partitionStore;
|
||||
this.functionStore = functionStore;
|
||||
this.schemaRegistryService = schemaRegistryService;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
@@ -147,6 +157,75 @@ public class GlueService {
|
||||
return partitionStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
public void createUserDefinedFunction(String databaseName, UserDefinedFunction function) {
|
||||
getDatabase(databaseName);
|
||||
String key = functionKey(databaseName, function.getFunctionName());
|
||||
if (functionStore.get(key).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"Function already exists: " + databaseName + "." + function.getFunctionName(), 400);
|
||||
}
|
||||
function.setDatabaseName(databaseName);
|
||||
function.setCreateTime(Instant.now());
|
||||
functionStore.put(key, function);
|
||||
}
|
||||
|
||||
public UserDefinedFunction getUserDefinedFunction(String databaseName, String functionName) {
|
||||
getDatabase(databaseName);
|
||||
return functionStore.get(functionKey(databaseName, functionName))
|
||||
.orElseThrow(() -> new AwsException("EntityNotFoundException",
|
||||
"Function not found: " + databaseName + "." + functionName, 400));
|
||||
}
|
||||
|
||||
public List<UserDefinedFunction> getUserDefinedFunctions(String databaseName, String pattern) {
|
||||
return getUserDefinedFunctions(databaseName, pattern, null, null, null).functions();
|
||||
}
|
||||
|
||||
public UserDefinedFunctionPage getUserDefinedFunctions(
|
||||
String databaseName,
|
||||
String pattern,
|
||||
String functionType,
|
||||
Integer maxResults,
|
||||
String nextToken) {
|
||||
if (databaseName != null) {
|
||||
getDatabase(databaseName);
|
||||
}
|
||||
Pattern compiledPattern = compileFunctionPattern(pattern);
|
||||
int offset = decodeFunctionNextToken(nextToken);
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > MAX_FUNCTION_RESULTS)) {
|
||||
throw new AwsException("InvalidInputException", "MaxResults must be between 1 and 100", 400);
|
||||
}
|
||||
List<UserDefinedFunction> functions = functionStore.scan(k -> true).stream()
|
||||
.filter(function -> databaseName == null || databaseName.equals(function.getDatabaseName()))
|
||||
.filter(function -> functionType == null || functionType.equals(function.getFunctionType()))
|
||||
.filter(function -> function.getFunctionName() != null)
|
||||
.filter(function -> compiledPattern.matcher(function.getFunctionName()).matches())
|
||||
.sorted(Comparator.comparing(
|
||||
UserDefinedFunction::getDatabaseName,
|
||||
Comparator.nullsFirst(String::compareTo))
|
||||
.thenComparing(UserDefinedFunction::getFunctionName, Comparator.nullsFirst(String::compareTo)))
|
||||
.toList();
|
||||
if (offset > functions.size()) {
|
||||
throw new AwsException("InvalidInputException", "Invalid NextToken", 400);
|
||||
}
|
||||
int limit = maxResults == null ? functions.size() : maxResults;
|
||||
int end = Math.min(functions.size(), offset + limit);
|
||||
String newNextToken = end < functions.size() ? Integer.toString(end) : null;
|
||||
return new UserDefinedFunctionPage(functions.subList(offset, end), newNextToken);
|
||||
}
|
||||
|
||||
public void updateUserDefinedFunction(String databaseName, String functionName, UserDefinedFunction function) {
|
||||
UserDefinedFunction existing = getUserDefinedFunction(databaseName, functionName);
|
||||
function.setDatabaseName(databaseName);
|
||||
function.setFunctionName(functionName);
|
||||
function.setCreateTime(existing.getCreateTime());
|
||||
functionStore.put(functionKey(databaseName, functionName), function);
|
||||
}
|
||||
|
||||
public void deleteUserDefinedFunction(String databaseName, String functionName) {
|
||||
getUserDefinedFunction(databaseName, functionName);
|
||||
functionStore.delete(functionKey(databaseName, functionName));
|
||||
}
|
||||
|
||||
private void validateSchemaReference(Table table) {
|
||||
SchemaReference ref = schemaReferenceOf(table);
|
||||
if (ref == null) {
|
||||
@@ -189,6 +268,43 @@ public class GlueService {
|
||||
return sd != null ? sd.getSchemaReference() : null;
|
||||
}
|
||||
|
||||
private static String functionKey(String databaseName, String functionName) {
|
||||
return databaseName + ":" + functionName;
|
||||
}
|
||||
|
||||
private static Pattern compileFunctionPattern(String pattern) {
|
||||
if (pattern == null) {
|
||||
return Pattern.compile(".*");
|
||||
}
|
||||
if (pattern.length() > MAX_FUNCTION_PATTERN_LENGTH) {
|
||||
throw new AwsException("InvalidInputException", "Invalid function pattern: pattern is too long", 400);
|
||||
}
|
||||
try {
|
||||
return Pattern.compile(pattern);
|
||||
}
|
||||
catch (PatternSyntaxException e) {
|
||||
throw new AwsException("InvalidInputException", "Invalid function pattern: " + pattern, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static int decodeFunctionNextToken(String nextToken) {
|
||||
if (nextToken == null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
int offset = Integer.parseInt(nextToken);
|
||||
if (offset < 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInputException", "Invalid NextToken", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public record UserDefinedFunctionPage(List<UserDefinedFunction> functions, String nextToken) {}
|
||||
|
||||
private static Table copyTable(Table source) {
|
||||
Table copy = new Table();
|
||||
copy.setName(source.getName());
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.github.hectorvent.floci.services.glue.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class ResourceUri {
|
||||
@JsonProperty("ResourceType")
|
||||
private String resourceType;
|
||||
@JsonProperty("Uri")
|
||||
private String uri;
|
||||
|
||||
public ResourceUri() {}
|
||||
|
||||
public String getResourceType() { return resourceType; }
|
||||
public void setResourceType(String resourceType) { this.resourceType = resourceType; }
|
||||
public String getUri() { return uri; }
|
||||
public void setUri(String uri) { this.uri = uri; }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.hectorvent.floci.services.glue.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
public class UserDefinedFunction {
|
||||
@JsonProperty("FunctionName")
|
||||
private String functionName;
|
||||
@JsonProperty("DatabaseName")
|
||||
private String databaseName;
|
||||
@JsonProperty("ClassName")
|
||||
private String className;
|
||||
@JsonProperty("FunctionType")
|
||||
private String functionType;
|
||||
@JsonProperty("OwnerName")
|
||||
private String ownerName;
|
||||
@JsonProperty("OwnerType")
|
||||
private String ownerType;
|
||||
@JsonProperty("CreateTime")
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
private Instant createTime;
|
||||
@JsonProperty("ResourceUris")
|
||||
private List<ResourceUri> resourceUris;
|
||||
|
||||
public UserDefinedFunction() {}
|
||||
|
||||
public String getFunctionName() { return functionName; }
|
||||
public void setFunctionName(String functionName) { this.functionName = functionName; }
|
||||
public String getDatabaseName() { return databaseName; }
|
||||
public void setDatabaseName(String databaseName) { this.databaseName = databaseName; }
|
||||
public String getClassName() { return className; }
|
||||
public void setClassName(String className) { this.className = className; }
|
||||
public String getFunctionType() { return functionType; }
|
||||
public void setFunctionType(String functionType) { this.functionType = functionType; }
|
||||
public String getOwnerName() { return ownerName; }
|
||||
public void setOwnerName(String ownerName) { this.ownerName = ownerName; }
|
||||
public String getOwnerType() { return ownerType; }
|
||||
public void setOwnerType(String ownerType) { this.ownerType = ownerType; }
|
||||
public Instant getCreateTime() { return createTime; }
|
||||
public void setCreateTime(Instant createTime) { this.createTime = createTime; }
|
||||
public List<ResourceUri> getResourceUris() { return resourceUris; }
|
||||
public void setResourceUris(List<ResourceUri> resourceUris) { this.resourceUris = resourceUris; }
|
||||
}
|
||||
Reference in new issue
Block a user