diff --git a/docs/services/redshift.md b/docs/services/redshift.md index 8db3fcf3fe..d120014535 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -801,11 +801,15 @@ order) through its own S3 service and streams the rows into the backing PostgreS match its format fails the COPY, whatever `MAXERROR` says; the message names the column and the format, and Floci writes no `stl_load_errors` row. `ACCEPTANYDATE` loads such a value as `NULL` instead; when no format is given it reads the ISO form of a date, with an optional time of day, and - turns anything else into `NULL`. Numeric parts with no separator between them, as in `YYYYMMDD`, need their + turns anything else into `NULL`. An empty unquoted field of a `DATE`, `TIMESTAMP` or other non-character column + is loaded as `NULL`, as in AWS, without requiring `ACCEPTANYDATE` or `EMPTYASNULL`. Numeric parts with no + separator between them, as in `YYYYMMDD`, need their full width, a part may be named once, and `HH12` needs `AM` or `PM`; any other format string is not recognized. - Like `TRUNCATECOLUMNS`, these read column types from the catalog, so they work only over the Simple Query - protocol and are not combined with `FORMAT AS JSON`. Floci deviations: `'auto'` is accepted but converts + Like `TRUNCATECOLUMNS`, these and the empty non-character rule read column types from the catalog, so + they apply only with the options that query column types (`DATEFORMAT`, `TIMEFORMAT`, `FILLRECORD`), over the + Simple Query protocol, and are not combined with `FORMAT AS JSON`; a plain `COPY` without those options does + not read the catalog. Floci deviations: `'auto'` is accepted but converts nothing, leaving every value to PostgreSQL, which reads ISO 8601, Julian and `YYYYMMDD HHMISS` but not every form Redshift recognizes (for example `BC` dates and `YYYY.DDD`), and with it `ACCEPTANYDATE` cannot turn a value into `NULL`, so a value PostgreSQL rejects fails the COPY; `TIMESTAMPTZ` columns are read by diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformer.java b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformer.java index 7265090409..682a5cabe1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformer.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformer.java @@ -22,7 +22,7 @@ final class CopyRecordTransformer { /** What a COPY column holds, as far as the date and time formats are concerned. */ enum ColumnKind { - DATE, TIMESTAMP, TEXT, OTHER + DATE, TIMESTAMP, TEXT, CHAR, OTHER } private record Field(int tokenEnd, byte[] value, boolean quoted) { @@ -221,8 +221,13 @@ final class CopyRecordTransformer { private void writeField(Field field, int column, ByteArrayOutputStream out) { byte[] value = field.value(); + boolean emptyField = !field.quoted() && value.length == 0; + boolean nonCharacter = columnKinds != null && column < columnKinds.size() + && columnKinds.get(column) != ColumnKind.TEXT + && columnKinds.get(column) != ColumnKind.CHAR; boolean nulled = (transforms.blanksAsNull() && isBlank(value)) - || (transforms.emptyAsNull() && value.length == 0); + || (transforms.emptyAsNull() && value.length == 0) + || (emptyField && nonCharacter); if (nulled) { out.writeBytes(nullMarker); return; @@ -297,7 +302,7 @@ final class CopyRecordTransformer { CopyTemporalFormat format = switch (kind) { case DATE -> transforms.dateFormat(); case TIMESTAMP -> transforms.timeFormat(); - case TEXT, OTHER -> null; + case TEXT, CHAR, OTHER -> null; }; if (format == null || !format.converts()) { return value; 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 0a7375d50a..a24ac94eff 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 @@ -646,7 +646,8 @@ public final class S3CopySimulator { /** * 1082 = date, 1114 = timestamp, and 25 = text, 1043 = varchar for the character columns. - * CHAR (1042 = bpchar) is left as OTHER so missing columns load as NULL, matching AWS FILLRECORD. + * 1042 = bpchar (CHAR) has its own kind so missing columns load as NULL under FILLRECORD + * while present empty fields remain character strings. * A timestamptz (1184) is read by PostgreSQL as it stands. */ static CopyRecordTransformer.ColumnKind kindOf(String typeOid) { @@ -659,6 +660,9 @@ public final class S3CopySimulator { if ("25".equals(typeOid) || "1043".equals(typeOid)) { return CopyRecordTransformer.ColumnKind.TEXT; } + if ("1042".equals(typeOid)) { + return CopyRecordTransformer.ColumnKind.CHAR; + } return CopyRecordTransformer.ColumnKind.OTHER; } diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformerTest.java index 05cde24f05..721b8b4041 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/proxy/CopyRecordTransformerTest.java @@ -72,11 +72,78 @@ class CopyRecordTransformerTest { } @Test - void emptyTemporalFieldsWithoutAcceptAnyDateFailTheCopy() { + void emptyTemporalFieldsWithoutAcceptAnyDateLoadAsNull() { + assertEquals("1|2003-03-31|\\N\n", + applyTemporal(false, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), "1|03/31/2003|\n")); + assertEquals("1|\\N|2003-03-31 12:00:00\n", + applyTemporal(false, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), "1||03/31/2003 12:00:00\n")); + } + + @Test + void unquotedEmptyTemporalFieldsInCsvLoadAsNullWithoutAcceptAnyDate() { + assertEquals("1,2003-03-31,\n", + applyTemporal(true, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), "1,03/31/2003,\n")); + assertEquals("1,,2003-03-31 12:00:00\n", + applyTemporal(true, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), "1,,03/31/2003 12:00:00\n")); + } + + @Test + void quotedEmptyTemporalFieldsInCsvWithoutAcceptAnyDateFailTheCopy() { assertThrows(S3CopySimulator.S3TransferException.class, - () -> applyTemporal(false, temporal("MM/DD/YYYY", "MM/DD/YYYY", false), "1|03/31/2003|\n")); - assertThrows(S3CopySimulator.S3TransferException.class, - () -> applyTemporal(false, temporal("MM/DD/YYYY", "MM/DD/YYYY", false), "1||03/31/2003 12:00:00\n")); + () -> applyTemporal(true, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), + "1,\"\",03/31/2003 12:00:00\n")); + } + + @Test + void emptyNonCharacterFieldsLoadAsNull() { + assertEquals("\\N|2003-03-31|2003-03-31 12:00:00\n", + applyTemporal(false, temporal("MM/DD/YYYY", "MM/DD/YYYY HH:MI:SS", false), + "|03/31/2003|03/31/2003 12:00:00\n")); + } + + @Test + void emptyCharFieldRemainsEmptyStringWithoutEmptyAsNull() { + List kindsWithChar = List.of( + CopyRecordTransformer.ColumnKind.CHAR, + CopyRecordTransformer.ColumnKind.DATE); + CopyStatementParser.CopyTransforms transforms = new CopyStatementParser.CopyTransforms( + false, false, false, false, null, CopyTemporalFormat.parse("MM/DD/YYYY", false), null, false); + CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( + "t", List.of(), "b", "k", "|", 0, false, false, null, null, false, false, false, transforms); + + assertEquals("|2003-03-31\n", new String( + new CopyRecordTransformer(spec, null, kindsWithChar).apply("|03/31/2003\n".getBytes(StandardCharsets.UTF_8)), + StandardCharsets.UTF_8)); + } + + @Test + void emptyCharFieldWithEmptyAsNullLoadsAsNull() { + List kindsWithChar = List.of( + CopyRecordTransformer.ColumnKind.CHAR, + CopyRecordTransformer.ColumnKind.DATE); + CopyStatementParser.CopyTransforms transforms = new CopyStatementParser.CopyTransforms( + false, true, false, false, null, CopyTemporalFormat.parse("MM/DD/YYYY", false), null, false); + CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( + "t", List.of(), "b", "k", "|", 0, false, false, null, null, false, false, false, transforms); + + assertEquals("\\N|2003-03-31\n", new String( + new CopyRecordTransformer(spec, null, kindsWithChar).apply("|03/31/2003\n".getBytes(StandardCharsets.UTF_8)), + StandardCharsets.UTF_8)); + } + + @Test + void fillRecordFillsMissingCharColumnAsNull() { + List kinds = List.of( + CopyRecordTransformer.ColumnKind.TEXT, + CopyRecordTransformer.ColumnKind.CHAR); + CopyStatementParser.CopyTransforms fill = new CopyStatementParser.CopyTransforms( + false, false, false, false, null, null, null, false, true); + CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( + "t", List.of(), "b", "k", "|", 0, false, false, null, null, false, false, false, fill); + + assertEquals("hello|\\N\n", new String( + new CopyRecordTransformer(spec, null, kinds).apply("hello\n".getBytes(StandardCharsets.UTF_8)), + StandardCharsets.UTF_8)); } @Test @@ -187,7 +254,7 @@ class CopyRecordTransformerTest { @Test void fillRecordTreatsABlankLineAsOneEmptyField() { - assertEquals("||\\N|\n", applyFill(false, false, "\n")); + assertEquals("\\N||\\N|\n", applyFill(false, false, "\n")); } @Test 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 05baf238ca..2c344c9e95 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 @@ -1698,7 +1698,7 @@ class S3CopySimulatorTest { assertEquals(CopyRecordTransformer.ColumnKind.DATE, S3CopySimulator.kindOf("1082")); assertEquals(CopyRecordTransformer.ColumnKind.TIMESTAMP, S3CopySimulator.kindOf("1114")); assertEquals(CopyRecordTransformer.ColumnKind.TEXT, S3CopySimulator.kindOf("25")); - assertEquals(CopyRecordTransformer.ColumnKind.OTHER, S3CopySimulator.kindOf("1042")); + assertEquals(CopyRecordTransformer.ColumnKind.CHAR, S3CopySimulator.kindOf("1042")); assertEquals(CopyRecordTransformer.ColumnKind.TEXT, S3CopySimulator.kindOf("1043")); assertEquals(CopyRecordTransformer.ColumnKind.OTHER, S3CopySimulator.kindOf("1184")); assertEquals(CopyRecordTransformer.ColumnKind.OTHER, S3CopySimulator.kindOf("23")); @@ -1736,6 +1736,24 @@ class S3CopySimulatorTest { assertEquals("1|2003-03-31\n", copyDataPayload(out.toByteArray())); } + @Test + void streamCopyInput_loadsEmptyNonCharacterAndTemporalFieldsAsNull() throws Exception { + CopyStatementParser.CopyTransforms transforms = new CopyStatementParser.CopyTransforms( + false, false, false, false, null, CopyTemporalFormat.parse("MM/DD/YYYY", false), null, false); + CopyStatementParser.S3CopyFrom spec = new CopyStatementParser.S3CopyFrom( + "people", List.of(), "wh", "p.txt", "|", 0, false, false, null, null, + false, false, false, transforms); + when(s3.getObject("wh", "p.txt")).thenReturn(new S3Object("wh", "p.txt", + "1|\n|03/31/2003\n".getBytes(StandardCharsets.UTF_8), "text/plain")); + S3CopySimulator.CopyInput input = new S3CopySimulator.CopyInput(spec, List.of("p.txt"), s3, null, null); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + S3CopySimulator.streamCopyInput(input, null, null, + List.of(CopyRecordTransformer.ColumnKind.OTHER, CopyRecordTransformer.ColumnKind.DATE), out); + + assertEquals("1|\\N\n\\N|2003-03-31\n", copyDataPayload(out.toByteArray())); + } + @Test void streamCopyInput_fillsShortRecordsWithTheColumnKindsOfTheCatalog() throws Exception { CopyStatementParser.CopyTransforms transforms = new CopyStatementParser.CopyTransforms(