Skip to content

#115 Skip over null values in PatternMapString #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct inpu
final String inputFieldName = inputField.name();
final Object value = inputStruct.get(inputFieldName);
outputStruct.put(inputFieldName, value);
if (inputFieldName.equals(config.srcfieldname)) {
if (inputFieldName.equals(config.srcfieldname) && value != null) {
String replacedField = (String) value;
final Matcher fieldMatcher = this.config.pattern.matcher(replacedField);
String replacedValue = fieldMatcher.replaceAll(this.config.replacement);
Expand All @@ -109,6 +109,8 @@ protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct inpu
}
outputStruct.put(config.destfieldname, replacedValue);
retVal = new SchemaAndValue(outPutSchema, outputStruct);
} else {
retVal = new SchemaAndValue(outPutSchema, inputStruct);
}
}
return retVal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,105 @@ public void multiple() {
assertStruct(expectedStruct, actualStruct);
}

@Test
public void missingFieldWithoutRename() {
this.transformation.configure(
ImmutableMap.of(
PatternMapStringConfig.SRC_FIELD_NAME_CONF, "fieldA",
PatternMapStringConfig.DEST_FIELD_NAME_CONF, "fieldA",
PatternMapStringConfig.VALUE_PATTERN_CONF, "from",
PatternMapStringConfig.VALUE_REPLACEMENT_CONF, "to"
)
);

Schema inputSchema = SchemaBuilder.struct()
.name("testing")
.field("fieldA", Schema.OPTIONAL_STRING_SCHEMA)
.field("fieldB", Schema.STRING_SCHEMA);
Struct inputStruct = new Struct(inputSchema)
.put("fieldB", "valueB");

final Object key = isKey ? inputStruct : null;
final Object value = isKey ? null : inputStruct;
final Schema keySchema = isKey ? inputSchema : null;
final Schema valueSchema = isKey ? null : inputSchema;

final SinkRecord inputRecord = new SinkRecord(
TOPIC,
1,
keySchema,
key,
valueSchema,
value,
1234L
);
final SinkRecord outputRecord = this.transformation.apply(inputRecord);
assertNotNull(outputRecord);

final Schema actualSchema = isKey ? outputRecord.keySchema() : outputRecord.valueSchema();
final Struct actualStruct = (Struct) (isKey ? outputRecord.key() : outputRecord.value());

final Schema expectedSchema = SchemaBuilder.struct()
.name("testing")
.field("fieldA", Schema.OPTIONAL_STRING_SCHEMA)
.field("fieldB", Schema.STRING_SCHEMA);
Struct expectedStruct = new Struct(expectedSchema)
.put("fieldB", "valueB");

assertSchema(expectedSchema, actualSchema);
assertStruct(expectedStruct, actualStruct);
}

@Test
public void missingFieldWithRename() {
this.transformation.configure(
ImmutableMap.of(
PatternMapStringConfig.SRC_FIELD_NAME_CONF, "fieldA",
PatternMapStringConfig.DEST_FIELD_NAME_CONF, "fieldC",
PatternMapStringConfig.VALUE_PATTERN_CONF, "from",
PatternMapStringConfig.VALUE_REPLACEMENT_CONF, "to"
)
);

Schema inputSchema = SchemaBuilder.struct()
.name("testing")
.field("fieldA", Schema.OPTIONAL_STRING_SCHEMA)
.field("fieldB", Schema.STRING_SCHEMA);
Struct inputStruct = new Struct(inputSchema)
.put("fieldB", "valueB");

final Object key = isKey ? inputStruct : null;
final Object value = isKey ? null : inputStruct;
final Schema keySchema = isKey ? inputSchema : null;
final Schema valueSchema = isKey ? null : inputSchema;

final SinkRecord inputRecord = new SinkRecord(
TOPIC,
1,
keySchema,
key,
valueSchema,
value,
1234L
);
final SinkRecord outputRecord = this.transformation.apply(inputRecord);
assertNotNull(outputRecord);

final Schema actualSchema = isKey ? outputRecord.keySchema() : outputRecord.valueSchema();
final Struct actualStruct = (Struct) (isKey ? outputRecord.key() : outputRecord.value());

final Schema expectedSchema = SchemaBuilder.struct()
.name("testing")
.field("fieldA", Schema.OPTIONAL_STRING_SCHEMA)
.field("fieldC", Schema.OPTIONAL_STRING_SCHEMA)
.field("fieldB", Schema.STRING_SCHEMA);
Struct expectedStruct = new Struct(expectedSchema)
.put("fieldB", "valueB");

assertSchema(expectedSchema, actualSchema);
assertStruct(expectedStruct, actualStruct);
}


public static class KeyTest<R extends ConnectRecord<R>> extends PatternMapStringTest {
protected KeyTest() {
Expand Down