From bcca1bb8d32daab2f2baef93987daf2315d12a2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C5=A9=20Th=E1=BA=BF=20Huy?=
<31652864+vuthehuyht@users.noreply.github.com>
Date: Mon, 16 Nov 2026 07:01:10 +0700
Subject: [PATCH] feat(redshift): add external schemas over Kinesis Data
Streams (#8457)
---
docs/services/redshift.md | 30 ++
.../spectrum/ExternalCatalogRegistry.java | 73 +----
.../spectrum/ExternalMetadataWriter.java | 22 ++
.../spectrum/ExternalSchemaBinding.java | 3 +-
.../redshift/spectrum/ExternalStatement.java | 8 +-
.../spectrum/ExternalStatementParser.java | 32 +-
.../spectrum/KinesisSchemaBinding.java | 27 ++
.../spectrum/KinesisSchemaRegistry.java | 20 ++
.../redshift/spectrum/SchemaBinding.java | 12 +
.../spectrum/SchemaBindingRegistry.java | 82 +++++
.../spectrum/SpectrumQueryPreparation.java | 169 ++++++++--
.../resources/redshift/bootstrap-catalog.sql | 2 +-
.../RedshiftKinesisSchemaIntegrationTest.java | 117 +++++++
.../spectrum/ExternalMetadataWriterTest.java | 21 ++
.../spectrum/ExternalStatementParserTest.java | 41 +++
.../spectrum/KinesisSchemaRegistryTest.java | 121 +++++++
.../SpectrumKinesisSchemaPreparationTest.java | 297 ++++++++++++++++++
17 files changed, 966 insertions(+), 111 deletions(-)
create mode 100644 src/main/java/io/floci/aws/services/redshift/spectrum/KinesisSchemaBinding.java
create mode 100644 src/main/java/io/floci/aws/services/redshift/spectrum/KinesisSchemaRegistry.java
create mode 100644 src/main/java/io/floci/aws/services/redshift/spectrum/SchemaBinding.java
create mode 100644 src/main/java/io/floci/aws/services/redshift/spectrum/SchemaBindingRegistry.java
create mode 100644 src/test/java/io/floci/aws/services/redshift/RedshiftKinesisSchemaIntegrationTest.java
create mode 100644 src/test/java/io/floci/aws/services/redshift/spectrum/KinesisSchemaRegistryTest.java
create mode 100644 src/test/java/io/floci/aws/services/redshift/spectrum/SpectrumKinesisSchemaPreparationTest.java
diff --git a/docs/services/redshift.md b/docs/services/redshift.md
index ba649bb0c1..a25090a475 100644
--- a/docs/services/redshift.md
+++ b/docs/services/redshift.md
@@ -1007,6 +1007,36 @@ Glue and PostgreSQL updates do not form a distributed transaction: an infrastruc
one store changes can require retrying the command. The legacy Phase 1 catalog does not gain these
lifecycle operations.
+### External schemas over Kinesis Data Streams
+
+Streaming ingestion from Kinesis Data Streams starts with an external schema that maps to the streaming
+source. This part adds that schema; the streaming materialized view and `REFRESH` that read from it are
+not implemented yet.
+
+```sql
+CREATE EXTERNAL SCHEMA [IF NOT EXISTS] kds
+FROM KINESIS
+[REGION 'us-west-2']
+IAM_ROLE 'arn:aws:iam::123456789012:role/StreamRole';
+```
+
+`CREATE EXTERNAL SCHEMA` creates a local PostgreSQL schema, remembers the role and the optional Region, and
+lists the schema in `svv_external_schemas` with `eskind` 9 and the role in `esoptions`. Without `REGION` the
+schema uses the cluster's own Region. `DROP SCHEMA [IF EXISTS] kds [CASCADE | RESTRICT]` removes the local
+schema, its metadata row and its registration; `RESTRICT` is the default, so a schema that holds objects is
+kept, as in Redshift.
+
+- `IAM_ROLE DEFAULT` is not supported. Name the role ARN in a single-quoted literal.
+- The role is not checked when the schema is created. Nothing reads the stream yet, so no Kinesis permission
+ is evaluated.
+- A statement that names a relation of the schema as a source (`SELECT * FROM kds.my_stream`,
+ `INSERT INTO kds.t`, `UPDATE kds.t`) is refused with SQLSTATE `0A000`. Redshift reads a stream through a
+ streaming materialized view, and keeps no user tables in such a schema.
+- `CREATE EXTERNAL SCHEMA` inside an explicit transaction fails with SQLSTATE `0A000`, and `DROP SCHEMA`
+ with `25001`, because the schema registration is not part of the PostgreSQL transaction.
+- `DROP EXTERNAL DATABASE` and dropping several schemas in one statement are outside this implementation.
+- Schemas over Amazon MSK or Kafka are not supported.
+
### COPY from S3
`COPY
[()] FROM 's3:///' [options]` sent over the Simple
diff --git a/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalCatalogRegistry.java b/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalCatalogRegistry.java
index c47e1579cd..a315230ebf 100644
--- a/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalCatalogRegistry.java
+++ b/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalCatalogRegistry.java
@@ -1,80 +1,13 @@
package io.floci.aws.services.redshift.spectrum;
import com.fasterxml.jackson.core.type.TypeReference;
-import io.floci.aws.core.common.Resettable;
-import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.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 bindings;
-
+public class ExternalCatalogRegistry extends SchemaBindingRegistry {
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 find(String accountId, String clusterKey, String databaseName, String schemaName) {
- return bindings.getForAccount(accountId, key(clusterKey, databaseName, schemaName));
- }
-
- public List 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);
+ super(storageFactory.create("redshift", "redshift-external-schemas.json",
+ new TypeReference<>() { }));
}
}
diff --git a/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalMetadataWriter.java b/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalMetadataWriter.java
index 511ddcd813..bec92dcd86 100644
--- a/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalMetadataWriter.java
+++ b/src/main/java/io/floci/aws/services/redshift/spectrum/ExternalMetadataWriter.java
@@ -21,6 +21,8 @@ import java.util.Map;
@ApplicationScoped
public class ExternalMetadataWriter {
private static final Logger LOG = Logger.getLogger(ExternalMetadataWriter.class);
+ /** The {@code eskind} that SVV_EXTERNAL_SCHEMAS gives a schema over an Amazon Kinesis data stream. */
+ private static final int KINESIS_SCHEMA_KIND = 9;
private final GlueService glueService;
private final ObjectMapper objectMapper;
@@ -39,6 +41,10 @@ public class ExternalMetadataWriter {
run(backend, refreshSql(binding, tables, partitions), false);
}
+ public void refreshKinesis(BackendSql backend, KinesisSchemaBinding binding) {
+ run(backend, refreshKinesisSql(binding), false);
+ }
+
public void purge(BackendSql backend, String schemaName) {
run(backend, purgeSql(schemaName), true);
}
@@ -59,6 +65,22 @@ public class ExternalMetadataWriter {
return "SELECT floci_internal.purge_external_schema(" + literal(schemaName) + ")";
}
+ /**
+ * A schema over a Kinesis stream lists as kind 9 with no tables. Its stream is read only through a
+ * materialized view, and Redshift documents no external database name for it, so that column stays empty.
+ */
+ String refreshKinesisSql(KinesisSchemaBinding binding) {
+ Map payload = row(
+ "eskind", KINESIS_SCHEMA_KIND,
+ "databasename", "",
+ "esoptions", json(Map.of("IAM_ROLE", binding.iamRoleArn())),
+ "tables", List.of(),
+ "columns", List.of(),
+ "partitions", List.of());
+ return "SELECT floci_internal.refresh_external_catalog(" + literal(binding.schemaName()) + ", "
+ + literal(json(payload)) + "::jsonb)";
+ }
+
String refreshSql(ExternalSchemaBinding binding, List tables,
Map> partitionsByTable) {
List