mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
* feat(redshift): support COMPUPDATE, STATUPDATE, and TRIMBLANKS in COPY (#7751) * fix(redshift): keep NULL markers and empty strings correct under COPY TRIMBLANKS (#7751) Skip trimming for a field that is the NULL marker, and quote a CSV field that trims to an empty string so it is not read as NULL. * fix(redshift): accept COMPUPDATE PRESET and trim trailing tabs under TRIMBLANKS (#7751)
This commit is contained in:
1 parent
5d086210b2
commit
6ca2b03d21
6 files changed
+231
-17
No files matched your search
@@ -221,17 +221,30 @@ final class CopyRecordTransformer {
|
||||
|
||||
private void writeField(Field field, int column, ByteArrayOutputStream out) {
|
||||
byte[] value = field.value();
|
||||
boolean isVarchar = columnKinds == null
|
||||
|| (column < columnKinds.size() && columnKinds.get(column) == ColumnKind.TEXT);
|
||||
// A NULL marker that ends in spaces is matched on the raw field, so trimming must leave it alone.
|
||||
boolean rawNullMarker = !field.quoted() && Arrays.equals(value, nullMarker);
|
||||
if (transforms.trimBlanks() && isVarchar && !rawNullMarker) {
|
||||
value = trimTrailingSpaces(value, csv);
|
||||
}
|
||||
// Trimming a CSV field down to nothing yields an empty string, which an unquoted empty field would turn into NULL.
|
||||
boolean trimmedToEmpty = csv && !field.quoted() && value.length == 0 && field.value().length > 0;
|
||||
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))
|
||||
boolean nulled = (transforms.blanksAsNull() && isBlank(field.value()))
|
||||
|| (transforms.emptyAsNull() && value.length == 0)
|
||||
|| (emptyField && nonCharacter);
|
||||
if (nulled) {
|
||||
out.writeBytes(nullMarker);
|
||||
return;
|
||||
}
|
||||
if (trimmedToEmpty && nullMarker.length == 0) {
|
||||
out.writeBytes(new byte[]{DOUBLE_QUOTE, DOUBLE_QUOTE});
|
||||
return;
|
||||
}
|
||||
// A field that already is the NULL marker must not be cut or reformatted: that would turn NULL into data.
|
||||
boolean nullMarkerField = !field.quoted() && Arrays.equals(value, nullMarker);
|
||||
boolean converted = false;
|
||||
@@ -257,6 +270,26 @@ final class CopyRecordTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] trimTrailingSpaces(byte[] value, boolean csv) {
|
||||
int end = value.length;
|
||||
while (end > 0) {
|
||||
if (value[end - 1] != ' ' && value[end - 1] != '\t') {
|
||||
break;
|
||||
}
|
||||
if (!csv) {
|
||||
int backslashes = 0;
|
||||
for (int k = end - 2; k >= 0 && value[k] == BACKSLASH; k--) {
|
||||
backslashes++;
|
||||
}
|
||||
if ((backslashes % 2) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
end--;
|
||||
}
|
||||
return end == value.length ? value : Arrays.copyOf(value, end);
|
||||
}
|
||||
|
||||
private boolean containsDelimiter(byte[] value) {
|
||||
for (byte b : value) {
|
||||
if (b == delimiter) {
|
||||
|
||||
@@ -40,14 +40,15 @@ public final class CopyStatementParser {
|
||||
CopyTemporalFormat timeFormat,
|
||||
boolean acceptAnyDate,
|
||||
boolean fillRecord,
|
||||
int maxError) {
|
||||
int maxError,
|
||||
boolean trimBlanks) {
|
||||
|
||||
public static final CopyTransforms NONE = new CopyTransforms(false, false, false, false, null);
|
||||
|
||||
public CopyTransforms(boolean blanksAsNull, boolean emptyAsNull, boolean removeQuotes,
|
||||
boolean truncateColumns, Character invalidCharReplacement) {
|
||||
this(blanksAsNull, emptyAsNull, removeQuotes, truncateColumns, invalidCharReplacement,
|
||||
null, null, false, false, 0);
|
||||
null, null, false, false, 0, false);
|
||||
}
|
||||
|
||||
public CopyTransforms(boolean blanksAsNull, boolean emptyAsNull, boolean removeQuotes,
|
||||
@@ -55,7 +56,7 @@ public final class CopyStatementParser {
|
||||
CopyTemporalFormat dateFormat, CopyTemporalFormat timeFormat,
|
||||
boolean acceptAnyDate) {
|
||||
this(blanksAsNull, emptyAsNull, removeQuotes, truncateColumns, invalidCharReplacement,
|
||||
dateFormat, timeFormat, acceptAnyDate, false, 0);
|
||||
dateFormat, timeFormat, acceptAnyDate, false, 0, false);
|
||||
}
|
||||
|
||||
public CopyTransforms(boolean blanksAsNull, boolean emptyAsNull, boolean removeQuotes,
|
||||
@@ -63,7 +64,15 @@ public final class CopyStatementParser {
|
||||
CopyTemporalFormat dateFormat, CopyTemporalFormat timeFormat,
|
||||
boolean acceptAnyDate, boolean fillRecord) {
|
||||
this(blanksAsNull, emptyAsNull, removeQuotes, truncateColumns, invalidCharReplacement,
|
||||
dateFormat, timeFormat, acceptAnyDate, fillRecord, 0);
|
||||
dateFormat, timeFormat, acceptAnyDate, fillRecord, 0, false);
|
||||
}
|
||||
|
||||
public CopyTransforms(boolean blanksAsNull, boolean emptyAsNull, boolean removeQuotes,
|
||||
boolean truncateColumns, Character invalidCharReplacement,
|
||||
CopyTemporalFormat dateFormat, CopyTemporalFormat timeFormat,
|
||||
boolean acceptAnyDate, boolean fillRecord, int maxError) {
|
||||
this(blanksAsNull, emptyAsNull, removeQuotes, truncateColumns, invalidCharReplacement,
|
||||
dateFormat, timeFormat, acceptAnyDate, fillRecord, maxError, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +85,7 @@ public final class CopyStatementParser {
|
||||
|
||||
/** Whether the options depend on the type of each column, which only the catalog can tell. */
|
||||
public boolean needsColumnTypes() {
|
||||
return temporal() || fillRecord;
|
||||
return temporal() || fillRecord || trimBlanks;
|
||||
}
|
||||
|
||||
/** Whether date or timestamp fields are read with a format other than PostgreSQL's own. */
|
||||
@@ -85,7 +94,8 @@ public final class CopyStatementParser {
|
||||
}
|
||||
|
||||
public boolean fieldLevel() {
|
||||
return blanksAsNull || emptyAsNull || removeQuotes || truncateColumns || temporal() || fillRecord;
|
||||
return blanksAsNull || emptyAsNull || removeQuotes || truncateColumns || temporal() || fillRecord
|
||||
|| trimBlanks;
|
||||
}
|
||||
|
||||
public boolean any() {
|
||||
@@ -380,6 +390,11 @@ public final class CopyStatementParser {
|
||||
private static final Pattern CSV_PATTERN = Pattern.compile("(?i)\\b(?:FORMAT\\s+(?:AS\\s+)?)?CSV\\b");
|
||||
private static final Pattern JSON_AUTO_PATTERN = Pattern.compile(
|
||||
"(?i)\\b(?:FORMAT\\s+(?:AS\\s+)?)?JSON(?:\\s+AS)?\\s+['\"]auto(?:\\s+(ignorecase))?['\"]");
|
||||
private static final Pattern COMPUPDATE_PATTERN = Pattern.compile(
|
||||
"(?i)\\bCOMPUPDATE(?:\\s+(?:PRESET|ON|OFF|TRUE|FALSE))?\\b");
|
||||
private static final Pattern STATUPDATE_PATTERN = Pattern.compile(
|
||||
"(?i)\\bSTATUPDATE(?:\\s+(?:ON|OFF|TRUE|FALSE))?\\b");
|
||||
private static final Pattern TRIMBLANKS_PATTERN = Pattern.compile("(?i)\\bTRIMBLANKS\\b");
|
||||
|
||||
/**
|
||||
* Options this simulator does not implement. A COPY carrying any of these is not intercepted:
|
||||
@@ -390,9 +405,8 @@ public final class CopyStatementParser {
|
||||
"(?i)\\b(FIXEDWIDTH|PARQUET|AVRO|ORC|SHAPEFILE|BZIP2|LZOP|ZSTD"
|
||||
+ "|ENCRYPTED|ENCODING"
|
||||
+ "|MASTER_SYMMETRIC_KEY|KMS_KEY_ID"
|
||||
+ "|TRIMBLANKS"
|
||||
+ "|IGNOREBLANKLINES|ESCAPE|EXPLICIT_IDS|COMPUPDATE"
|
||||
+ "|STATUPDATE|NOLOAD|ROUNDEC|QUOTE|SSH|READRATIO|COMPROWS|DIMENSION)\\b");
|
||||
+ "|IGNOREBLANKLINES|ESCAPE|EXPLICIT_IDS"
|
||||
+ "|NOLOAD|ROUNDEC|QUOTE|SSH|READRATIO|COMPROWS|DIMENSION)\\b");
|
||||
|
||||
/** A {@code ;} followed by another statement: only a lone trailing {@code ;} is tolerated. */
|
||||
private static final Pattern TRAILING_STATEMENT = Pattern.compile(";\\s*\\S");
|
||||
@@ -725,6 +739,10 @@ public final class CopyStatementParser {
|
||||
boolean fillRecord = false;
|
||||
int maxError = 0;
|
||||
boolean seenMaxError = false;
|
||||
boolean trimBlanks = false;
|
||||
boolean seenCompupdate = false;
|
||||
boolean seenStatupdate = false;
|
||||
boolean seenTrimBlanks = false;
|
||||
|
||||
boolean seenDateFormat = false;
|
||||
boolean seenTimeFormat = false;
|
||||
@@ -759,6 +777,9 @@ public final class CopyStatementParser {
|
||||
Matcher acceptAnyDateMatcher = ACCEPTANYDATE_PATTERN.matcher(options);
|
||||
Matcher fillRecordMatcher = FILLRECORD_PATTERN.matcher(options);
|
||||
Matcher maxErrorMatcher = MAXERROR_PATTERN.matcher(options);
|
||||
Matcher compupdateMatcher = COMPUPDATE_PATTERN.matcher(options);
|
||||
Matcher statupdateMatcher = STATUPDATE_PATTERN.matcher(options);
|
||||
Matcher trimBlanksMatcher = TRIMBLANKS_PATTERN.matcher(options);
|
||||
AuthClauses auth = new AuthClauses(options, defaultIamRoleArn);
|
||||
|
||||
int next;
|
||||
@@ -900,6 +921,25 @@ public final class CopyStatementParser {
|
||||
seenMaxError = true;
|
||||
maxError = parsed;
|
||||
offset = maxErrorMatcher.end();
|
||||
} else if (matchClause(compupdateMatcher, offset, len)) {
|
||||
if (seenCompupdate) {
|
||||
return null;
|
||||
}
|
||||
seenCompupdate = true;
|
||||
offset = compupdateMatcher.end();
|
||||
} else if (matchClause(statupdateMatcher, offset, len)) {
|
||||
if (seenStatupdate) {
|
||||
return null;
|
||||
}
|
||||
seenStatupdate = true;
|
||||
offset = statupdateMatcher.end();
|
||||
} else if (matchClause(trimBlanksMatcher, offset, len)) {
|
||||
if (seenTrimBlanks) {
|
||||
return null;
|
||||
}
|
||||
seenTrimBlanks = true;
|
||||
trimBlanks = true;
|
||||
offset = trimBlanksMatcher.end();
|
||||
} else if ((next = auth.consume(offset, len)) != AuthClauses.NO_MATCH) {
|
||||
if (next == AuthClauses.REJECT) {
|
||||
return null;
|
||||
@@ -928,7 +968,7 @@ public final class CopyStatementParser {
|
||||
}
|
||||
CopyTransforms transforms = new CopyTransforms(
|
||||
blanksAsNull, emptyAsNull, removeQuotes, truncateColumns, invalidChar,
|
||||
dateFormat, timeFormat, acceptAnyDate, fillRecord, maxError);
|
||||
dateFormat, timeFormat, acceptAnyDate, fillRecord, maxError, trimBlanks);
|
||||
if (transforms.any() && jsonAuto) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -577,7 +577,7 @@ public final class S3CopySimulator {
|
||||
}
|
||||
if (spec.transforms().needsColumnTypes() && columnKinds == null) {
|
||||
throw new S3TransferException(SQLSTATE_INTERNAL,
|
||||
"COPY ... DATEFORMAT, TIMEFORMAT and FILLRECORD need catalog column types, which are only "
|
||||
"COPY ... DATEFORMAT, TIMEFORMAT, FILLRECORD and TRIMBLANKS need catalog column types, which are only "
|
||||
+ "available over the Simple Query protocol; connect with preferQueryMode=simple", null);
|
||||
}
|
||||
byte[] buffer = new byte[CHUNK];
|
||||
@@ -626,7 +626,7 @@ public final class S3CopySimulator {
|
||||
"COPY object exceeds the " + COPY_TRANSFORM_MAX_OBJECT_BYTES
|
||||
+ "-byte limit for the field-level options "
|
||||
+ "(EMPTYASNULL, BLANKSASNULL, REMOVEQUOTES, ACCEPTINVCHARS, TRUNCATECOLUMNS, "
|
||||
+ "DATEFORMAT, TIMEFORMAT, FILLRECORD)", null);
|
||||
+ "DATEFORMAT, TIMEFORMAT, FILLRECORD, TRIMBLANKS)", null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user