mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
feat(redshift): materialize Glue external tables into PostgreSQL (#4996)
* feat(redshift): materialize Glue external tables into PostgreSQL Add ExternalTableMaterializer, which loads a Glue-backed external table into the cluster's PostgreSQL through a staging table, plus SpectrumCatalogResolver (Glue binding first, phase-one catalog as a fallback) and SpectrumGlueCsvAdapter (simple CSV tables keep the streaming path and need no floci-duck sidecar). The bound IAM role is enforced for every S3 object read, including Iceberg metadata, manifests and data files, before DuckDB sees them. SpectrumS3Reader now reads through the role as well, so a phase-one external schema needs a real role associated with the cluster. Spectrum semantics: hidden files and folder markers are ignored, a partitioned table without registered partitions returns no rows, and a sized string value wider than its column in bytes becomes NULL. Also: PartitionProjection accepts IN lists for injected columns and checks each table alias, S3Service hides the Spectrum scratch bucket from ListBuckets, S3TransferException becomes public, and FLOCI_SERVICES_REDSHIFT_SPECTRUM_MAX_ROWS is added. * fix(redshift): harden external table loads against review findings - A session outside a transaction waits for a slow load instead of failing after 30 seconds; only a session in a transaction keeps the timeout, since it can hold a PostgreSQL lock the Java lock cannot see. - A missing S3 bucket is reported as XX000, not as access denied. - A load failure goes to the log with its cause and returns a generic message, because the cause can carry DuckDB request text. - The partition fingerprint includes input format and SerDe, so a format change at the same location reloads the table. - A partition value wider than its sized string key becomes NULL, as a data column does, instead of failing the whole load. - The first load of an account removes scratch objects left by a JVM that died mid-load, keeping those younger than an hour. - Document why the phase-one reader enforces the role without the cluster association check. * fix(redshift): use a real IAM role in the Spectrum compat test The external schema now enforces its IAM role, so the compat test needs a role that exists, trusts redshift.amazonaws.com and is associated with the cluster. Create it through the IAM SDK client, pass it to CreateCluster and delete it in the cleanup. * fix(redshift): address review findings on the external table materializer - PartitionProjection: a table name must end at an identifier boundary, comma separated sources are checked like FROM and JOIN sources, an unqualified injected column is accepted when exactly one source declares it, and a constant may be wrapped in parentheses. - The partition fingerprint covers the whole descriptor (columns, formats, SerDe and parameters), and an Iceberg table is read on every query because its snapshot can change without any visible change to the Glue table. - A transactional load that keeps the committed columns keeps the schema signature, so a view that depends on the table can still be refilled. - Files under a hidden folder such as _temporary are ignored, not only hidden file names. - The scratch bucket is told apart from a user bucket of the same name by a tag: a user bucket is listed, never written to, and never swept. - Drop the SPECTRUM_MAX_ROWS row from the docs until routing makes the cap reachable. * fix(redshift): verify slow-load wait with timeout
This commit is contained in:
1 parent
2c544fdb9f
commit
06ceec2aee
25 files changed
+3178
-82
No files matched your search
+31
-2
@@ -4,6 +4,11 @@ import com.floci.test.TestFixtures;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.CreateRoleRequest;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteRolePolicyRequest;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteRoleRequest;
|
||||
import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest;
|
||||
import software.amazon.awssdk.services.redshift.RedshiftClient;
|
||||
import software.amazon.awssdk.services.redshift.model.Cluster;
|
||||
import software.amazon.awssdk.services.redshift.model.CreateClusterRequest;
|
||||
@@ -29,18 +34,33 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RedshiftSpectrumTest {
|
||||
|
||||
private static final String ROLE_PREFIX = "SpectrumCompatRole";
|
||||
|
||||
@Test
|
||||
@DisplayName("queries an S3 CSV external table through the Redshift JDBC wire")
|
||||
void queriesCsvExternalTable() throws Exception {
|
||||
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
|
||||
String clusterId = "spectrum-" + suffix;
|
||||
String bucket = "spectrum-" + suffix;
|
||||
String roleName = ROLE_PREFIX + suffix;
|
||||
String roleArn = "arn:aws:iam::000000000000:role/" + roleName;
|
||||
RedshiftClient redshift = TestFixtures.redshiftClient();
|
||||
S3Client s3 = TestFixtures.s3Client();
|
||||
IamClient iam = TestFixtures.iamClient();
|
||||
boolean bucketCreated = false;
|
||||
boolean clusterCreated = false;
|
||||
|
||||
try {
|
||||
iam.createRole(CreateRoleRequest.builder().roleName(roleName).path("/")
|
||||
.assumeRolePolicyDocument("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"redshift.amazonaws.com"},"Action":"sts:AssumeRole"}]}""")
|
||||
.build());
|
||||
iam.putRolePolicy(PutRolePolicyRequest.builder().roleName(roleName).policyName("SpectrumS3")
|
||||
.policyDocument("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":["s3:GetObject","s3:ListBucket"],"Resource":"*"}]}""")
|
||||
.build());
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
|
||||
bucketCreated = true;
|
||||
s3.putObject(PutObjectRequest.builder().bucket(bucket).key("events/part-1.csv")
|
||||
@@ -48,7 +68,7 @@ class RedshiftSpectrumTest {
|
||||
RequestBody.fromString("id,name\n1,Alice\n2,Bob\n"));
|
||||
redshift.createCluster(CreateClusterRequest.builder().clusterIdentifier(clusterId)
|
||||
.nodeType("dc2.large").masterUsername("admin").masterUserPassword("Password123")
|
||||
.build());
|
||||
.iamRoles(roleArn).build());
|
||||
clusterCreated = true;
|
||||
|
||||
Cluster cluster = waitForCluster(redshift, clusterId);
|
||||
@@ -56,7 +76,8 @@ class RedshiftSpectrumTest {
|
||||
+ cluster.endpoint().port() + "/dev";
|
||||
try (Connection connection = waitForConnection(jdbcUrl);
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE EXTERNAL SCHEMA analytics FROM DATA CATALOG DATABASE 'dev' IAM_ROLE 'role'");
|
||||
statement.execute("CREATE EXTERNAL SCHEMA analytics FROM DATA CATALOG DATABASE 'dev' IAM_ROLE '"
|
||||
+ roleArn + "'");
|
||||
statement.execute("CREATE EXTERNAL TABLE analytics.events (id INTEGER, name VARCHAR) "
|
||||
+ "STORED AS TEXTFILE LOCATION 's3://" + bucket + "/events/' "
|
||||
+ "TBLPROPERTIES ('skip.header.line.count'='1')");
|
||||
@@ -81,8 +102,16 @@ class RedshiftSpectrumTest {
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key("events/part-1.csv").build());
|
||||
s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());
|
||||
}
|
||||
try {
|
||||
iam.deleteRolePolicy(DeleteRolePolicyRequest.builder().roleName(roleName)
|
||||
.policyName("SpectrumS3").build());
|
||||
iam.deleteRole(DeleteRoleRequest.builder().roleName(roleName).build());
|
||||
} catch (RuntimeException ignored) {
|
||||
// Cleanup is best effort if role setup failed partway through.
|
||||
}
|
||||
redshift.close();
|
||||
s3.close();
|
||||
iam.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user