Skip to content
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

sink-las: fix getField and column types mapping #113

Merged
merged 3 commits into from
Aug 22, 2024
Merged
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
40 changes: 22 additions & 18 deletions sink-las/app/src/main/java/sink/LasSinkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.apache.avro.generic.GenericData;
import com.google.common.base.Preconditions;



import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -106,7 +104,10 @@ void handle(SinkRecordBatch batch) {
}

void putValue(GenericData.Record record, Schema schema, String field, Object value) {
Schema valueSchema = schema.getField(field).schema();
String lowerFieldName = field.toLowerCase();
Schema.Field valueField = schema.getField(lowerFieldName);
Preconditions.checkNotNull(valueField, "Can not find the column named %s in the target LAS table", lowerFieldName);
Schema valueSchema = valueField.schema();
switch (valueSchema.getType()) {
// for number type, it must be a double type.
case INT:
Expand All @@ -121,24 +122,27 @@ void putValue(GenericData.Record record, Schema schema, String field, Object val
float floatValue = ((Double) value).floatValue();
record.put(field, floatValue);
break;
case DOUBLE:
double doubleValue = (Double) value;
record.put(field, doubleValue);
break;
case STRING:
record.put(field, value.toString());
break;
case UNION:
// by default, a field in LAS table is nullable, ant its schema is a union like [type, null].
// by default, a field in a LAS table is nullable, ant its schema is a union like [type, null].
List<Schema> unionTypes = valueSchema.getTypes();
if(unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.INT))) {
int unionIntValue = ((Double) value).intValue();
record.put(field, unionIntValue);
break;
} else if (unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.LONG))) {
long unionLongValue = ((Double) value).longValue();
record.put(field, unionLongValue);
break;
} else if (unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.FLOAT))) {
float unionFloatValue = ((Double) value).floatValue();
record.put(field, unionFloatValue);
break;
Preconditions.checkArgument(unionTypes.size() == 2, "Unsupported column schema %s in a LAS table", valueSchema.toString());
Preconditions.checkArgument(unionTypes.get(1).getType().equals(Schema.Type.NULL) || unionTypes.get(0).getType().equals(Schema.Type.NULL), "Unsupported column schema %s in a LAS table", valueSchema.toString());

if(unionTypes.get(1).getType().equals(Schema.Type.NULL)) {
putValue(record, unionTypes.get(0), field, value);
} else {
putValue(record, unionTypes.get(1), field, value);
}
break;
default:
record.put(field, value);
throw new RuntimeException(String.format("Unsupported column type %s in a LAS table, we only allow string type and number types in a LAS table.", valueSchema.getType().toString()));

}
}
Expand All @@ -153,7 +157,7 @@ GenericData.Record convertRecord(SinkRecord sinkRecord) {
var r = new GenericData.Record(schema);
if(valueRecord == null) {
// This represents a deleting row event from CDC source, for LAS sink, we set 'is_deleted' column to true.
putValue(r, schema, "is_deleted", true);
putValue(r, schema, "is_deleted", "true");
} else {
for (var entry : valueRecord.entrySet()) {
var value = entry.getValue();
Expand Down
Loading