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

las-sink: handle nullable columns #108

Merged
merged 1 commit into from
Aug 15, 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
50 changes: 44 additions & 6 deletions sink-las/app/src/main/java/sink/LasSinkTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,49 @@ void handle(SinkRecordBatch batch) {
session.commit(List.of(0L), List.of(recordWriter.getAttemptId()));
}

void putValue(GenericData.Record record, Schema schema, String field, Object value) {
Schema valueSchema = schema.getField(field).schema();
switch (valueSchema.getType()) {
case INT:
int intValue = (int) value;
record.put(field, intValue);
break;
case LONG:
long longValue = (long) value;
record.put(field, longValue);
break;
case FLOAT:
float floatValue = (float) value;
record.put(field, floatValue);
break;
case DOUBLE:
double doubleValue = (double) value;
record.put(field, doubleValue);
case UNION:
// by default, a field in 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 = (int) value;
record.put(field, unionIntValue);
break;
} else if (unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.LONG))) {
long unionLongValue = (long) value;
record.put(field, unionLongValue);
break;
} else if (unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.FLOAT))) {
float unionFloatValue = (float) value;
record.put(field, unionFloatValue);
break;
} else if (unionTypes.stream().anyMatch(it -> it.getType().equals(Schema.Type.DOUBLE))) {
double unionDoubleValue = (double) value;
record.put(field, unionDoubleValue);
break;
}
default:
record.put(field, value);

}
}
GenericData.Record convertRecord(SinkRecord sinkRecord) {
try {
var lasRecord = LasRecord.fromSinkRecord(sinkRecord);
Expand All @@ -114,12 +157,7 @@ GenericData.Record convertRecord(SinkRecord sinkRecord) {
var r = new GenericData.Record(schema);
for (var entry : valueRecord.entrySet()) {
var value = entry.getValue();
if (schema.getField(entry.getKey()).schema().getType().equals(Schema.Type.INT)) {
var intValue = (int) value;
r.put(entry.getKey(), intValue);
} else {
r.put(entry.getKey(), value);
}
putValue(r, schema, entry.getKey(), value);
}
return r;
} catch (Exception e) {
Expand Down
Loading