the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548001412


##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,944 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hudi.common.schema;
+
+import org.apache.hudi.avro.HoodieAvroUtils;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+import org.apache.hudi.exception.HoodieAvroSchemaException;
+
+import org.apache.avro.JsonProperties;
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+/**
+ * Wrapper class for Avro Schema that provides Hudi-specific schema 
functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class serves as the primary interface for schema operations within 
Hudi.
+ * It encapsulates an Avro Schema and provides a consistent, type-safe API 
while
+ * maintaining full compatibility with existing Avro-based code.</p>
+ *
+ * <p>Key features:
+ * <ul>
+ *   <li>Binary compatibility with Avro Schema</li>
+ *   <li>Type-safe field access through HoodieSchemaField</li>
+ *   <li>Support for all Avro schema types and operations</li>
+ *   <li>Consistent error handling using Hudi exceptions</li>
+ *   <li>Integration with Hudi's Option type for null safety</li>
+ * </ul></p>
+ *
+ * <p>Usage examples:
+ * <pre>{@code
+ * // Create from JSON
+ * HoodieSchema schema = HoodieSchema.parse(jsonSchemaString);
+ *
+ * // Create primitive schemas
+ * HoodieSchema stringSchema = HoodieSchema.create(HoodieSchemaType.STRING);
+ *
+ * // Access schema properties
+ * HoodieSchemaType type = schema.getType();
+ * List<HoodieSchemaField> fields = schema.getFields();
+ * Option<HoodieSchemaField> field = schema.getField("fieldName");
+ *
+ * // Convert back to Avro for compatibility
+ * Schema avroSchema = schema.getAvroSchema();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchema implements Serializable {
+
+  /**
+   * Constant representing a null JSON value, equivalent to 
JsonProperties.NULL_VALUE.
+   * This provides compatibility with Avro's JsonProperties while maintaining 
Hudi's API.
+   */
+  public static final Object NULL_VALUE = JsonProperties.NULL_VALUE;
+  private static final long serialVersionUID = 1L;
+  private final Schema avroSchema;
+  private final HoodieSchemaType type;
+
+  /**
+   * Creates a new HoodieSchema wrapping the given Avro schema.
+   *
+   * @param avroSchema the Avro schema to wrap, cannot be null
+   * @throws IllegalArgumentException if avroSchema is null
+   */
+  private HoodieSchema(Schema avroSchema) {
+    ValidationUtils.checkArgument(avroSchema != null, "Avro schema cannot be 
null");
+    this.avroSchema = avroSchema;
+    Schema.Type avroType = avroSchema.getType();
+    ValidationUtils.checkState(avroType != null, "Avro schema type cannot be 
null");
+    this.type = HoodieSchemaType.fromAvroType(avroType);
+  }
+
+  /**
+   * Factory method to create HoodieSchema from an Avro schema.
+   *
+   * @param avroSchema the Avro schema to wrap
+   * @return new HoodieSchema instance
+   * @throws IllegalArgumentException if avroSchema is null
+   */
+  public static HoodieSchema fromAvroSchema(Schema avroSchema) {
+    return new HoodieSchema(avroSchema);
+  }
+
+  /**
+   * Parses a JSON schema string and returns the corresponding HoodieSchema.
+   *
+   * @param jsonSchema the JSON schema string to parse
+   * @return parsed HoodieSchema
+   * @throws HoodieAvroSchemaException if the schema string is invalid
+   */
+  public static HoodieSchema parse(String jsonSchema) {
+    ValidationUtils.checkArgument(jsonSchema != null && 
!jsonSchema.trim().isEmpty(),
+        "Schema string cannot be null or empty");
+
+    try {
+      Schema avroSchema = new Schema.Parser().parse(jsonSchema);
+      return new HoodieSchema(avroSchema);
+    } catch (Exception e) {
+      throw new HoodieAvroSchemaException("Failed to parse schema: " + 
jsonSchema + ". Error: " + e.getMessage());

Review Comment:
   Yes, I'll add a constructor for HoodieAvroSchemaException to allow this



-- 
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]

Reply via email to