diff --git a/docs/services/redshift.md b/docs/services/redshift.md index 90e7424bc7..21cba1b547 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -766,7 +766,7 @@ order) through its own S3 service and streams the rows into the backing PostgreS compatible with output from `UNLOAD ... MANIFEST`. Missing files marked `mandatory: true` abort the load. When `mandatory` is omitted, it defaults to `false`. All entries in the manifest must reside in the same S3 bucket as the manifest file itself; cross-bucket manifest entries are rejected as an intentional deviation. -- `IGNOREHEADER` and `HEADER` skip lines from the first resolved object only. +- `IGNOREHEADER` and `HEADER` skip lines from the start of every resolved object, as AWS does for a parallel load of several files. - `GZIP` is the only input compression recognized; `BZIP2`, `LZOP` and `ZSTD` are not. - `IAM_ROLE ''` is supported. The role must be associated with the cluster (at `CreateCluster` or through `ModifyClusterIamRoles`; connections opened before a change keep the diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulator.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulator.java index 8e697d2a85..7fd5de1e1c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulator.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulator.java @@ -519,7 +519,8 @@ public final class S3CopySimulator { JsonLinesToCsvConverter.convert(in, targetCols, copyDataOut, spec.jsonAutoIgnoreCase()); copyDataOut.flush(); } else { - if (i == 0 && spec.headerLines() > 0) { + // Every object has its own header, as in a parallel load of several files. + if (spec.headerLines() > 0) { skipLines(in, spec.headerLines()); } InputStream source = in; diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulatorTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulatorTest.java index 70a686603b..f660444950 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/S3CopySimulatorTest.java @@ -1218,17 +1218,17 @@ class S3CopySimulatorTest { } @Test - void concatenatesPrefixObjectsInKeyOrderAndSkipsHeaderOnFirstOnly() throws Exception { + void concatenatesPrefixObjectsInKeyOrderAndSkipsTheHeaderOfEachObject() throws Exception { when(s3.objectExists("wh", "p/")).thenReturn(false); when(s3.listObjectsWithPrefixes(eq("wh"), eq("p/"), isNull(), anyInt(), any(), any())).thenReturn( new S3Service.ListObjectsResult(List.of( new S3Object("wh", "p/1", "h1|h2\n1|a\n".getBytes(StandardCharsets.US_ASCII), "text/plain"), - new S3Object("wh", "p/2", "2|b\n".getBytes(StandardCharsets.US_ASCII), "text/plain")), + new S3Object("wh", "p/2", "h1|h2\n2|b\n".getBytes(StandardCharsets.US_ASCII), "text/plain")), List.of(), false, null)); when(s3.getObject("wh", "p/1")).thenReturn( new S3Object("wh", "p/1", "h1|h2\n1|a\n".getBytes(StandardCharsets.US_ASCII), "text/plain")); when(s3.getObject("wh", "p/2")).thenReturn( - new S3Object("wh", "p/2", "2|b\n".getBytes(StandardCharsets.US_ASCII), "text/plain")); + new S3Object("wh", "p/2", "h1|h2\n2|b\n".getBytes(StandardCharsets.US_ASCII), "text/plain")); CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( "t", List.of(), "wh", "p/", "|", 1, false, false, null, null); @@ -1596,6 +1596,23 @@ class S3CopySimulatorTest { assertEquals("1|\\N\n2|\\N|x\n", copyDataPayload(out.toByteArray())); } + @Test + void streamCopyInput_skipsTheHeaderOfEveryObject() throws Exception { + CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( + "people", List.of(), "wh", "p/", "|", 1, false, false, null, null); + when(s3.getObject("wh", "p/a.txt")).thenReturn(new S3Object("wh", "p/a.txt", + "id|name\n1|a\n".getBytes(StandardCharsets.UTF_8), "text/plain")); + when(s3.getObject("wh", "p/b.txt")).thenReturn(new S3Object("wh", "p/b.txt", + "id|name\n2|b\n".getBytes(StandardCharsets.UTF_8), "text/plain")); + S3CopySimulator.CopyInput input = + new S3CopySimulator.CopyInput(spec, List.of("p/a.txt", "p/b.txt"), s3, null, null); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + S3CopySimulator.streamCopyInput(input, out); + + assertEquals("1|a\n2|b\n", copyDataPayload(out.toByteArray())); + } + @Test void streamCopyInput_transformsAfterGzipDecodeAndHeaderSkip() throws Exception { CopyStatementParser.CopyTransforms transforms =