voonhous commented on code in PR #17546:
URL: https://github.com/apache/hudi/pull/17546#discussion_r2607354104
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaUtils.java:
##########
@@ -573,4 +574,89 @@ public static HoodieSchema getRecordKeySchema() {
return RECORD_KEY_SCHEMA;
}
+ /**
+ * Finds the schema of a nested field given a dot-separated field name.
+ * This is equivalent to {@link
AvroSchemaUtils#findNestedFieldSchema(Schema, String, boolean)} but operates on
HoodieSchema.
+ *
+ * <p>Unlike {@link #findNestedField(HoodieSchema, String)}, this method
returns just the schema
+ * of the leaf field (unwrapped from union if nullable), not the field with
its parent lineage.
+ *
+ * @param schema the schema to search in
+ * @param fieldName the dot-separated field name (e.g.,
"user.profile.name")
+ * @param allowsMissingField if true, returns Option.empty() when field is
not found;
+ * if false, throws HoodieSchemaException
+ * @return Option containing the schema of the nested field, or
Option.empty() if not found and allowsMissingField is true
+ * @throws HoodieSchemaException if field is not found and
allowsMissingField is false
+ * @since 1.2.0
+ */
+ public static Option<HoodieSchema> findNestedFieldSchema(HoodieSchema
schema, String fieldName, boolean allowsMissingField) {
+ if (fieldName == null || fieldName.isEmpty()) {
+ return Option.empty();
+ }
+ String[] parts = fieldName.split("\\.");
+
+ HoodieSchema currentSchema = schema;
+ for (String part : parts) {
+ HoodieSchema nonNullSchema = getNonNullTypeFromUnion(currentSchema);
+ if (nonNullSchema.getType() != HoodieSchemaType.RECORD) {
+ if (allowsMissingField) {
+ return Option.empty();
+ }
+ throw new HoodieSchemaException(fieldName + " not a field in " +
schema);
+ }
+ Option<HoodieSchemaField> foundFieldOpt = nonNullSchema.getField(part);
+ if (foundFieldOpt.isEmpty()) {
+ if (allowsMissingField) {
+ return Option.empty();
+ }
+ throw new HoodieSchemaException(fieldName + " not a field in " +
schema);
+ }
+ currentSchema = foundFieldOpt.get().schema();
+ }
+ return Option.of(getNonNullTypeFromUnion(currentSchema));
+ }
+
+ /**
+ * Finds the type of a nested field given a dot-separated field name.
+ * This is equivalent to {@link AvroSchemaUtils#findNestedFieldType(Schema,
String)} but operates on HoodieSchema.
+ *
+ * @param schema the schema to search in
+ * @param fieldName the dot-separated field name (e.g., "user.profile.name")
+ * @return Option containing the HoodieSchemaType of the nested field, or
Option.empty() if not found
+ * @since 1.2.0
+ */
+ public static Option<HoodieSchemaType> findNestedFieldType(HoodieSchema
schema, String fieldName) {
+ return findNestedFieldSchema(schema, fieldName,
false).map(HoodieSchema::getType);
+ }
+
+ /**
+ * Returns true if the schema contains a field with the provided name.
+ * This is equivalent to {@link
AvroSchemaUtils#containsFieldInSchema(Schema, String)} but operates on
HoodieSchema.
+ *
+ * @param schema the schema to search in
+ * @param fieldName the field name to check for
+ * @return true if the field exists in the schema, false otherwise
+ * @since 1.2.0
+ */
+ public static boolean containsFieldInSchema(HoodieSchema schema, String
fieldName) {
Review Comment:
Sure, will close this PR then.
--
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]