Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sramazzina committed Jan 24, 2025
1 parent e6089f4 commit 29b077e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ private Object[] concatFields(Object[] inputRowData) throws HopException {
nullString = Const.NVL(field.getNullString(), "");
}

concatField(targetString, valueMeta, valueData, nullString);
String trimType = data.trimType[i];
concatField(targetString, valueMeta, valueData, nullString, trimType);
}

outputRowData[data.outputRowMeta.size() - 1] = targetString.toString();
Expand All @@ -166,13 +167,32 @@ private Object[] concatFields(Object[] inputRowData) throws HopException {
}

private void concatField(
StringBuilder targetField, IValueMeta valueMeta, Object valueData, String nullString)
StringBuilder targetField,
IValueMeta valueMeta,
Object valueData,
String nullString,
String trimType)
throws HopValueException {

if (valueMeta.isNull(valueData)) {
targetField.append(nullString);
} else {
targetField.append(valueMeta.getString(valueData));
if (trimType != null) {
switch (trimType) {
case ConcatFieldsMeta.TRIM_TYPE_LEFT:
targetField.append(valueMeta.getString(valueData).replaceAll("^\\s+", ""));
break;
case ConcatFieldsMeta.TRIM_TYPE_RIGHT:
targetField.append(valueMeta.getString(valueData).replaceAll("\\s+$", ""));
break;
case ConcatFieldsMeta.TRIM_TYPE_BOTH:
targetField.append(valueMeta.getString(valueData).trim());
break;
default:
targetField.append(valueMeta.getString(valueData));
break;
}
}
}
}

Expand Down Expand Up @@ -220,12 +240,18 @@ private void initStringDataFields() {
}

data.stringNullValue = new String[meta.getOutputFields().size()];
data.trimType = new String[meta.getOutputFields().size()];

for (int i = 0; i < meta.getOutputFields().size(); i++) {
data.stringNullValue[i] = "";
String nullString = meta.getOutputFields().get(i).getNullString();
if (!StringUtil.isEmpty(nullString)) {
data.stringNullValue[i] = nullString;
}
String trimType = meta.getOutputFields().get(i).getTrimType();
if (!StringUtil.isEmpty(trimType)) {
data.trimType[i] = trimType;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class ConcatFieldsData extends BaseTransformData implements ITransformDat
public String stringSeparator;
public String stringEnclosure;
public String[] stringNullValue;

public String[] trimType;

public int targetFieldLength; // for fast data dump (StringBuilder size)

public ConcatFieldsData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
public class ConcatFieldsMeta extends BaseTransformMeta<ConcatFields, ConcatFieldsData> {
private static final Class<?> PKG = ConcatFieldsMeta.class;

public static final String TRIM_TYPE_LEFT = "left";
public static final String TRIM_TYPE_RIGHT = "right";
public static final String TRIM_TYPE_BOTH = "both";

/** The separator to choose for the CSV file */
@HopMetadataProperty(key = "separator", injectionKey = "SEPARATOR")
private String separator;
Expand Down

0 comments on commit 29b077e

Please sign in to comment.