pvary commented on code in PR #14245:
URL: https://github.com/apache/iceberg/pull/14245#discussion_r2414429141
##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/source/RowDataToAvroGenericRecordConverter.java:
##########
@@ -41,17 +44,114 @@
public class RowDataToAvroGenericRecordConverter implements Function<RowData,
GenericRecord> {
private final RowDataToAvroConverters.RowDataToAvroConverter converter;
private final Schema avroSchema;
+ private final RowType rowType;
private RowDataToAvroGenericRecordConverter(RowType rowType, Schema
avroSchema) {
this.converter = RowDataToAvroConverters.createConverter(rowType);
this.avroSchema = avroSchema;
+ this.rowType = rowType;
}
@Override
public GenericRecord apply(RowData rowData) {
+ // Check if schema has timestamp-nanos fields
+ if (needsCustomConversion(avroSchema)) {
+ return convertWithNanos(rowData);
+ }
return (GenericRecord) converter.convert(avroSchema, rowData);
}
+ private boolean needsCustomConversion(Schema schema) {
+ for (Schema.Field field : schema.getFields()) {
+ if (field.schema().getLogicalType() instanceof
LogicalTypes.TimestampNanos) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private GenericRecord convertWithNanos(RowData rowData) {
+ GenericRecord result = new GenericData.Record(avroSchema);
+
+ for (int i = 0; i < avroSchema.getFields().size(); i++) {
+ Schema.Field field = avroSchema.getFields().get(i);
+ if (rowData.isNullAt(i)) {
+ result.put(i, null);
+ } else if (field.schema().getLogicalType() instanceof
LogicalTypes.TimestampNanos) {
+ // Handle timestamp-nanos by converting TimestampData to long nanos
+ // Get precision from the rowType
+ int precision = 6; // default
+ if (rowType.getTypeAt(i).asSummaryString().contains("(9)")) {
+ precision = 9;
+ }
+ TimestampData timestampData = rowData.getTimestamp(i, precision);
+ long nanos =
+ timestampData.getMillisecond() * 1_000_000L +
timestampData.getNanoOfMillisecond();
Review Comment:
Why not do this in the `getFieldValue`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]