feat(redshift): add external catalog statement parsing and metadata writers (#4679)

* feat(redshift): add external catalog statement parsing and metadata writers

* fix(redshift): harden external statement parsing and reference scanning

- Scanner: recognise dollar-quoted bodies, E'..\'..' strings and nested
  block comments; catch SELECT INTO, CREATE VIEW and CREATE INDEX writes;
  ignore FROM inside EXTRACT, SUBSTRING, TRIM and IS DISTINCT FROM.
- Parser: accept CREATE EXTERNAL SCHEMA IF NOT EXISTS, strip leading
  comments, support quoted names with dots, read TBLPROPERTIES and
  PARTITION values that contain parentheses or escaped quotes, validate
  IAM_ROLE as one quoted literal, and canonicalise Redshift type aliases
  to Hive types.
- Registry: percent-encode key parts so separators in identifiers cannot
  collide.
- Backend session: do not treat socket write failures as source read
  failures during COPY.
- Keep property order stable in statements.

* fix(redshift): address external catalog review findings

* fix(redshift): inherit omitted partition settings

* fix(redshift): preserve explicit empty partition settings

* fix(redshift): canonicalise character and varbyte external column types

* fix(redshift): default unsized character types

* fix(redshift): enforce Spectrum varchar default length

* fix(redshift): null surplus varchar values during Spectrum copy
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-02 13:42:21 -04:00
1 parent 5de9b0d6c1
commit d7d2f73e99
22 files changed
+2607 -14

No files matched your search

@@ -0,0 +1,8 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import java.io.InputStream;
public interface BackendSql {
void execute(String sql);
long copyIn(String copySql, InputStream data);
}
@@ -0,0 +1,80 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import jakarta.enterprise.context.ApplicationScoped;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
@ApplicationScoped
public class ExternalCatalogRegistry implements Resettable {
private static final char SEPARATOR = '|';
private final AccountAwareStorageBackend<ExternalSchemaBinding> bindings;
public ExternalCatalogRegistry(StorageFactory storageFactory) {
bindings = storageFactory.create("redshift", "redshift-external-schemas.json", new TypeReference<>() { });
}
public synchronized void bind(ExternalSchemaBinding binding) {
String key = key(binding.clusterKey(), binding.databaseName(), binding.schemaName());
if (bindings.getForAccount(binding.accountId(), key).isPresent()) {
throw new SpectrumSqlException("42P06", "schema \"" + binding.schemaName() + "\" already exists");
}
bindings.putForAccount(binding.accountId(), key, binding);
bindings.flush();
}
public Optional<ExternalSchemaBinding> find(String accountId, String clusterKey, String databaseName, String schemaName) {
return bindings.getForAccount(accountId, key(clusterKey, databaseName, schemaName));
}
public List<ExternalSchemaBinding> list(String accountId, String clusterKey, String databaseName) {
String prefix = prefix(clusterKey, databaseName);
return bindings.scanForAccount(accountId, key -> key.startsWith(prefix)).stream()
.sorted(Comparator.comparing(ExternalSchemaBinding::schemaName)).toList();
}
public synchronized void unbind(String accountId, String clusterKey, String databaseName, String schemaName) {
bindings.deleteForAccount(accountId, key(clusterKey, databaseName, schemaName));
bindings.flush();
}
public synchronized void removeCluster(String accountId, String clusterKey) {
String prefix = prefix(clusterKey);
for (String key : bindings.keysForAccount(accountId)) {
if (key.startsWith(prefix)) {
bindings.deleteForAccount(accountId, key);
}
}
bindings.flush();
}
@Override
public synchronized void clear() {
bindings.clear();
bindings.flush();
}
private static String key(String clusterKey, String databaseName, String schemaName) {
return prefix(clusterKey, databaseName) + encode(schemaName);
}
private static String prefix(String clusterKey, String databaseName) {
return prefix(clusterKey) + encode(databaseName) + SEPARATOR;
}
private static String prefix(String clusterKey) {
return encode(clusterKey) + SEPARATOR;
}
/** Percent-encodes a component so a quoted identifier containing the separator cannot collide with another key. */
private static String encode(String component) {
return URLEncoder.encode(component, StandardCharsets.UTF_8);
}
}
@@ -0,0 +1,189 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.services.glue.GlueService;
import io.github.hectorvent.floci.services.glue.model.Column;
import io.github.hectorvent.floci.services.glue.model.Partition;
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
import io.github.hectorvent.floci.services.glue.model.Table;
import jakarta.enterprise.context.ApplicationScoped;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/** Mirrors Glue metadata into the tables exposed by the SVV external catalog views. */
@ApplicationScoped
public class ExternalMetadataWriter {
private static final Logger LOG = Logger.getLogger(ExternalMetadataWriter.class);
private final GlueService glueService;
private final ObjectMapper objectMapper;
public ExternalMetadataWriter(GlueService glueService, ObjectMapper objectMapper) {
this.glueService = glueService;
this.objectMapper = objectMapper;
}
public void refresh(BackendSql backend, String accountId, ExternalSchemaBinding binding) {
List<Table> tables = RequestScopes.callAs(accountId, () -> glueService.getTables(binding.glueDatabase()));
Map<String, List<Partition>> partitions = new LinkedHashMap<>();
for (Table table : tables) {
partitions.put(table.getName(), RequestScopes.callAs(accountId,
() -> glueService.getPartitions(binding.glueDatabase(), table.getName())));
}
run(backend, refreshSql(binding, tables, partitions), false);
}
public void purge(BackendSql backend, String schemaName) {
run(backend, purgeSql(schemaName), true);
}
private void run(BackendSql backend, String sql, boolean tolerateMissingBootstrapTables) {
try {
backend.execute(sql);
} catch (SpectrumReadException exception) {
if (!tolerateMissingBootstrapTables || !"42P01".equals(exception.sqlState())) {
throw exception;
}
LOG.warnv("Spectrum metadata tables are missing; restart the cluster to re-run the catalog bootstrap: {0}",
exception.getMessage());
}
}
String purgeSql(String schemaName) {
return "SELECT floci_internal.purge_external_schema(" + literal(schemaName) + ")";
}
String refreshSql(ExternalSchemaBinding binding, List<Table> tables,
Map<String, List<Partition>> partitionsByTable) {
List<Map<String, Object>> tableRows = new ArrayList<>();
List<Map<String, Object>> columnRows = new ArrayList<>();
List<Map<String, Object>> partitionRows = new ArrayList<>();
for (Table table : tables) {
StorageDescriptor descriptor = table.getStorageDescriptor();
String serdeLibrary = serdeLibrary(descriptor);
String serdeParameters = json(serdeParameters(descriptor));
int compressed = descriptor != null && Boolean.TRUE.equals(descriptor.getCompressed()) ? 1 : 0;
tableRows.add(row(
"tablename", table.getName(),
"tabletype", "VIRTUAL_VIEW".equals(table.getTableType()) ? "VIEW" : "TABLE",
"location", descriptor == null ? "" : descriptor.getLocation(),
"input_format", descriptor == null ? "" : descriptor.getInputFormat(),
"output_format", descriptor == null ? "" : descriptor.getOutputFormat(),
"serialization_lib", serdeLibrary,
"serde_parameters", serdeParameters,
"compressed", compressed,
"parameters", json(table.getParameters())));
appendColumns(columnRows, table);
List<Partition> partitions = partitionsByTable.get(table.getName());
if (partitions != null) {
for (Partition partition : partitions) {
appendPartition(partitionRows, table.getName(), partition, descriptor);
}
}
}
Map<String, Object> payload = row(
"databasename", binding.glueDatabase(),
"esoptions", json(Map.of("IAM_ROLE", binding.iamRoleArn())),
"tables", tableRows,
"columns", columnRows,
"partitions", partitionRows);
return "SELECT floci_internal.refresh_external_catalog(" + literal(binding.schemaName()) + ", "
+ literal(json(payload)) + "::jsonb)";
}
private static void appendColumns(List<Map<String, Object>> rows, Table table) {
StorageDescriptor descriptor = table.getStorageDescriptor();
List<Column> dataColumns = descriptor == null || descriptor.getColumns() == null
? List.of() : descriptor.getColumns();
List<Column> partitionKeys = table.getPartitionKeys() == null ? List.of() : table.getPartitionKeys();
int number = 1;
for (Column column : dataColumns) {
appendColumn(rows, table.getName(), column, number++, 0);
}
int partitionOrder = 1;
for (Column column : partitionKeys) {
appendColumn(rows, table.getName(), column, number++, partitionOrder++);
}
}
private static void appendColumn(List<Map<String, Object>> rows, String tableName, Column column,
int columnNumber, int partitionOrder) {
rows.add(row("tablename", tableName,
"columnname", column.getName(),
"external_type", column.getType(),
"columnnum", columnNumber,
"part_key", partitionOrder,
"is_nullable", "true"));
}
private void appendPartition(List<Map<String, Object>> rows, String tableName, Partition partition,
StorageDescriptor tableDescriptor) {
StorageDescriptor descriptor = partition.getStorageDescriptor();
String values = jsonValues(partition.getValues());
String serializationLibrary = serdeLibrary(descriptor);
if (serializationLibrary.isBlank()) {
serializationLibrary = serdeLibrary(tableDescriptor);
}
Map<String, String> parameters = serdeParameters(descriptor);
if (parameters == null) {
parameters = serdeParameters(tableDescriptor);
}
Boolean compressedValue = descriptor == null ? null : descriptor.getCompressed();
if (compressedValue == null && tableDescriptor != null) {
compressedValue = tableDescriptor.getCompressed();
}
rows.add(row("tablename", tableName,
"values", values,
"location", descriptor == null ? "" : descriptor.getLocation(),
"input_format", descriptor == null ? "" : descriptor.getInputFormat(),
"output_format", descriptor == null ? "" : descriptor.getOutputFormat(),
"serialization_lib", serializationLibrary,
"serde_parameters", json(parameters),
"compressed", Boolean.TRUE.equals(compressedValue) ? 1 : 0,
"parameters", json(partition.getParameters())));
}
private static String serdeLibrary(StorageDescriptor descriptor) {
return descriptor == null || descriptor.getSerdeInfo() == null
|| descriptor.getSerdeInfo().getSerializationLibrary() == null
? "" : descriptor.getSerdeInfo().getSerializationLibrary();
}
private static Map<String, String> serdeParameters(StorageDescriptor descriptor) {
return descriptor == null || descriptor.getSerdeInfo() == null
? null : descriptor.getSerdeInfo().getParameters();
}
private String json(Object value) {
try {
return objectMapper.writeValueAsString(value == null ? new LinkedHashMap<>() : value);
} catch (JsonProcessingException exception) {
throw new IllegalStateException("Unable to serialize external table metadata", exception);
}
}
private String jsonValues(List<String> values) {
try {
return objectMapper.writeValueAsString(values == null ? List.of() : values);
} catch (JsonProcessingException exception) {
throw new IllegalStateException("Unable to serialize external partition values", exception);
}
}
private static String literal(String value) {
return "'" + (value == null ? "" : value.replace("'", "''")) + "'";
}
private static Map<String, Object> row(Object... values) {
Map<String, Object> result = new LinkedHashMap<>();
for (int index = 0; index < values.length; index += 2) {
result.put((String) values[index], values[index + 1]);
}
return result;
}
}
@@ -0,0 +1,456 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
/**
* Finds references to bound external schemas while ignoring literals and comments.
*
* <p>A qualified name only counts as a table reference where a table can appear: after
* {@code FROM}, {@code JOIN}, {@code USING}, {@code INTO} or {@code UPDATE}, and in a comma-separated
* {@code FROM} list. Anywhere else (a select list, {@code ON}, {@code WHERE}) {@code schema.name}
* is a column qualifier, so a table aliased with an external schema's name is not mistaken for it.
*/
public final class ExternalReferenceScanner {
/** Keywords after which the following names are table sources. */
private static final Set<String> TABLE_INTRODUCERS = Set.of("from", "join", "using", "into", "update");
/** Functions whose argument list uses FROM as an expression separator, not to start a table list. */
private static final Set<String> EXPRESSION_FROM_FUNCTIONS = Set.of("extract", "substring", "trim", "overlay", "position");
/** Keywords that end a table-source list: what follows is an expression or a new clause. */
private static final Set<String> CLAUSE_ENDERS = Set.of("select", "where", "group", "order", "having",
"limit", "offset", "union", "intersect", "except", "on", "set", "values", "returning", "window",
"fetch", "qualify");
public record Reference(String schema, String table) {
}
private record Word(String value, boolean quoted) {
}
/** An identifier chain ({@code a.b.c}) or one structural character. */
private record Token(List<Word> chain, char symbol) {
boolean isChain() {
return chain != null;
}
boolean isKeyword(String keyword) {
return chain != null && chain.size() == 1 && !chain.getFirst().quoted()
&& chain.getFirst().value().equals(keyword);
}
String keyword() {
return chain != null && chain.size() == 1 && !chain.getFirst().quoted() ? chain.getFirst().value() : null;
}
}
private ExternalReferenceScanner() {
}
public static Set<Reference> scan(String sql, Set<String> schemaNames) {
Set<Reference> result = new LinkedHashSet<>();
Deque<Boolean> outerContexts = new ArrayDeque<>();
Deque<Boolean> expressionFromParens = new ArrayDeque<>();
boolean tableContext = false;
String previousKeyword = null;
for (Token token : tokenize(sql)) {
if (!token.isChain()) {
switch (token.symbol()) {
case '(' -> {
outerContexts.push(tableContext);
expressionFromParens.push(previousKeyword != null && EXPRESSION_FROM_FUNCTIONS.contains(previousKeyword));
}
case ')' -> {
tableContext = !outerContexts.isEmpty() && outerContexts.pop();
if (!expressionFromParens.isEmpty()) {
expressionFromParens.pop();
}
}
case ';' -> {
tableContext = false;
outerContexts.clear();
expressionFromParens.clear();
}
default -> {
// a comma keeps the current context: it separates items of the same list
}
}
previousKeyword = null;
continue;
}
String keyword = token.keyword();
boolean expressionFrom = "from".equals(keyword)
&& ("distinct".equals(previousKeyword) || !expressionFromParens.isEmpty() && expressionFromParens.peek());
previousKeyword = keyword;
if (expressionFrom) {
continue;
}
if (keyword != null && TABLE_INTRODUCERS.contains(keyword)) {
tableContext = true;
} else if (keyword != null && CLAUSE_ENDERS.contains(keyword)) {
tableContext = false;
} else if (tableContext) {
Reference reference = reference(token.chain(), schemaNames);
if (reference != null) {
result.add(reference);
}
}
}
return result;
}
public static boolean containsIdentifierPrefix(String sql, String prefix) {
if (sql == null || prefix == null) {
return false;
}
return tokenize(sql).stream().filter(Token::isChain).flatMap(token -> token.chain().stream())
.anyMatch(word -> word.value().regionMatches(true, 0, prefix, 0, prefix.length()));
}
/**
* The first external table or schema an {@code INSERT}, {@code UPDATE}, {@code DELETE}, {@code MERGE},
* {@code TRUNCATE}, {@code COPY}, {@code SELECT INTO}, {@code CREATE TABLE}, {@code CREATE VIEW}, {@code CREATE INDEX},
* {@code ALTER TABLE} or {@code DROP} anywhere in
* {@code sql} changes: every statement of a batch and every data-modifying CTE is checked, not only
* the first write. The external DDL Floci implements is parsed before this is reached.
*/
public static Optional<Reference> writeTarget(String sql, Set<String> schemaNames) {
if (sql == null) {
return Optional.empty();
}
List<Token> tokens = tokenize(sql);
for (int i = 0; i < tokens.size(); i++) {
Optional<Reference> moved = movedIntoExternalSchema(tokens, i, schemaNames);
if (moved.isPresent()) {
return moved;
}
Optional<Reference> truncated = truncatedTarget(tokens, i, schemaNames);
if (truncated.isPresent()) {
return truncated;
}
int target = writeTargetIndex(tokens, i);
if (target >= 0 && target < tokens.size() && tokens.get(target).isChain()) {
Reference reference = reference(tokens.get(target).chain(), schemaNames);
if (reference != null) {
return Optional.of(reference);
}
}
Optional<Reference> dropped = droppedTarget(tokens, i, schemaNames);
if (dropped.isPresent()) {
return dropped;
}
}
return Optional.empty();
}
private static Optional<Reference> movedIntoExternalSchema(List<Token> tokens, int i, Set<String> schemaNames) {
if (!keywordAt(tokens, i, "alter") || !keywordAt(tokens, i + 1, "table")) {
return Optional.empty();
}
int source = skipOnly(tokens, i + 2);
if (keywordAt(tokens, source, "if") && keywordAt(tokens, source + 1, "exists")) {
source += 2;
}
int operation = source + 1;
if (keywordAt(tokens, operation, "rename") && keywordAt(tokens, operation + 1, "to")) {
int target = operation + 2;
if (target < tokens.size() && tokens.get(target).isChain()) {
return Optional.ofNullable(reference(tokens.get(target).chain(), schemaNames));
}
}
if (keywordAt(tokens, operation, "set") && keywordAt(tokens, operation + 1, "schema")) {
int target = operation + 2;
if (target < tokens.size() && tokens.get(target).isChain()) {
Word schema = tokens.get(target).chain().getFirst();
if (schemaNames.contains(schema.value())) {
return Optional.of(new Reference(schema.value(), ""));
}
}
}
return Optional.empty();
}
/** {@code DROP TABLE a, b} and {@code DROP SCHEMA a, b}: any name of the list may be an external one. */
private static Optional<Reference> droppedTarget(List<Token> tokens, int i, Set<String> schemaNames) {
if (!keywordAt(tokens, i, "drop")) {
return Optional.empty();
}
boolean table = keywordAt(tokens, i + 1, "table");
if (!table && !keywordAt(tokens, i + 1, "schema")) {
return Optional.empty();
}
int index = i + 2;
if (keywordAt(tokens, index, "if") && keywordAt(tokens, index + 1, "exists")) {
index += 2;
}
while (index < tokens.size() && tokens.get(index).isChain()) {
List<Word> chain = tokens.get(index).chain();
Reference reference = table ? reference(chain, schemaNames)
: schemaNames.contains(chain.getFirst().value()) ? new Reference(chain.getFirst().value(), "") : null;
if (reference != null) {
return Optional.of(reference);
}
boolean anotherName = index + 1 < tokens.size() && !tokens.get(index + 1).isChain()
&& tokens.get(index + 1).symbol() == ',';
index += anotherName ? 2 : tokens.size();
}
return Optional.empty();
}
private static int writeTargetIndex(List<Token> tokens, int i) {
if (keywordAt(tokens, i, "create")) {
int index = i + 1;
while (keywordAt(tokens, index, "temp") || keywordAt(tokens, index, "temporary")
|| keywordAt(tokens, index, "unlogged") || keywordAt(tokens, index, "local")) {
index++;
}
if (keywordAt(tokens, index, "or") && keywordAt(tokens, index + 1, "replace")) {
index += 2;
while (keywordAt(tokens, index, "temp") || keywordAt(tokens, index, "temporary")) {
index++;
}
}
if (keywordAt(tokens, index, "materialized")) {
index++;
}
if (keywordAt(tokens, index, "unique")) {
index++;
}
if (keywordAt(tokens, index, "index")) {
return indexTable(tokens, index + 1);
}
if (!(keywordAt(tokens, index, "table") || keywordAt(tokens, index, "view"))) {
return -1;
}
index++;
if (keywordAt(tokens, index, "if") && keywordAt(tokens, index + 1, "not") && keywordAt(tokens, index + 2, "exists")) {
index += 3;
}
return index;
}
if (keywordAt(tokens, i, "alter") && keywordAt(tokens, i + 1, "table")) {
int index = skipOnly(tokens, i + 2);
if (keywordAt(tokens, index, "if") && keywordAt(tokens, index + 1, "exists")) {
index += 2;
}
return skipOnly(tokens, index);
}
if (keywordAt(tokens, i, "copy")) {
return i + 1;
}
if (keywordAt(tokens, i, "into")) {
int index = i + 1;
while (keywordAt(tokens, index, "temp") || keywordAt(tokens, index, "temporary")
|| keywordAt(tokens, index, "unlogged") || keywordAt(tokens, index, "table")) {
index++;
}
return skipOnly(tokens, index);
}
if (keywordAt(tokens, i, "update")) {
return skipOnly(tokens, i + 1);
}
if (keywordAt(tokens, i, "delete") && keywordAt(tokens, i + 1, "from")) {
return skipOnly(tokens, i + 2);
}
return -1;
}
/** {@code CREATE INDEX [CONCURRENTLY] [IF NOT EXISTS] name ON [ONLY] table}: {@code from} follows INDEX. */
private static int indexTable(List<Token> tokens, int from) {
for (int index = from; index < from + 6 && index < tokens.size(); index++) {
if (keywordAt(tokens, index, "on")) {
return skipOnly(tokens, index + 1);
}
}
return -1;
}
private static Optional<Reference> truncatedTarget(List<Token> tokens, int i, Set<String> schemaNames) {
if (!keywordAt(tokens, i, "truncate")) {
return Optional.empty();
}
int target = skipOnly(tokens, keywordAt(tokens, i + 1, "table") ? i + 2 : i + 1);
while (target < tokens.size() && tokens.get(target).isChain()) {
Reference reference = reference(tokens.get(target).chain(), schemaNames);
if (reference != null) {
return Optional.of(reference);
}
int comma = target + 1;
if (comma >= tokens.size() || tokens.get(comma).isChain() || tokens.get(comma).symbol() != ',') {
return Optional.empty();
}
target = skipOnly(tokens, comma + 1);
}
return Optional.empty();
}
private static int skipOnly(List<Token> tokens, int index) {
return keywordAt(tokens, index, "only") ? index + 1 : index;
}
private static boolean keywordAt(List<Token> tokens, int index, String keyword) {
return index < tokens.size() && tokens.get(index).isKeyword(keyword);
}
private static Reference reference(List<Word> chain, Set<String> schemas) {
if (chain.size() >= 2 && schemas.contains(chain.get(0).value())) {
return new Reference(chain.get(0).value(), chain.get(1).value());
}
if (chain.size() >= 3 && schemas.contains(chain.get(1).value())) {
return new Reference(chain.get(1).value(), chain.get(2).value());
}
return null;
}
private static List<Token> tokenize(String sql) {
List<Token> result = new ArrayList<>();
int i = 0;
while (i < sql.length()) {
char c = sql.charAt(i);
if (c == '\'' || c == '-' && i + 1 < sql.length() && sql.charAt(i + 1) == '-'
|| c == '/' && i + 1 < sql.length() && sql.charAt(i + 1) == '*') {
i = skip(sql, i, c == '\'' && isEscapeStringPrefix(sql, i));
} else if (c == '$' && dollarTagEnd(sql, i) > 0) {
i = skipDollarQuoted(sql, i);
} else if (c == '"' || Character.isLetter(c) || c == '_') {
List<Word> chain = new ArrayList<>();
i = read(sql, i, chain);
while (true) {
int separator = skipTrivia(sql, i);
if (separator >= sql.length() || sql.charAt(separator) != '.') {
break;
}
int next = skipTrivia(sql, separator + 1);
if (next >= sql.length() || !(sql.charAt(next) == '"' || Character.isLetter(sql.charAt(next))
|| sql.charAt(next) == '_')) {
break;
}
i = read(sql, next, chain);
}
result.add(new Token(chain, '\0'));
} else {
if (c == '(' || c == ')' || c == ',' || c == ';') {
result.add(new Token(null, c));
}
i++;
}
}
return result;
}
private static int skipTrivia(String sql, int start) {
int i = start;
while (i < sql.length()) {
if (Character.isWhitespace(sql.charAt(i))) {
i++;
} else if (sql.charAt(i) == '-' && i + 1 < sql.length() && sql.charAt(i + 1) == '-'
|| sql.charAt(i) == '/' && i + 1 < sql.length() && sql.charAt(i + 1) == '*') {
i = skip(sql, i, false);
} else {
break;
}
}
return i;
}
private static int read(String sql, int start, List<Word> chain) {
if (sql.charAt(start) == '"') {
StringBuilder value = new StringBuilder();
int i = start + 1;
while (i < sql.length()) {
if (sql.charAt(i) == '"') {
if (i + 1 < sql.length() && sql.charAt(i + 1) == '"') {
value.append('"');
i += 2;
} else {
i++;
break;
}
} else {
value.append(sql.charAt(i++));
}
}
chain.add(new Word(value.toString(), true));
return i;
}
int i = start;
while (i < sql.length() && (Character.isLetterOrDigit(sql.charAt(i)) || sql.charAt(i) == '_'
|| sql.charAt(i) == '$')) {
i++;
}
chain.add(new Word(sql.substring(start, i).toLowerCase(Locale.ROOT), false));
return i;
}
private static boolean isEscapeStringPrefix(String sql, int quote) {
if (quote == 0 || Character.toLowerCase(sql.charAt(quote - 1)) != 'e') {
return false;
}
if (quote == 1) {
return true;
}
char before = sql.charAt(quote - 2);
return !(Character.isLetterOrDigit(before) || before == '_' || before == '$');
}
/** Returns the index just past an opening dollar-quote tag such as {@code $$} or {@code $body$}, or -1. */
private static int dollarTagEnd(String sql, int start) {
int i = start + 1;
if (i < sql.length() && (Character.isLetter(sql.charAt(i)) || sql.charAt(i) == '_')) {
while (i < sql.length() && (Character.isLetterOrDigit(sql.charAt(i)) || sql.charAt(i) == '_')) {
i++;
}
}
return i < sql.length() && sql.charAt(i) == '$' ? i + 1 : -1;
}
private static int skipDollarQuoted(String sql, int start) {
int bodyStart = dollarTagEnd(sql, start);
int close = sql.indexOf(sql.substring(start, bodyStart), bodyStart);
return close < 0 ? sql.length() : close + (bodyStart - start);
}
private static int skip(String sql, int start, boolean backslashEscapes) {
if (sql.charAt(start) == '\'') {
int i = start + 1;
while (i < sql.length()) {
char c = sql.charAt(i);
if (backslashEscapes && c == '\\') {
i += 2;
} else if (c == '\'' && (i + 1 >= sql.length() || sql.charAt(i + 1) != '\'')) {
return i + 1;
} else {
i += c == '\'' ? 2 : 1;
}
}
return sql.length();
}
if (sql.charAt(start) == '-') {
int i = sql.indexOf('\n', start + 2);
return i < 0 ? sql.length() : i;
}
int depth = 1;
int i = start + 2;
while (i < sql.length() && depth > 0) {
if (sql.startsWith("/*", i)) {
depth++;
i += 2;
} else if (sql.startsWith("*/", i)) {
depth--;
i += 2;
} else {
i++;
}
}
return Math.min(i, sql.length());
}
}
@@ -0,0 +1,22 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public record ExternalSchemaBinding(String accountId, String clusterKey, String databaseName,
String schemaName, String glueDatabase, String iamRoleArn) {
public ExternalSchemaBinding {
require(accountId, "accountId");
require(clusterKey, "clusterKey");
require(databaseName, "databaseName");
require(schemaName, "schemaName");
require(glueDatabase, "glueDatabase");
require(iamRoleArn, "iamRoleArn");
}
private static void require(String value, String field) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException(field + " is required");
}
}
}
@@ -0,0 +1,46 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public sealed interface ExternalStatement permits ExternalStatement.CreateSchema, ExternalStatement.CreateTable,
ExternalStatement.AddPartitions, ExternalStatement.DropSchema, ExternalStatement.DropTable {
/** Read-only copy that keeps statement order, so Glue payloads are stable across runs. */
private static <V> Map<String, V> ordered(Map<String, V> source) {
return Collections.unmodifiableMap(new LinkedHashMap<>(source));
}
record ColumnDefinition(String name, String type) { }
enum TableFormat { PARQUET, TEXTFILE, JSON }
record PartitionSpec(Map<String, String> values, String location) {
public PartitionSpec { values = ordered(values); }
}
record CreateSchema(String schemaName, String glueDatabase, String iamRoleArn, boolean createDatabaseIfNotExists,
boolean ifNotExists) implements ExternalStatement { }
record CreateTable(String schemaName, String tableName, List<ColumnDefinition> columns,
List<ColumnDefinition> partitionColumns, TableFormat format, String location,
String delimiter, String serde, Map<String, String> properties,
Map<String, String> serdeProperties) implements ExternalStatement {
public CreateTable {
columns = List.copyOf(columns);
partitionColumns = List.copyOf(partitionColumns);
properties = ordered(properties);
serdeProperties = ordered(serdeProperties);
}
public CreateTable(String schemaName, String tableName, List<ColumnDefinition> columns,
List<ColumnDefinition> partitionColumns, TableFormat format, String location,
String delimiter, String serde, Map<String, String> properties) {
this(schemaName, tableName, columns, partitionColumns, format, location, delimiter, serde, properties, Map.of());
}
}
record AddPartitions(String schemaName, String tableName, boolean ifNotExists,
List<PartitionSpec> partitions) implements ExternalStatement {
public AddPartitions { partitions = List.copyOf(partitions); }
}
/** {@code cascade} is true only when the statement said CASCADE; RESTRICT is the default, as in PostgreSQL and Redshift. */
record DropSchema(String schemaName, boolean ifExists, boolean cascade) implements ExternalStatement { }
record DropTable(String schemaName, String tableName, boolean ifExists, boolean cascade) implements ExternalStatement { }
}
@@ -0,0 +1,323 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ApplicationScoped
public class ExternalStatementParser {
private static final Pattern CREATE_SCHEMA = Pattern.compile("(?is)^\\s*CREATE\\s+EXTERNAL\\s+SCHEMA\\s+(IF\\s+NOT\\s+EXISTS\\s+)?(.+?)\\s+FROM\\s+DATA\\s+CATALOG\\s+DATABASE\\s+'((?:''|[^'])*)'(?:\\s+REGION\\s+'(?:''|[^'])*')?\\s+IAM_ROLE\\s+(.+?)(\\s+CREATE\\s+EXTERNAL\\s+DATABASE\\s+IF\\s+NOT\\s+EXISTS)?\\s*;?\\s*$");
private static final Pattern CREATE_EXTERNAL_TABLE = Pattern.compile("(?is)^\\s*CREATE\\s+EXTERNAL\\s+TABLE\\b.*");
private static final Pattern CREATE_EXTERNAL = Pattern.compile("(?is)^\\s*CREATE\\s+EXTERNAL\\b.*");
private static final Pattern SERDE_PROPERTIES = Pattern.compile("(?is)WITH\\s+SERDEPROPERTIES\\s*\\(");
private static final String NAME = "(\"(?:\"\"|[^\"])+\"|[^.;,\\s\"()]+)";
private static final Pattern CREATE_TABLE_HEAD = Pattern.compile("(?is)^\\s*CREATE\\s+EXTERNAL\\s+TABLE\\s+" + NAME + "\\s*\\.\\s*" + NAME + "\\s*\\(");
private static final Pattern DROP_TABLE = Pattern.compile("(?is)^\\s*DROP\\s+TABLE\\s+(IF\\s+EXISTS\\s+)?" + NAME + "\\.\\s*" + NAME + "(?:\\s+(CASCADE|RESTRICT))?\\s*;?\\s*$");
private static final Pattern DROP_SCHEMA = Pattern.compile("(?is)^\\s*DROP\\s+SCHEMA\\s+(IF\\s+EXISTS\\s+)?" + NAME + "(?:\\s+(CASCADE|RESTRICT))?\\s*;?\\s*$");
private static final Pattern TABLE_PROPERTIES = Pattern.compile("(?is)(?:TBLPROPERTIES|TABLE\\s+PROPERTIES)\\s*\\(");
private static final Pattern PARTITION_VALUE = Pattern.compile("(?is)([^,=]+)=\\s*'((?:''|[^'])*)'\\s*(?:,|$)");
private static final Pattern LOCATION_VALUE = Pattern.compile("(?is)LOCATION\\s+'((?:''|[^'])*)'");
private static final Pattern SERDE_VALUE = Pattern.compile("(?is)SERDE\\s+'((?:''|[^'])*)'");
private static final Pattern TERMINATED_BY_VALUE = Pattern.compile("(?is)TERMINATED BY\\s+'((?:''|[^'])*)'");
private static final Pattern STORED_AS_VALUE = Pattern.compile("(?is)STORED AS\\s+([A-Za-z]+)");
private static final Pattern QUOTED_LITERAL = Pattern.compile("'(?:''|[^'])*'");
private static final Pattern QUOTED_PAIR = Pattern.compile("'((?:''|[^'])*)'\\s*=\\s*'((?:''|[^'])*)'");
private static final Pattern PARTITION_HEAD = Pattern.compile("(?is)PARTITION\\s*\\(");
private static final Pattern PARTITION_LOCATION = Pattern.compile("(?is)\\s*LOCATION\\s+'((?:''|[^'])*)'");
private static final Pattern PARTITIONED_BY = Pattern.compile("(?is)PARTITIONED\\s+BY\\s*\\(");
private static final Pattern ADD_PARTITIONS = Pattern.compile("(?is)^\\s*ALTER\\s+TABLE\\s+" + NAME + "\\.\\s*" + NAME + "\\s+ADD\\s+(IF\\s+NOT\\s+EXISTS\\s+)?(.+?)\\s*;?\\s*$");
public Optional<ExternalStatement> parse(String sql) {
if (sql == null) {
return Optional.empty();
}
String text = stripLeadingComments(sql);
Matcher schema = CREATE_SCHEMA.matcher(text);
if (schema.matches()) {
String role = schema.group(4).trim();
if (role.equalsIgnoreCase("default")) {
throw new SpectrumSqlException("0A000", "IAM_ROLE DEFAULT is not supported");
}
if (!QUOTED_LITERAL.matcher(role).matches()) {
throw new SpectrumSqlException("42601", "IAM_ROLE must be a single quoted role ARN");
}
return Optional.of(new ExternalStatement.CreateSchema(identifier(schema.group(2)),
schema.group(3).replace("''", "'"), unquote(role), schema.group(5) != null,
schema.group(1) != null));
}
if (CREATE_EXTERNAL_TABLE.matcher(text).matches()) {
return Optional.of(parseTable(text));
}
if (CREATE_EXTERNAL.matcher(text).matches()) {
throw new SpectrumSqlException("0A000", "unsupported CREATE EXTERNAL statement");
}
Matcher dropTable = DROP_TABLE.matcher(text);
if (dropTable.matches()) {
return Optional.of(new ExternalStatement.DropTable(identifier(dropTable.group(2)), identifier(dropTable.group(3)),
dropTable.group(1) != null, "CASCADE".equalsIgnoreCase(dropTable.group(4))));
}
Matcher dropSchema = DROP_SCHEMA.matcher(text);
if (dropSchema.matches()) {
return Optional.of(new ExternalStatement.DropSchema(identifier(dropSchema.group(2)),
dropSchema.group(1) != null, "CASCADE".equalsIgnoreCase(dropSchema.group(3))));
}
Matcher addPartitions = ADD_PARTITIONS.matcher(text);
if (addPartitions.matches()) {
List<ExternalStatement.PartitionSpec> partitions = parsePartitions(addPartitions.group(4));
return partitions.isEmpty() ? Optional.empty() : Optional.of(new ExternalStatement.AddPartitions(
identifier(addPartitions.group(1)), identifier(addPartitions.group(2)),
addPartitions.group(3) != null, partitions));
}
return Optional.empty();
}
private ExternalStatement.CreateTable parseTable(String text) {
Matcher matcher = CREATE_TABLE_HEAD.matcher(text);
if (!matcher.find()) {
throw new SpectrumSqlException("0A000", "malformed CREATE EXTERNAL TABLE");
}
int open = matcher.end() - 1;
int close = matchingParen(text, open);
if (close < 0) {
throw new SpectrumSqlException("0A000", "malformed CREATE EXTERNAL TABLE column list");
}
String tail = text.substring(close + 1);
List<ExternalStatement.ColumnDefinition> columns = columns(text.substring(open + 1, close));
List<ExternalStatement.ColumnDefinition> partitions = List.of();
Matcher partition = PARTITIONED_BY.matcher(tail);
if (partition.find()) {
// Partition columns may have types such as decimal(10,2), so match nested parentheses.
int partitionOpen = partition.end() - 1;
int partitionClose = matchingParen(tail, partitionOpen);
if (partitionClose < 0) {
throw new SpectrumSqlException("0A000", "malformed CREATE EXTERNAL TABLE partition list");
}
partitions = columns(tail.substring(partitionOpen + 1, partitionClose));
}
String location = value(tail, LOCATION_VALUE);
if (location == null || !location.startsWith("s3://")) {
throw new SpectrumSqlException("22023", "external table location must be an s3:// URI");
}
String serde = value(tail, SERDE_VALUE);
String delimiter = value(tail, TERMINATED_BY_VALUE);
String stored = keywordValue(tail, STORED_AS_VALUE);
if (stored == null) {
throw new SpectrumSqlException("0A000", "external table requires a STORED AS clause");
}
if (!(stored.equalsIgnoreCase("PARQUET") || stored.equalsIgnoreCase("TEXTFILE"))) {
throw new SpectrumSqlException("0A000", "unsupported external table format: " + stored);
}
ExternalStatement.TableFormat format = stored.equalsIgnoreCase("PARQUET")
? ExternalStatement.TableFormat.PARQUET
: serde != null && serde.toLowerCase(Locale.ROOT).contains("json")
? ExternalStatement.TableFormat.JSON : ExternalStatement.TableFormat.TEXTFILE;
Map<String, String> properties = properties(tail);
return new ExternalStatement.CreateTable(identifier(matcher.group(1)), identifier(matcher.group(2)), columns, partitions,
format, location, delimiter, serde, properties, serdeProperties(tail));
}
private static String stripLeadingComments(String sql) {
String text = sql.trim();
while (true) {
int end;
if (text.startsWith("--")) {
end = text.indexOf('\n');
end = end < 0 ? text.length() : end;
} else if (text.startsWith("/*")) {
end = text.indexOf("*/", 2);
end = end < 0 ? text.length() : end + 2;
} else {
return text;
}
text = text.substring(end).trim();
}
}
private static int matchingParen(String text, int open) {
int depth = 0;
char quote = 0;
for (int i = open; i < text.length(); i++) {
char c = text.charAt(i);
if (quote != 0) {
if (c == quote) {
if (i + 1 < text.length() && text.charAt(i + 1) == quote) {
i++;
} else {
quote = 0;
}
}
} else if (c == '\'' || c == '"') {
quote = c;
} else if (c == '(') {
depth++;
} else if (c == ')' && --depth == 0) {
return i;
}
}
return -1;
}
private static List<ExternalStatement.ColumnDefinition> columns(String text) {
List<ExternalStatement.ColumnDefinition> result = new ArrayList<>();
for (String item : split(text)) {
String definition = item.trim();
int nameEnd = columnNameEnd(definition);
if (nameEnd < 0 || nameEnd == definition.length() || definition.substring(nameEnd).isBlank()) {
throw new SpectrumSqlException("0A000", "malformed column definition");
}
result.add(new ExternalStatement.ColumnDefinition(identifier(definition.substring(0, nameEnd)),
GlueTypeMapper.canonicalGlueType(definition.substring(nameEnd).trim())));
}
return result;
}
private static int columnNameEnd(String definition) {
if (!definition.startsWith("\"")) {
for (int index = 0; index < definition.length(); index++) {
if (Character.isWhitespace(definition.charAt(index))) {
return index;
}
}
return -1;
}
for (int index = 1; index < definition.length(); index++) {
if (definition.charAt(index) == '"') {
if (index + 1 < definition.length() && definition.charAt(index + 1) == '"') {
index++;
} else {
return index + 1;
}
}
}
return -1;
}
private static List<String> split(String text) {
List<String> result = new ArrayList<>();
int depth = 0;
int start = 0;
char quote = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (quote != 0) {
if (c == quote) {
if (i + 1 < text.length() && text.charAt(i + 1) == quote) {
i++;
} else {
quote = 0;
}
}
} else if (c == '\'' || c == '"') {
quote = c;
} else if (c == '(' || c == '<') {
depth++;
} else if (c == ')' || c == '>') {
depth--;
} else if (c == ',' && depth == 0) {
result.add(text.substring(start, i));
start = i + 1;
}
}
result.add(text.substring(start));
return result;
}
private static String value(String text, Pattern pattern) {
Matcher matcher = pattern.matcher(text);
return matcher.find() ? matcher.group(1).replace("''", "'") : null;
}
private static String keywordValue(String text, Pattern pattern) {
Matcher matcher = pattern.matcher(text);
return matcher.find() ? matcher.group(1) : null;
}
private static Map<String, String> serdeProperties(String text) {
Map<String, String> result = new LinkedHashMap<>();
Matcher matcher = SERDE_PROPERTIES.matcher(text);
if (matcher.find()) {
int open = matcher.end() - 1;
int close = matchingParen(text, open);
if (close < 0) {
throw new SpectrumSqlException("0A000", "malformed SERDEPROPERTIES clause");
}
result.putAll(quotedPairs(text.substring(open + 1, close)));
}
return result;
}
private static Map<String, String> properties(String text) {
Map<String, String> result = new LinkedHashMap<>();
Matcher matcher = TABLE_PROPERTIES.matcher(text);
if (matcher.find()) {
int open = matcher.end() - 1;
int close = matchingParen(text, open);
if (close < 0) {
throw new SpectrumSqlException("0A000", "malformed TABLE PROPERTIES clause");
}
result.putAll(quotedPairs(text.substring(open + 1, close)));
}
return result;
}
private static Map<String, String> quotedPairs(String text) {
Map<String, String> result = new LinkedHashMap<>();
Matcher pair = QUOTED_PAIR.matcher(text);
while (pair.find()) {
result.put(pair.group(1).replace("''", "'"), pair.group(2).replace("''", "'"));
}
return result;
}
private static List<ExternalStatement.PartitionSpec> parsePartitions(String text) {
List<ExternalStatement.PartitionSpec> result = new ArrayList<>();
Matcher head = PARTITION_HEAD.matcher(text);
int end = 0;
while (head.find(end)) {
if (!text.substring(end, head.start()).isBlank()) {
return List.of();
}
int open = head.end() - 1;
int close = matchingParen(text, open);
if (close < 0) {
return List.of();
}
Matcher location = PARTITION_LOCATION.matcher(text).region(close + 1, text.length());
if (!location.lookingAt()) {
return List.of();
}
Map<String, String> values = new LinkedHashMap<>();
Matcher value = PARTITION_VALUE.matcher(text.substring(open + 1, close).trim() + ",");
while (value.find()) {
values.put(identifier(value.group(1)), value.group(2).replace("''", "'"));
}
if (values.isEmpty()) {
return List.of();
}
result.add(new ExternalStatement.PartitionSpec(values, location.group(1).replace("''", "'")));
end = location.end();
}
return end == text.length() || text.substring(end).isBlank() ? result : List.of();
}
private static String identifier(String value) {
String trimmed = value.trim();
if (trimmed.startsWith("\"") && trimmed.endsWith("\"")) {
return trimmed.substring(1, trimmed.length() - 1).replace("\"\"", "\"");
}
return trimmed.toLowerCase(Locale.ROOT);
}
private static String unquote(String value) {
String trimmed = value.trim();
return trimmed.startsWith("'") && trimmed.endsWith("'")
? trimmed.substring(1, trimmed.length() - 1).replace("''", "'") : trimmed;
}
}
@@ -0,0 +1,115 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import io.github.hectorvent.floci.services.glue.model.Column;
import io.github.hectorvent.floci.services.glue.model.Partition;
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
import io.github.hectorvent.floci.services.glue.model.Table;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public final class GlueTableBuilder {
private static final String TEXT_INPUT_FORMAT = "org.apache.hadoop.mapred.TextInputFormat";
private static final String TEXT_OUTPUT_FORMAT = "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat";
private static final String LAZY_SIMPLE_SERDE = "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe";
private static final String OPEN_CSV_SERDE = "org.apache.hadoop.hive.serde2.OpenCSVSerde";
private GlueTableBuilder() {
}
public static Table toGlueTable(ExternalStatement.CreateTable statement) {
StorageDescriptor descriptor = new StorageDescriptor();
descriptor.setLocation(statement.location());
descriptor.setColumns(columns(statement.columns()));
StorageDescriptor.SerDeInfo serde = new StorageDescriptor.SerDeInfo();
Map<String, String> serdeParameters = new LinkedHashMap<>();
switch (statement.format()) {
case PARQUET -> {
descriptor.setInputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat");
descriptor.setOutputFormat("org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat");
serde.setSerializationLibrary("org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe");
}
case JSON -> {
descriptor.setInputFormat(TEXT_INPUT_FORMAT);
descriptor.setOutputFormat(TEXT_OUTPUT_FORMAT);
serde.setSerializationLibrary(statement.serde() == null ? "org.openx.data.jsonserde.JsonSerDe" : statement.serde());
}
case TEXTFILE -> {
descriptor.setInputFormat(TEXT_INPUT_FORMAT);
descriptor.setOutputFormat(TEXT_OUTPUT_FORMAT);
if (isOpenCsv(statement.serde())) {
// OpenCSVSerde takes its delimiter from separatorChar, which the SERDEPROPERTIES supply
serde.setSerializationLibrary(OPEN_CSV_SERDE);
} else {
serde.setSerializationLibrary(LAZY_SIMPLE_SERDE);
String delimiter = statement.delimiter() == null ? "," : statement.delimiter();
serdeParameters.put("field.delim", delimiter);
serdeParameters.put("serialization.format", delimiter);
}
}
}
serdeParameters.putAll(statement.serdeProperties());
serde.setParameters(serdeParameters);
descriptor.setSerdeInfo(serde);
Table table = new Table();
table.setName(statement.tableName());
table.setTableType("EXTERNAL_TABLE");
table.setStorageDescriptor(descriptor);
table.setPartitionKeys(columns(statement.partitionColumns()));
Map<String, String> parameters = new LinkedHashMap<>(statement.properties());
parameters.put("EXTERNAL", "TRUE");
table.setParameters(parameters);
return table;
}
public static Partition toGluePartition(Table table, ExternalStatement.PartitionSpec spec) {
if (spec.location() == null || !spec.location().startsWith("s3://")) {
throw new SpectrumSqlException("22023", "partition location must be an s3:// URI");
}
List<String> values = new ArrayList<>();
for (Column key : table.getPartitionKeys()) {
String value = spec.values().entrySet().stream()
.filter(entry -> entry.getKey().equalsIgnoreCase(key.getName()))
.map(Map.Entry::getValue).findFirst().orElse(null);
if (value == null) {
throw new SpectrumSqlException("22023", "partition key \"" + key.getName() + "\" is missing");
}
values.add(value);
}
for (String name : spec.values().keySet()) {
boolean declared = table.getPartitionKeys().stream().anyMatch(key -> key.getName().equalsIgnoreCase(name));
if (!declared) {
throw new SpectrumSqlException("22023", "\"" + name + "\" is not a partition key of the table");
}
}
StorageDescriptor source = table.getStorageDescriptor();
StorageDescriptor descriptor = new StorageDescriptor();
descriptor.setColumns(source.getColumns());
descriptor.setInputFormat(source.getInputFormat());
descriptor.setOutputFormat(source.getOutputFormat());
descriptor.setSerdeInfo(source.getSerdeInfo());
descriptor.setLocation(spec.location());
Partition partition = new Partition();
partition.setValues(values);
partition.setStorageDescriptor(descriptor);
return partition;
}
private static boolean isOpenCsv(String serde) {
return serde != null && serde.toLowerCase(Locale.ROOT).contains("opencsvserde");
}
private static List<Column> columns(List<ExternalStatement.ColumnDefinition> definitions) {
List<Column> result = new ArrayList<>();
for (ExternalStatement.ColumnDefinition definition : definitions) {
Column column = new Column();
column.setName(definition.name());
column.setType(definition.type().toLowerCase(Locale.ROOT));
result.add(column);
}
return result;
}
}
@@ -0,0 +1,111 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Maps Glue column types to PostgreSQL types and DuckDB text projections. */
public final class GlueTypeMapper {
private static final Pattern SIZED_STRING = Pattern.compile("(?:varchar|char)\\((\\d+)\\)");
private static final Pattern DECIMAL = Pattern.compile("(?:decimal|numeric)(?:\\((\\d+)(?:,(\\d+))?\\))?");
private static final String NULL_MARKER = "'\\N'";
private GlueTypeMapper() {
}
/** Rewrites Redshift type aliases to the Hive type names that Glue stores; other types pass through lowercased. */
public static String canonicalGlueType(String type) {
String normalized = normalize(type);
if (isTypeOrSized(normalized, "charactervarying")) {
String length = normalized.substring("charactervarying".length());
return "varchar" + (length.isEmpty() ? "(256)" : length);
}
if (isTypeOrSized(normalized, "character")) {
String length = normalized.substring("character".length());
return "char" + (length.isEmpty() ? "(1)" : length);
}
if (isTypeOrSized(normalized, "numeric")) {
return "decimal" + normalized.substring("numeric".length());
}
if (isBinary(normalized)) {
return "binary";
}
return switch (normalized) {
case "varchar" -> "varchar(256)";
case "char" -> "char(1)";
case "doubleprecision", "float8" -> "double";
case "real", "float4" -> "float";
case "int2" -> "smallint";
case "int4" -> "int";
case "int8" -> "bigint";
case "bool" -> "boolean";
default -> normalized;
};
}
public static String toPostgres(String glueType) {
String type = canonicalGlueType(glueType);
Matcher sized = SIZED_STRING.matcher(type);
if (sized.matches()) {
return "varchar(" + sized.group(1) + ")";
}
Matcher decimal = DECIMAL.matcher(type);
if (decimal.matches()) {
if (decimal.group(1) == null) {
return "numeric";
}
return "numeric(" + decimal.group(1) + "," + (decimal.group(2) == null ? "0" : decimal.group(2)) + ")";
}
if (isNested(type)) {
return "jsonb";
}
return switch (type) {
case "string", "varchar", "char" -> "text";
case "tinyint", "smallint" -> "smallint";
case "int", "integer" -> "integer";
case "bigint" -> "bigint";
case "float" -> "real";
case "double" -> "double precision";
case "boolean" -> "boolean";
case "date" -> "date";
case "timestamp" -> "timestamp";
case "binary" -> "bytea";
default -> throw new SpectrumSqlException("0A000", "Unsupported external column type: " + glueType);
};
}
public static boolean isNested(String glueType) {
String type = normalize(glueType);
return type.startsWith("array<") || type.startsWith("map<") || type.startsWith("struct<");
}
public static String duckProjection(String columnName, String glueType) {
String column = quote(columnName);
String type = canonicalGlueType(glueType);
String expression = isNested(type) ? "to_json(" + column + ")"
: "binary".equals(type) ? "'\\x' || hex(" + column + ")" : column;
return "COALESCE(CAST(" + expression + " AS VARCHAR), " + NULL_MARKER + ") AS " + column;
}
private static boolean isTypeOrSized(String type, String base) {
return type.equals(base) || type.startsWith(base + "(");
}
private static boolean isBinary(String type) {
return isTypeOrSized(type, "varbyte")
|| isTypeOrSized(type, "varbinary")
|| isTypeOrSized(type, "binaryvarying")
|| isTypeOrSized(type, "binary");
}
private static String normalize(String glueType) {
if (glueType == null) {
throw new SpectrumSqlException("0A000", "External column type is missing");
}
return glueType.trim().toLowerCase(Locale.ROOT).replace(" ", "");
}
private static String quote(String identifier) {
return "\"" + identifier.replace("\"", "\"\"") + "\"";
}
}
@@ -0,0 +1,173 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import io.github.hectorvent.floci.services.redshift.proxy.PostgresWireDecoder;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public final class PostgresBackendSession implements BackendSql {
private static final String SQLSTATE_IO = "58030";
private final Socket backend;
public PostgresBackendSession(Socket backend) {
this.backend = backend;
}
@Override
public void execute(String sql) {
try {
OutputStream output = backend.getOutputStream();
output.write(PostgresWireDecoder.encodeQuery(sql));
output.flush();
awaitReady(new PostgresWireDecoder(backend.getInputStream()));
} catch (IOException exception) {
throw new SpectrumReadException(SQLSTATE_IO, "Unable to run a statement on the Redshift backend", exception);
}
}
@Override
public long copyIn(String copySql, InputStream data) {
try {
OutputStream output = backend.getOutputStream();
output.write(PostgresWireDecoder.encodeQuery(copySql));
output.flush();
PostgresWireDecoder decoder = new PostgresWireDecoder(backend.getInputStream());
awaitCopyIn(decoder);
byte[] chunk = new byte[8192];
int read = readChunk(output, decoder, data, chunk);
while (read > 0) {
output.write('d');
writeInt32(output, read + 4);
output.write(chunk, 0, read);
read = readChunk(output, decoder, data, chunk);
}
output.write('c');
writeInt32(output, 4);
output.flush();
return rowCount(awaitReady(decoder));
} catch (IOException exception) {
throw new SpectrumReadException(SQLSTATE_IO, "Unable to load external table data", exception);
}
}
/** A failure reading the source aborts the COPY on the backend; a failure writing to the backend socket does not. */
private static int readChunk(OutputStream output, PostgresWireDecoder decoder, InputStream data, byte[] chunk)
throws IOException {
try {
int read = data.read(chunk);
if (read != 0) {
return read;
}
int nextByte = data.read();
if (nextByte < 0) {
return -1;
}
chunk[0] = (byte) nextByte;
return 1;
} catch (IOException exception) {
writeCopyFail(output, exception.getMessage());
try {
awaitReady(decoder);
} catch (IOException | SpectrumReadException backendFailure) {
exception.addSuppressed(backendFailure);
}
throw exception;
}
}
private static void awaitCopyIn(PostgresWireDecoder decoder) throws IOException {
SpectrumReadException failure = null;
PostgresWireDecoder.FrontendMessage message = decoder.nextMessage();
while (message != null) {
if (message.type() == 'G') {
if (failure != null) {
throw failure;
}
return;
}
if (message.type() == 'E') {
failure = toException(message.body());
} else if (message.type() == 'Z') {
if (failure != null) {
throw failure;
}
throw new IOException("PostgreSQL did not enter CopyIn mode");
}
message = decoder.nextMessage();
}
throw new IOException("PostgreSQL closed before CopyInResponse");
}
private static String awaitReady(PostgresWireDecoder decoder) throws IOException {
SpectrumReadException failure = null;
String tag = "";
PostgresWireDecoder.FrontendMessage message = decoder.nextMessage();
while (message != null) {
if (message.type() == 'C') {
tag = new String(message.body(), StandardCharsets.UTF_8).trim();
} else if (message.type() == 'E') {
failure = toException(message.body());
} else if (message.type() == 'Z') {
if (failure != null) {
throw failure;
}
return tag;
}
message = decoder.nextMessage();
}
throw new IOException("PostgreSQL closed before ReadyForQuery");
}
private static long rowCount(String tag) {
int space = tag.lastIndexOf(' ');
if (space < 0) {
return 0;
}
try {
return Long.parseLong(tag.substring(space + 1).trim());
} catch (NumberFormatException exception) {
throw new SpectrumReadException(SQLSTATE_IO, "Unexpected COPY command tag: " + tag, exception);
}
}
private static SpectrumReadException toException(byte[] body) {
String state = SQLSTATE_IO;
String message = "PostgreSQL rejected the statement";
int offset = 0;
while (body != null && offset < body.length && body[offset] != 0) {
char code = (char) body[offset++];
int end = offset;
while (end < body.length && body[end] != 0) {
end++;
}
String value = new String(body, offset, end - offset, StandardCharsets.UTF_8);
if (code == 'C') {
state = value;
} else if (code == 'M') {
message = value;
}
offset = end + 1;
}
return new SpectrumReadException(state, message);
}
private static void writeInt32(OutputStream output, int value) throws IOException {
output.write((value >>> 24) & 0xff);
output.write((value >>> 16) & 0xff);
output.write((value >>> 8) & 0xff);
output.write(value & 0xff);
}
private static void writeCopyFail(OutputStream output, String message) throws IOException {
byte[] detail = (message == null ? "Unable to stream COPY input" : message)
.getBytes(StandardCharsets.UTF_8);
output.write('f');
writeInt32(output, detail.length + 5);
output.write(detail);
output.write(0);
output.flush();
}
}
@@ -50,7 +50,7 @@ public record SpectrumColumn(String name, Type type) {
public String postgresType() {
return switch (this) {
case VARCHAR -> "VARCHAR";
case VARCHAR -> "VARCHAR(256)";
case CHAR -> "CHAR";
case INTEGER -> "INTEGER";
case BIGINT -> "BIGINT";
@@ -21,6 +21,7 @@ public final class SpectrumMaterializer {
private static final SecureRandom RANDOM = new SecureRandom();
private static final String SQLSTATE_DATA = "22000";
private static final int DEFAULT_VARCHAR_MAX_BYTES = 256;
public Materialization materialize(Socket backend, SpectrumExternalTable table,
SpectrumExternalSchema schema, SpectrumS3Reader reader) {
@@ -44,7 +45,7 @@ public final class SpectrumMaterializer {
try (Stream<SpectrumRow> rows = reader.read(schema, table)) {
Iterator<SpectrumRow> iterator = rows.iterator();
while (iterator.hasNext()) {
writeCopyData(output, encodeRow(iterator.next()));
writeCopyData(output, encodeRow(iterator.next(), table.columns()));
}
}
writeCopyDone(output);
@@ -96,13 +97,18 @@ public final class SpectrumMaterializer {
+ ") FROM STDIN";
}
private static byte[] encodeRow(SpectrumRow row) {
private static byte[] encodeRow(SpectrumRow row, List<SpectrumColumn> columns) {
StringBuilder line = new StringBuilder();
for (int i = 0; i < row.values().size(); i++) {
if (i > 0) {
line.append('\t');
}
String value = row.values().get(i);
if (value != null
&& columns.get(i).type() == SpectrumColumn.Type.VARCHAR
&& value.getBytes(StandardCharsets.UTF_8).length > DEFAULT_VARCHAR_MAX_BYTES) {
value = null;
}
if (value == null) {
line.append("\\N");
} else {
@@ -0,0 +1,10 @@
package io.github.hectorvent.floci.services.redshift.spectrum;
import java.util.List;
public record SpectrumSession(String accountId, String clusterKey, String databaseName,
List<String> iamRoleArns, boolean inTransaction) {
public SpectrumSession {
iamRoleArns = iamRoleArns == null ? List.of() : List.copyOf(iamRoleArns);
}
}