Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -137,7 +137,7 @@ public R apply(R record) {
}

private boolean isTombstoneRecord(R record) {
return record.value() == null;
return operatingValue(record) == null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please clean up the logic? This method no longer returns true if it's just a tombstone record; it also returns true if the key is null for InsertField$Key.

I'd suggest removing this method altogether and just changing the point where this method is called to simply be:

          if (operatingValue(record) == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus one for this; confusion around the term tombstone has already caused plenty of miscommunication around this issue and we should be careful to use it correctly.

}

private R applySchemaless(R record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@
import static org.junit.Assert.assertSame;

public class InsertFieldTest {
private InsertField<SourceRecord> xform = new InsertField.Value<>();
private InsertField<SourceRecord> xformKey = new InsertField.Key<>();
private InsertField<SourceRecord> xformValue = new InsertField.Value<>();

@After
public void teardown() {
xform.close();
xformValue.close();
}

@Test(expected = DataException.class)
public void topLevelStructRequired() {
xform.configure(Collections.singletonMap("topic.field", "topic_field"));
xform.apply(new SourceRecord(null, null, "", 0, Schema.INT32_SCHEMA, 42));
xformValue.configure(Collections.singletonMap("topic.field", "topic_field"));
xformValue.apply(new SourceRecord(null, null, "", 0, Schema.INT32_SCHEMA, 42));
}

@Test
Expand All @@ -55,13 +56,13 @@ public void copySchemaAndInsertConfiguredFields() {
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final Schema simpleStructSchema = SchemaBuilder.struct().name("name").version(1).doc("doc").field("magic", Schema.OPTIONAL_INT64_SCHEMA).build();
final Struct simpleStruct = new Struct(simpleStructSchema).put("magic", 42L);

final SourceRecord record = new SourceRecord(null, null, "test", 0, simpleStructSchema, simpleStruct);
final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(simpleStructSchema.name(), transformedRecord.valueSchema().name());
assertEquals(simpleStructSchema.version(), transformedRecord.valueSchema().version());
Expand All @@ -83,7 +84,7 @@ public void copySchemaAndInsertConfiguredFields() {
assertEquals("my-instance-id", ((Struct) transformedRecord.value()).getString("instance_id"));

// Exercise caching
final SourceRecord transformedRecord2 = xform.apply(
final SourceRecord transformedRecord2 = xformValue.apply(
new SourceRecord(null, null, "test", 1, simpleStructSchema, new Struct(simpleStructSchema)));
assertSame(transformedRecord.valueSchema(), transformedRecord2.valueSchema());
}
Expand All @@ -97,12 +98,12 @@ public void schemalessInsertConfiguredFields() {
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, Collections.singletonMap("magic", 42L));

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(42L, ((Map<?, ?>) transformedRecord.value()).get("magic"));
assertEquals("test", ((Map<?, ?>) transformedRecord.value()).get("topic_field"));
Expand All @@ -121,12 +122,12 @@ public void insertConfiguredFieldsIntoTombstoneEventWithoutSchemaLeavesValueUnch
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, null);

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(null, transformedRecord.value());
assertEquals(null, transformedRecord.valueSchema());
Expand All @@ -141,16 +142,39 @@ public void insertConfiguredFieldsIntoTombstoneEventWithSchemaLeavesValueUnchang
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xform.configure(props);
xformValue.configure(props);

final Schema simpleStructSchema = SchemaBuilder.struct().name("name").version(1).doc("doc").field("magic", Schema.OPTIONAL_INT64_SCHEMA).build();

final SourceRecord record = new SourceRecord(null, null, "test", 0,
simpleStructSchema, null);

final SourceRecord transformedRecord = xform.apply(record);
final SourceRecord transformedRecord = xformValue.apply(record);

assertEquals(null, transformedRecord.value());
assertEquals(simpleStructSchema, transformedRecord.valueSchema());
}

@Test
public void insertkeyFieldsIntoTombstoneEvent() {
final Map<String, Object> props = new HashMap<>();
props.put("topic.field", "topic_field!");
props.put("partition.field", "partition_field");
props.put("timestamp.field", "timestamp_field?");
props.put("static.field", "instance_id");
props.put("static.value", "my-instance-id");

xformKey.configure(props);

final SourceRecord record = new SourceRecord(null, null, "test", 0,
null, Collections.singletonMap("magic", 42L), null, null);

final SourceRecord transformedRecord = xformKey.apply(record);

assertEquals(42L, ((Map<?, ?>) transformedRecord.key()).get("magic"));
assertEquals("test", ((Map<?, ?>) transformedRecord.key()).get("topic_field"));
assertEquals(0, ((Map<?, ?>) transformedRecord.key()).get("partition_field"));
assertEquals(null, ((Map<?, ?>) transformedRecord.key()).get("timestamp_field"));
assertEquals("my-instance-id", ((Map<?, ?>) transformedRecord.key()).get("instance_id"));
Comment thread
gharris1727 marked this conversation as resolved.
Comment thread
gharris1727 marked this conversation as resolved.
}
}