Skip to content
Closed
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 @@ -35,7 +35,7 @@
import java.util.Map;

import static org.apache.kafka.connect.transforms.util.Requirements.requireMap;
import static org.apache.kafka.connect.transforms.util.Requirements.requireStruct;
import static org.apache.kafka.connect.transforms.util.Requirements.requireStructOrNull;

public abstract class Flatten<R extends ConnectRecord<R>> implements Transformation<R> {

Expand Down Expand Up @@ -136,20 +136,24 @@ private void applySchemaless(Map<String, Object> originalRecord, String fieldNam
}

private R applyWithSchema(R record) {
final Struct value = requireStruct(operatingValue(record), PURPOSE);
final Struct value = requireStructOrNull(operatingValue(record), PURPOSE);

Schema updatedSchema = schemaUpdateCache.get(value.schema());
Schema schema = operatingSchema(record);
Schema updatedSchema = schemaUpdateCache.get(schema);
if (updatedSchema == null) {
final SchemaBuilder builder = SchemaUtil.copySchemaBasics(value.schema(), SchemaBuilder.struct());
Struct defaultValue = (Struct) value.schema().defaultValue();

@HungUnicorn HungUnicorn Jul 9, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it looks for me that before this change, only value.schema is used instead of key.schema should also be considered, which looks not correct, or?

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.

The operatingSchema(record) (line 141 in the PR) obtains the key or value schema, depending upon which transformation is used. So the new PR is a bit more clear, and I believe it is correct.

The prior code used value for the operating value, but if the Flatten$Key transformation was being used the operatingValue(...) actually returned the key, so the value variable name is a bit of a misnomer.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for your detail explanation!

buildUpdatedSchema(value.schema(), "", builder, value.schema().isOptional(), defaultValue);
final SchemaBuilder builder = SchemaUtil.copySchemaBasics(schema, SchemaBuilder.struct());
Struct defaultValue = (Struct) schema.defaultValue();
buildUpdatedSchema(schema, "", builder, schema.isOptional(), defaultValue);
updatedSchema = builder.build();
schemaUpdateCache.put(value.schema(), updatedSchema);
schemaUpdateCache.put(schema, updatedSchema);
}
if (value == null) {
return newRecord(record, updatedSchema, null);
} else {
final Struct updatedValue = new Struct(updatedSchema);
buildWithSchema(value, "", updatedValue);
return newRecord(record, updatedSchema, updatedValue);
}

final Struct updatedValue = new Struct(updatedSchema);
buildWithSchema(value, "", updatedValue);
return newRecord(record, updatedSchema, updatedValue);
}

/**
Expand Down Expand Up @@ -216,6 +220,9 @@ private Schema convertFieldSchema(Schema orig, boolean optional, Object defaultF
}

private void buildWithSchema(Struct record, String fieldNamePrefix, Struct newRecord) {
if (record == null) {
return;
}
for (Field field : record.schema().fields()) {
final String fieldName = fieldName(fieldNamePrefix, field.name());
switch (field.schema().type()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,46 @@ public void testOptionalFieldStruct() {
assertNull(transformedStruct.get("B.opt_int32"));
}

@Test
public void testOptionalStruct() {
xformValue.configure(Collections.<String, String>emptyMap());

SchemaBuilder builder = SchemaBuilder.struct().optional();
builder.field("opt_int32", Schema.OPTIONAL_INT32_SCHEMA);
Schema schema = builder.build();

SourceRecord transformed = xformValue.apply(new SourceRecord(null, null,
"topic", 0,
schema, null));

assertEquals(Schema.Type.STRUCT, transformed.valueSchema().type());
assertNull(transformed.value());
}

@Test
public void testOptionalNestedStruct() {
xformValue.configure(Collections.<String, String>emptyMap());

SchemaBuilder builder = SchemaBuilder.struct().optional();
builder.field("opt_int32", Schema.OPTIONAL_INT32_SCHEMA);
Schema supportedTypesSchema = builder.build();

builder = SchemaBuilder.struct();
builder.field("B", supportedTypesSchema);
Schema oneLevelNestedSchema = builder.build();

Struct oneLevelNestedStruct = new Struct(oneLevelNestedSchema);
oneLevelNestedStruct.put("B", null);

SourceRecord transformed = xformValue.apply(new SourceRecord(null, null,
"topic", 0,
oneLevelNestedSchema, oneLevelNestedStruct));

assertEquals(Schema.Type.STRUCT, transformed.valueSchema().type());
Struct transformedStruct = (Struct) transformed.value();
assertNull(transformedStruct.get("B.opt_int32"));
}

@Test
public void testOptionalFieldMap() {
xformValue.configure(Collections.<String, String>emptyMap());
Expand Down