kamronis commented on code in PR #17682:
URL: https://github.com/apache/hudi/pull/17682#discussion_r2646630539
##########
hudi-common/src/main/java/org/apache/hudi/avro/HoodieAvroUtils.java:
##########
@@ -1369,6 +1369,45 @@ public static BigDecimal convertBytesToBigDecimal(byte[]
value, int precision, i
scale, new MathContext(precision, RoundingMode.HALF_UP));
}
+ /**
+ * Projects a record to a new schema by performing a shallow copy of fields.
+ * Best used for adding or removing top-level metadata fields.
+ * <p>
+ * This is a high-performance alternative to deep rewriting. It only
iterates through
+ * the top-level fields of the target schema and pulls values from the
source record
+ * by field name.
+ * <p>
+ * <p>
+ * This is significantly faster than {@link #rewriteRecordWithNewSchema} for:
+ * 1. Wide records (many top-level fields): Reduces CPU overhead/recursion.
+ * 2. Deeply nested records: Uses reference-copying for nested structures
instead of rebuilding them.
+ * <p>
+ * <b>Warning:</b> This method does not recursively rewrite/transform nested
records, arrays,
+ * or maps. It assumes that the underlying values for each field are already
+ * compatible with the target schema.
+ *
+ * @param record The source GenericRecord to project.
+ * @param targetSchema The schema to project the record into.
+ * @return A new GenericRecord matching targetSchema, or the original record
if
+ * the schemas are identical in field count.
+ */
+
+ public static GenericRecord projectRecordToNewSchemaShallow(GenericRecord
record, Schema targetSchema) {
+ if (record.getSchema().getFields().size() ==
targetSchema.getFields().size()) {
+ return record;
+ } else {
+ GenericData.Record rec = new GenericData.Record(targetSchema);
+ for (Schema.Field field : targetSchema.getFields()) {
+ if (record.hasField(field.name())) {
Review Comment:
Fixed
--
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]