feat(redshift): support MAXERROR in COPY (#7291)

* feat(redshift): support MAXERROR in COPY

MAXERROR n of 2 or more now asks the backend to skip records it cannot convert
(ON_ERROR ignore) and to fail the COPY at the n-th one (REJECT_LIMIT n - 1),
which needs a PostgreSQL 18 backend. A MAXERROR of 0 or 1 fails at the first
error, as without the option.

* test(redshift): add PostgreSQL 18 MAXERROR integration test

* test(redshift): use shared DockerAvailability and RedshiftConnections in PostgreSQL 18 IT

Address review feedback on PR #7291:
- Call testing.DockerAvailability.isAvailable() instead of carrying a private copy of the probe.
- Use RedshiftConnections.waitForConnection(cluster) instead of duplicating the JDBC connection wait helper.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-02 11:58:15 -04:00
1 parent 6a390496eb
commit d4eeada135
7 files changed
+270 -11

No files matched your search

@@ -0,0 +1,132 @@
package io.github.hectorvent.floci.services.redshift;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.testing.DockerAvailability;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@TestProfile(RedshiftMaxErrorPostgres18IntegrationTest.Postgres18Profile.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RedshiftMaxErrorPostgres18IntegrationTest {
public static class Postgres18Profile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.redshift.image-version", "postgres:18-alpine");
}
}
private static final String CLUSTER_ID = "it-maxerror-pg18";
private static final String BUCKET = "it-maxerror-pg18-bucket";
@Inject
RedshiftService service;
@Inject
RedshiftSnapshotScheduler snapshotScheduler;
@Inject
S3Service s3;
private Cluster cluster;
@BeforeAll
void createCluster() throws Exception {
Assumptions.assumeTrue(DockerAvailability.isAvailable(), "Docker daemon must be available for Redshift Docker integration tests");
cluster = service.createCluster(CLUSTER_ID, "dc2.large", "admin", "Secret123");
s3.createBucket(BUCKET, "us-east-1");
}
@AfterAll
void deleteCluster() {
if (cluster != null) {
service.deleteCluster(CLUSTER_ID);
snapshotScheduler.tick(Instant.now());
}
}
@Test
void maxError2LoadsGoodRowsPastOneBadValueAndFailsOnSecond() throws Exception {
s3.putObject(BUCKET, "one-error.txt",
"1|alice\nnot_a_number|bob\n2|charlie\n".getBytes(StandardCharsets.UTF_8),
"text/plain", Map.of());
s3.putObject(BUCKET, "two-errors.txt",
"1|alice\nbad_1|bob\nbad_2|charlie\n2|david\n".getBytes(StandardCharsets.UTF_8),
"text/plain", Map.of());
try (Connection connection = RedshiftConnections.waitForConnection(cluster);
Statement stmt = connection.createStatement()) {
stmt.execute("CREATE TABLE t_maxerror_two (id int, name text)");
// MAXERROR 2 tolerates 1 conversion error (REJECT_LIMIT 1) and loads the good rows past it
stmt.execute("COPY t_maxerror_two (id, name) FROM 's3://" + BUCKET + "/one-error.txt' "
+ "DELIMITER '|' MAXERROR 2");
try (ResultSet rs = stmt.executeQuery("SELECT count(*), min(id), max(id) FROM t_maxerror_two")) {
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(1, rs.getInt(2));
assertEquals(2, rs.getInt(3));
}
// A second bad value reaches the error count of 2 and aborts the COPY
stmt.execute("CREATE TABLE t_maxerror_fail (id int, name text)");
assertThrows(SQLException.class, () -> stmt.execute(
"COPY t_maxerror_fail (id, name) FROM 's3://" + BUCKET + "/two-errors.txt' "
+ "DELIMITER '|' MAXERROR 2"));
try (ResultSet rs = stmt.executeQuery("SELECT count(*) FROM t_maxerror_fail")) {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
}
}
}
@Test
void maxError0And1FailAtFirstConversionError() throws Exception {
s3.putObject(BUCKET, "one-error-default.txt",
"1|alice\nnot_a_number|bob\n2|charlie\n".getBytes(StandardCharsets.UTF_8),
"text/plain", Map.of());
try (Connection connection = RedshiftConnections.waitForConnection(cluster);
Statement stmt = connection.createStatement()) {
stmt.execute("CREATE TABLE t_maxerror_default (id int, name text)");
// MAXERROR 0 fails at the first error
assertThrows(SQLException.class, () -> stmt.execute(
"COPY t_maxerror_default (id, name) FROM 's3://" + BUCKET + "/one-error-default.txt' "
+ "DELIMITER '|' MAXERROR 0"));
// MAXERROR 1 also fails at the first error
assertThrows(SQLException.class, () -> stmt.execute(
"COPY t_maxerror_default (id, name) FROM 's3://" + BUCKET + "/one-error-default.txt' "
+ "DELIMITER '|' MAXERROR 1"));
try (ResultSet rs = stmt.executeQuery("SELECT count(*) FROM t_maxerror_default")) {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
}
}
}
}
@@ -226,7 +226,7 @@ class CopyStatementParserTest {
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' FORMAT AS PARQUET"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' JSON 's3://mybucket/jsonpaths.json'"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' FIXEDWIDTH 'a:1,b:2'"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' GZIP MAXERROR 10"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' GZIP ESCAPE"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' TRIMBLANKS"));
}
@@ -520,6 +520,54 @@ class CopyStatementParserTest {
assertEquals("YYYY-MM-DD[ HH:MI:SS]", transforms.timeFormat().spec());
}
@Test
void copyParsesMaxError() {
CopyStatementParser.CopyTransforms tolerant =
copyFrom("COPY t FROM 's3://b/k' MAXERROR AS 10").transforms();
assertEquals(10, tolerant.maxError());
assertTrue(tolerant.toleratesErrors());
assertFalse(tolerant.fieldLevel());
assertEquals(100_000, copyFrom("COPY t FROM 's3://b/k' MAXERROR 100000").transforms().maxError());
assertTrue(copyFrom("COPY t FROM 's3://b/k' FORMAT AS JSON 'auto' MAXERROR 5").transforms()
.toleratesErrors());
}
@Test
void aMaxErrorOfZeroOrOneFailsAtTheFirstErrorAsWithoutTheOption() {
for (String count : new String[] {"0", "1"}) {
CopyStatementParser.CopyTransforms transforms =
copyFrom("COPY t FROM 's3://b/k' MAXERROR " + count).transforms();
assertFalse(transforms.toleratesErrors(), count);
}
assertEquals(0, copyFrom("COPY t FROM 's3://b/k'").transforms().maxError());
}
@Test
void copyRejectsAMaxErrorOutOfRangeOrRepeated() {
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR 100001"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR 5 MAXERROR 6"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR -1"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR AS"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR abc"));
assertNull(CopyStatementParser.parse("COPY t FROM 's3://b/k' MAXERROR 1234567890"));
}
@Test
void maxErrorReadsLeadingZerosAndFollowsAnyClauseOrder() {
assertEquals(7, copyFrom("COPY t FROM 's3://b/k' MAXERROR 007").transforms().maxError());
CopyStatementParser.S3CopyFrom before = copyFrom("COPY t FROM 's3://b/k' MAXERROR 5 GZIP IGNOREHEADER 1");
CopyStatementParser.S3CopyFrom after = copyFrom("COPY t FROM 's3://b/k' GZIP IGNOREHEADER 1 MAXERROR 5");
assertEquals(5, before.transforms().maxError());
assertEquals(5, after.transforms().maxError());
assertTrue(before.gzip() && after.gzip());
assertEquals(1, after.headerLines());
}
@Test
void copyParsesFillRecord() {
CopyStatementParser.CopyTransforms transforms =
@@ -1662,6 +1662,37 @@ class S3CopySimulatorTest {
S3CopySimulator.alignKinds(explicit, catalog));
}
private static String backendCopy(int maxError, boolean csv) {
CopyStatementParser.CopyTransforms transforms = new CopyStatementParser.CopyTransforms(
false, false, false, false, null, null, null, false, false, maxError);
return S3CopySimulator.copyBackendSql(new CopyStatementParser.S3CopyFrom(
"t", List.of(), "wh", "k", "|", 0, false, csv, null, null, false, false, false, transforms), null);
}
@Test
void copyBackendSql_asksTheBackendToSkipConversionErrorsUpToMaxErrorMinusOne() {
assertTrue(backendCopy(10, false).endsWith(", ON_ERROR ignore, REJECT_LIMIT 9)"), backendCopy(10, false));
assertTrue(backendCopy(2, true).contains("FORMAT csv"), backendCopy(2, true));
assertTrue(backendCopy(2, true).endsWith(", ON_ERROR ignore, REJECT_LIMIT 1)"), backendCopy(2, true));
}
@Test
void copyBackendSql_buildsTheWholeStatementFromAParsedMaxError() {
CopyStatementParser.S3Statement parsed = CopyStatementParser.parse(
"COPY t FROM 's3://wh/k' DELIMITER '|' NULL AS 'x' GZIP IGNOREHEADER 1 MAXERROR AS 10");
assertEquals("COPY t FROM STDIN WITH (FORMAT text, DELIMITER '|', NULL 'x', ON_ERROR ignore, "
+ "REJECT_LIMIT 9)",
S3CopySimulator.copyBackendSql((CopyStatementParser.S3CopyFrom) parsed, null));
}
@Test
void copyBackendSql_failsAtTheFirstErrorForAMaxErrorOfZeroOrOne() {
assertFalse(backendCopy(0, false).contains("ON_ERROR"), backendCopy(0, false));
assertFalse(backendCopy(1, false).contains("ON_ERROR"), backendCopy(1, false));
assertFalse(backendCopy(1, false).contains("REJECT_LIMIT"), backendCopy(1, false));
}
@Test
void kindOf_mapsTheCatalogTypeOids() {
assertEquals(CopyRecordTransformer.ColumnKind.DATE, S3CopySimulator.kindOf("1082"));