feat(redshift): emulate Redshift UNLOAD TO s3 over Simple Query protocol (#3129)

* feat(redshift): parse UNLOAD TO s3 statements

* feat(redshift): add S3Service.authorizeAnonymousPutObject for the UNLOAD writer

* feat(redshift): simulate Redshift UNLOAD TO s3 via CopyOut

* feat(redshift): dispatch UNLOAD via the intercepting bridge

* test(redshift): cover UNLOAD to S3 end to end

* docs(redshift): document UNLOAD to S3 support

* fix(redshift): drain backend on UNLOAD streaming failure and handle final slice error

* refactor(redshift): clean up inline FQCN imports across main and test sources

* fix(redshift): address review feedback on UNLOAD to S3 simulator

* fix(redshift): authorize ListBucket on UNLOAD prefix probe and clean up manifest on backend EOF

* fix(redshift): authorize UNLOAD cleanup deletes and skip them under ALLOWOVERWRITE

* fix(redshift): fail open on UNLOAD MAXFILESIZE above the buffering ceiling

* feat(redshift): repeat the UNLOAD header row at the start of every slice

* test(redshift): cover UNLOAD to S3 over JDBC in the SDK compatibility suite

* docs(redshift): document UNLOAD per-file header, MAXFILESIZE bytes, and over-cap fail-open

* fix(redshift): clean up new UNLOAD slice objects even under ALLOWOVERWRITE

deleteWritten previously bailed entirely when ALLOWOVERWRITE was set, to avoid
destroying a pre-existing object a slice key had overwritten. That also left
genuinely new slice keys behind on a MANIFEST failure, exposing a partial result
with no manifest. Track which slice keys existed before the write and skip only
those on cleanup.

* fix(redshift): never delete UNLOAD output under ALLOWOVERWRITE

The preExistingKeys heuristic could not distinguish an overwrite from a fresh
write atomically: two concurrent UNLOADs to the same prefix could both classify a
key as new, and one operation's cleanup could then delete the other's output.
Drop the heuristic and simply never delete under ALLOWOVERWRITE. Non-overwrite
runs verified the prefix was empty, so their cleanup is still safe. A failed
MANIFEST + ALLOWOVERWRITE UNLOAD may leave data objects without a manifest, which
is now documented.

* fix(redshift): read UNLOAD objects via getObject in the integration test

listObjects returns metadata-only S3Object entries whose getData() is null, so
unloadToS3PrefixWritesRowsAsObjects and unloadGzipWritesCompressedObject threw
NullPointerException in CI. Fetch each body with getObject, as the MANIFEST test
already does.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-06 15:58:37 -05:00
1 parent ee2f55b784
commit 4570bf62c5
17 files changed
+1853 -69

No files matched your search

@@ -13,6 +13,16 @@ import software.amazon.awssdk.services.redshift.model.DescribeClustersRequest;
import software.amazon.awssdk.services.redshift.model.DescribeClustersResponse;
import software.amazon.awssdk.services.redshift.model.DeleteClusterRequest;
import software.amazon.awssdk.services.redshift.model.DeleteClusterResponse;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -59,6 +69,45 @@ public class RedshiftTest {
@Test
@Order(2)
public void testUnloadOverSimpleQueryWritesToS3() throws Exception {
RedshiftClient client = getClient();
Cluster cluster = client.describeClusters(DescribeClustersRequest.builder()
.clusterIdentifier("test-cluster")
.build()).clusters().get(0);
String jdbcUrl = "jdbc:postgresql://" + cluster.endpoint().address() + ":"
+ cluster.endpoint().port() + "/dev?preferQueryMode=simple";
S3Client s3 = TestFixtures.s3Client();
String bucket = "redshift-unload-compat";
s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
try (Connection conn = DriverManager.getConnection(jdbcUrl, "admin", "Password123");
Statement st = conn.createStatement()) {
st.execute("DROP TABLE IF EXISTS unload_src");
st.execute("CREATE TABLE unload_src (id int, name text)");
st.execute("INSERT INTO unload_src VALUES (1, 'alice'), (2, 'bob')");
st.execute("UNLOAD ('select id, name from unload_src order by id') "
+ "TO 's3://redshift-unload-compat/out/'");
}
ListObjectsV2Response listing = s3.listObjectsV2(ListObjectsV2Request.builder()
.bucket(bucket)
.prefix("out/")
.build());
assertTrue(listing.keyCount() >= 1, "UNLOAD must write at least one object");
StringBuilder all = new StringBuilder();
listing.contents().stream()
.sorted((a, b) -> a.key().compareTo(b.key()))
.forEach(obj -> all.append(new String(
s3.getObjectAsBytes(GetObjectRequest.builder().bucket(bucket).key(obj.key()).build())
.asByteArray(),
StandardCharsets.UTF_8)));
assertEquals("1|alice\n2|bob\n", all.toString());
}
@Test
@Order(3)
public void testDeleteCluster() {
RedshiftClient client = getClient();
DeleteClusterResponse res = client.deleteCluster(DeleteClusterRequest.builder()