You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by "bvaradar (via GitHub)" <gi...@apache.org> on 2025/11/14 10:45:18 UTC
[PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
bvaradar opened a new pull request, #14265:
URL: https://github.com/apache/hudi/pull/14265
### Describe the issue this Pull Request addresses
[HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema
### Summary and Changelog
#### **Implementation Details**
**Core Classes Implemented**
##### HoodieSchema (org.apache.hudi.schema.HoodieSchema)
- Primary wrapper for org.apache.avro.Schema
- Delegates all operations to underlying Avro Schema for compatibility
- Provides Builder pattern for complex schema construction
- Includes static factory methods for common schema types
- Supports all Avro schema types: RECORD, ARRAY, MAP, UNION, ENUM, FIXED, and primitives
##### HoodieSchemaField (org.apache.hudi.schema.HoodieSchemaField)
- Wrapper for org.apache.avro.Schema.Field
- Provides field manipulation utilities
- Supports property management and field evolution
##### HoodieSchemaType (org.apache.hudi.schema.HoodieSchemaType)
- Enum wrapper for org.apache.avro.Schema.Type
- Provides type checking and conversion utilities
- Maintains mapping to Avro types
##### HoodieSchemaUtils (org.apache.hudi.schema.HoodieSchemaUtils)
- Utility class for table schema operations
- Delegates to existing AvroSchemaUtils for proven logic
- Provides HoodieSchema-native APIs for common operations
##### HoodieSchemaCompatibility (org.apache.hudi.schema.HoodieSchemaCompatibility)
- Schema evolution and compatibility validation utilities
- Wraps AvroSchemaUtils compatibility checking logic
##### Supporting Classes
- HoodieFieldOrder - Wrapper for Schema.Field.Order enum
- HoodieSchemaFactory - Factory utilities for schema creation
- HoodieJsonProperties - Constants for JSON property handling
### Impact
Not integrated yet.
### Risk Level
None
### Documentation Update
<!-- Describe any necessary documentation update if there is any new feature, config, or user-facing change. If not, put "none".
- The config description must be updated if new configs are added or the default value of the configs are changed.
- Any new feature or user-facing change requires updating the Hudi website. Please follow the
[instruction](https://hudi.apache.org/contribute/developer-setup#website) to make changes to the website. -->
### Contributor's checklist
- [ ] Read through [contributor's guide](https://hudi.apache.org/contribute/how-to-contribute)
- [ ] Enough context is provided in the sections above
- [ ] Adequate tests were added if applicable
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "cshuo (via GitHub)" <gi...@apache.org>.
cshuo commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3544669149
> And then after that, we can break away from the avro implementation backing these objects entirely which provides the flexibility to add new types like variant and blob.
So do you mean the Avro based `HoodieSchema` in this pr is kind of temporal solution for efficient migration away from Avro schema apis, and after migration is done, the `HoodieSchema` will be refactored to decouple from AVRO, or just introduce something like `HoodieSchemaV2`?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2543243482
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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;
Review Comment:
this is a precursor (temporary step) before we define our own schema. The goal is to bring all references to Avro schema in one place before we can swap it.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3554963060
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3561093640
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
}, {
"hash" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9607",
"triggerID" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* b87db1b9597bccda2d309b51de81eaa852a4da61 Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9607)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "yihua (via GitHub)" <gi...@apache.org>.
yihua merged PR #14265:
URL: https://github.com/apache/hudi/pull/14265
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "yihua (via GitHub)" <gi...@apache.org>.
yihua commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2547737419
##########
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 {
Review Comment:
Once this class `HoodieSchema`, `HoodieSchemaField` and other schema-related classes are integrated, we should mark these `@PublicAPIClass` as I imagine the new schema system will also be used by other engine integration outside Hudi repo such as Presto and Trino.
##########
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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
+ return version != null ? version : "unknown";
+ }
+
+ /**
+ * Creates a Hudi write schema from a given schema string with optional operation field.
+ * This is equivalent to HoodieAvroUtils.createHoodieWriteSchema() but returns HoodieSchema.
+ *
+ * @param schemaStr the schema string to convert
+ * @param withOperationField whether to include operation field metadata
+ * @return HoodieSchema configured for write operations
+ * @throws IllegalArgumentException if schema string is invalid
+ */
+ public static HoodieSchema createHoodieWriteSchema(String schemaStr, boolean withOperationField) {
+ ValidationUtils.checkArgument(schemaStr != null && !schemaStr.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+ Schema avroSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaStr, withOperationField);
+ return HoodieSchema.fromAvroSchema(avroSchema);
+ }
+
+ /**
+ * Adds metadata fields to an existing HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.addMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the base schema to add metadata fields to
+ * @param withOperationField whether to include operation field metadata
+ * @return new HoodieSchema with metadata fields added
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema addMetadataFields(HoodieSchema schema, boolean withOperationField) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.addMetadataFields(avroSchema, withOperationField);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Removes metadata fields from a HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.removeMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the schema to remove metadata fields from
+ * @return new HoodieSchema without metadata fields
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema removeMetadataFields(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.removeMetadataFields(avroSchema);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Creates a nullable union (null + specified type).
+ *
+ * @param type the non-null type
+ * @return new HoodieSchema representing a nullable union
+ */
+ public static HoodieSchema createNullableSchema(HoodieSchema type) {
+ ValidationUtils.checkArgument(type != null, "Type cannot be null");
+
+ HoodieSchema nullSchema = HoodieSchema.create(HoodieSchemaType.NULL);
+ return createUnion(nullSchema, type);
+ }
+
+ /**
+ * Returns the type of this schema.
+ *
+ * @return the schema type
+ */
+ public HoodieSchemaType getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this schema, if it has one.
+ *
+ * @return Option containing the schema name, or Option.empty() if none
+ */
+ public Option<String> getName() {
+ return Option.ofNullable(avroSchema.getName());
+ }
+
+ /**
+ * Returns the namespace of this schema, if it has one.
+ *
+ * @return Option containing the schema namespace, or Option.empty() if none
+ */
+ public Option<String> getNamespace() {
+ return Option.ofNullable(avroSchema.getNamespace());
+ }
+
+ /**
+ * Returns the full name of this schema (namespace + name).
+ *
+ * @return Option containing the full schema name, or Option.empty() if none
+ */
+ public Option<String> getFullName() {
+ return Option.ofNullable(avroSchema.getFullName());
+ }
+
+ /**
+ * Returns the documentation string for this schema, if any.
+ *
+ * @return Option containing the documentation, or Option.empty() if none
+ */
+ public Option<String> getDoc() {
+ return Option.ofNullable(avroSchema.getDoc());
+ }
+
+ /**
+ * Returns the fields of this record schema.
+ *
+ * @return list of HoodieSchemaField objects
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public List<HoodieSchemaField> getFields() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get fields from non-record schema: " + type);
+ }
+
+ return avroSchema.getFields().stream()
+ .map(HoodieSchemaField::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Sets the fields for this record schema.
+ *
+ * @param fields the list of fields to set
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public void setFields(List<HoodieSchemaField> fields) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot set fields on non-record schema: " + type);
+ }
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ avroSchema.setFields(avroFields);
+ }
+
+ /**
+ * Returns the field with the specified name.
+ *
+ * @param name the field name to look up
+ * @return Option containing the field, or Option.empty() if not found
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public Option<HoodieSchemaField> getField(String name) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get field from non-record schema: " + type);
+ }
+
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+
+ Schema.Field avroField = avroSchema.getField(name);
+ return avroField != null ? Option.of(new HoodieSchemaField(avroField)) : Option.empty();
+ }
+
+ /**
+ * Returns the element schema for array types.
+ *
+ * @return the element schema
+ * @throws IllegalStateException if this is not an array schema
+ */
+ public HoodieSchema getElementType() {
+ if (type != HoodieSchemaType.ARRAY) {
+ throw new IllegalStateException("Cannot get element type from non-array schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getElementType());
+ }
+
+ /**
+ * Returns the value schema for map types.
+ *
+ * @return the value schema
+ * @throws IllegalStateException if this is not a map schema
+ */
+ public HoodieSchema getValueType() {
+ if (type != HoodieSchemaType.MAP) {
+ throw new IllegalStateException("Cannot get value type from non-map schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getValueType());
+ }
+
+ /**
+ * Returns the schemas in a union type.
+ *
+ * @return list of schemas in the union
+ * @throws IllegalStateException if this is not a union schema
+ */
+ public List<HoodieSchema> getTypes() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get types from non-union schema: " + type);
+ }
+
+ return avroSchema.getTypes().stream()
+ .map(HoodieSchema::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Returns the size of a fixed schema.
+ *
+ * @return the fixed size in bytes
+ * @throws IllegalStateException if this is not a fixed schema
+ */
+ public int getFixedSize() {
+ if (type != HoodieSchemaType.FIXED) {
+ throw new IllegalStateException("Cannot get fixed size from non-fixed schema: " + type);
+ }
+
+ return avroSchema.getFixedSize();
+ }
+
+ /**
+ * Returns the symbols for an enum schema.
+ *
+ * @return list of enum symbols
+ * @throws IllegalStateException if this is not an enum schema
+ */
+ public List<String> getEnumSymbols() {
+ if (type != HoodieSchemaType.ENUM) {
+ throw new IllegalStateException("Cannot get symbols from non-enum schema: " + type);
+ }
+
+ return new ArrayList<>(avroSchema.getEnumSymbols());
+ }
+
+ /**
+ * Returns custom properties attached to this schema.
+ *
+ * @return map of custom properties
+ */
+ public Map<String, Object> getObjectProps() {
+ return avroSchema.getObjectProps();
+ }
+
+ /**
+ * Returns the value of a custom property.
+ *
+ * @param key the property key
+ * @return the property value, or null if not found
+ */
+ public Object getProp(String key) {
+ return avroSchema.getProp(key);
+ }
+
+ /**
+ * Adds a custom property to this schema.
+ *
+ * @param key the property key
+ * @param value the property value
+ */
+ public void addProp(String key, Object value) {
+ ValidationUtils.checkArgument(key != null && !key.isEmpty(), "Property key cannot be null or empty");
+ avroSchema.addProp(key, value);
+ }
+
+ /**
+ * Checks if this is an error record schema.
+ *
+ * @return true if this is an error record
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public boolean isError() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot check error flag on non-record schema: " + type);
+ }
+ return avroSchema.isError();
+ }
+
+ /**
+ * Checks if this schema is nullable (union containing null).
+ *
+ * @return true if the schema is nullable
+ */
+ public boolean isNullable() {
+ if (type != HoodieSchemaType.UNION) {
+ return false;
+ }
+
+ return avroSchema.getTypes().stream()
+ .anyMatch(schema -> schema.getType() == Schema.Type.NULL);
+ }
+
+ /**
+ * If this is a union schema, returns the non-null type.
+ *
+ * @return the non-null schema from a union
+ * @throws IllegalStateException if this is not a nullable union
+ */
+ public HoodieSchema getNonNullType() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get non-null type from non-union schema: " + type);
+ }
+
+ List<Schema> types = avroSchema.getTypes();
Review Comment:
Should this check that the union only has two Schema type elements to be safe?
##########
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:
nit: should wrap `e` in the `HoodieAvroSchemaException`
##########
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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
Review Comment:
Do we plan to introduce `HoodieSchemaVersion` in the future?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560648605
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599)
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2543254522
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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 {
Review Comment:
Lets focus on encapsulating avro schema only in this PR. There is a multi-step work defined in this [parent ticket](https://github.com/apache/hudi/issues/14263?issue=apache%7Chudi%7C14302) on the plan
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3550415462
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2532407802
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchema.java:
##########
@@ -0,0 +1,579 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.exception.HoodieAvroSchemaException;
+
+import org.apache.avro.JsonProperties;
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test class for {@link HoodieSchema}.
+ */
+public class TestHoodieSchema {
+
+ private static final String SAMPLE_RECORD_SCHEMA =
Review Comment:
Should we have some more complex schema here? Something with nested fields, arrays, maps, and logical types for example.
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
+
+ // Test without operation field
+ HoodieSchema writeSchemaNoOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, false);
+
+ assertNotNull(writeSchemaNoOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaNoOp.getType());
+
+ // Should have different number of fields
+ assertNotNull(writeSchemaNoOp.getFields());
Review Comment:
This assertion is just check if it is non null, not the number of fields. Should this be updated?
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
+
+ // Test without operation field
+ HoodieSchema writeSchemaNoOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, false);
+
+ assertNotNull(writeSchemaNoOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaNoOp.getType());
+
+ // Should have different number of fields
+ assertNotNull(writeSchemaNoOp.getFields());
+ }
+
+ @Test
+ public void testCreateHoodieWriteSchemaValidation() {
+ // Should throw on null schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema(null, true);
+ });
+
+ // Should throw on empty schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema("", true);
+ });
+
+ // Should throw on whitespace-only schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema(" ", true);
+ });
+ }
+
+ @Test
+ public void testAddMetadataFields() {
+ HoodieSchema baseSchema = HoodieSchema.parse(SIMPLE_SCHEMA);
+
+ // Test adding metadata fields with operation field
+ HoodieSchema schemaWithMeta = HoodieSchemaUtils.addMetadataFields(baseSchema, true);
+
+ assertNotNull(schemaWithMeta);
+ assertTrue(schemaWithMeta.getFields().size() > baseSchema.getFields().size());
Review Comment:
Can this assert on an exact number? Same with line 111
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
Review Comment:
Can this assert on an exact number?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "cshuo (via GitHub)" <gi...@apache.org>.
cshuo commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2532482784
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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;
Review Comment:
WRT the extensibility, does it mean the new types or new schema related fuctionalities should rely on AVRO schema, since HoodieSchema is implemented as wrapper of AVRO schema.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560709000
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605)
* 3fabee1beb76f051d355511808f516b015866ef7 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560682120
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "voonhous (via GitHub)" <gi...@apache.org>.
voonhous commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2542855161
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
+1, ordering here dictates how records are sorted and not ordering of fields/columns within a table:
i.e. schema with:
```json
{
"type": "record",
"name": "UserRecord",
"namespace": "com.example",
"fields": [
{"name": "score", "type": "int", "order": "descending"},
{"name": "lastName", "type": "string", "order": "ascending"},
{"name": "firstName", "type": "string"},
{"name": "internal_id", "type": "string", "order": "ignore"}
]
}
```
Gives a sort order when sort is performed on the data below:
```
Record score lastName firstName internal_id
A 100 Smith Alice X001
B 100 Jones Bob X002
C 90 Zeller Cathy
```
Don't think this is really required for now, we can add it in later if it's absolutely required.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548009710
##########
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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
+ return version != null ? version : "unknown";
+ }
+
+ /**
+ * Creates a Hudi write schema from a given schema string with optional operation field.
+ * This is equivalent to HoodieAvroUtils.createHoodieWriteSchema() but returns HoodieSchema.
+ *
+ * @param schemaStr the schema string to convert
+ * @param withOperationField whether to include operation field metadata
+ * @return HoodieSchema configured for write operations
+ * @throws IllegalArgumentException if schema string is invalid
+ */
+ public static HoodieSchema createHoodieWriteSchema(String schemaStr, boolean withOperationField) {
+ ValidationUtils.checkArgument(schemaStr != null && !schemaStr.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+ Schema avroSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaStr, withOperationField);
+ return HoodieSchema.fromAvroSchema(avroSchema);
+ }
+
+ /**
+ * Adds metadata fields to an existing HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.addMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the base schema to add metadata fields to
+ * @param withOperationField whether to include operation field metadata
+ * @return new HoodieSchema with metadata fields added
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema addMetadataFields(HoodieSchema schema, boolean withOperationField) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.addMetadataFields(avroSchema, withOperationField);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Removes metadata fields from a HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.removeMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the schema to remove metadata fields from
+ * @return new HoodieSchema without metadata fields
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema removeMetadataFields(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.removeMetadataFields(avroSchema);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Creates a nullable union (null + specified type).
+ *
+ * @param type the non-null type
+ * @return new HoodieSchema representing a nullable union
+ */
+ public static HoodieSchema createNullableSchema(HoodieSchema type) {
+ ValidationUtils.checkArgument(type != null, "Type cannot be null");
+
+ HoodieSchema nullSchema = HoodieSchema.create(HoodieSchemaType.NULL);
+ return createUnion(nullSchema, type);
+ }
+
+ /**
+ * Returns the type of this schema.
+ *
+ * @return the schema type
+ */
+ public HoodieSchemaType getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this schema, if it has one.
+ *
+ * @return Option containing the schema name, or Option.empty() if none
+ */
+ public Option<String> getName() {
+ return Option.ofNullable(avroSchema.getName());
+ }
+
+ /**
+ * Returns the namespace of this schema, if it has one.
+ *
+ * @return Option containing the schema namespace, or Option.empty() if none
+ */
+ public Option<String> getNamespace() {
+ return Option.ofNullable(avroSchema.getNamespace());
+ }
+
+ /**
+ * Returns the full name of this schema (namespace + name).
+ *
+ * @return Option containing the full schema name, or Option.empty() if none
+ */
+ public Option<String> getFullName() {
+ return Option.ofNullable(avroSchema.getFullName());
+ }
+
+ /**
+ * Returns the documentation string for this schema, if any.
+ *
+ * @return Option containing the documentation, or Option.empty() if none
+ */
+ public Option<String> getDoc() {
+ return Option.ofNullable(avroSchema.getDoc());
+ }
+
+ /**
+ * Returns the fields of this record schema.
+ *
+ * @return list of HoodieSchemaField objects
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public List<HoodieSchemaField> getFields() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get fields from non-record schema: " + type);
+ }
+
+ return avroSchema.getFields().stream()
+ .map(HoodieSchemaField::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Sets the fields for this record schema.
+ *
+ * @param fields the list of fields to set
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public void setFields(List<HoodieSchemaField> fields) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot set fields on non-record schema: " + type);
+ }
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ avroSchema.setFields(avroFields);
+ }
+
+ /**
+ * Returns the field with the specified name.
+ *
+ * @param name the field name to look up
+ * @return Option containing the field, or Option.empty() if not found
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public Option<HoodieSchemaField> getField(String name) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get field from non-record schema: " + type);
+ }
+
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+
+ Schema.Field avroField = avroSchema.getField(name);
+ return avroField != null ? Option.of(new HoodieSchemaField(avroField)) : Option.empty();
+ }
+
+ /**
+ * Returns the element schema for array types.
+ *
+ * @return the element schema
+ * @throws IllegalStateException if this is not an array schema
+ */
+ public HoodieSchema getElementType() {
+ if (type != HoodieSchemaType.ARRAY) {
+ throw new IllegalStateException("Cannot get element type from non-array schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getElementType());
+ }
+
+ /**
+ * Returns the value schema for map types.
+ *
+ * @return the value schema
+ * @throws IllegalStateException if this is not a map schema
+ */
+ public HoodieSchema getValueType() {
+ if (type != HoodieSchemaType.MAP) {
+ throw new IllegalStateException("Cannot get value type from non-map schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getValueType());
+ }
+
+ /**
+ * Returns the schemas in a union type.
+ *
+ * @return list of schemas in the union
+ * @throws IllegalStateException if this is not a union schema
+ */
+ public List<HoodieSchema> getTypes() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get types from non-union schema: " + type);
+ }
+
+ return avroSchema.getTypes().stream()
+ .map(HoodieSchema::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Returns the size of a fixed schema.
+ *
+ * @return the fixed size in bytes
+ * @throws IllegalStateException if this is not a fixed schema
+ */
+ public int getFixedSize() {
+ if (type != HoodieSchemaType.FIXED) {
+ throw new IllegalStateException("Cannot get fixed size from non-fixed schema: " + type);
+ }
+
+ return avroSchema.getFixedSize();
+ }
+
+ /**
+ * Returns the symbols for an enum schema.
+ *
+ * @return list of enum symbols
+ * @throws IllegalStateException if this is not an enum schema
+ */
+ public List<String> getEnumSymbols() {
+ if (type != HoodieSchemaType.ENUM) {
+ throw new IllegalStateException("Cannot get symbols from non-enum schema: " + type);
+ }
+
+ return new ArrayList<>(avroSchema.getEnumSymbols());
+ }
+
+ /**
+ * Returns custom properties attached to this schema.
+ *
+ * @return map of custom properties
+ */
+ public Map<String, Object> getObjectProps() {
+ return avroSchema.getObjectProps();
+ }
+
+ /**
+ * Returns the value of a custom property.
+ *
+ * @param key the property key
+ * @return the property value, or null if not found
+ */
+ public Object getProp(String key) {
+ return avroSchema.getProp(key);
+ }
+
+ /**
+ * Adds a custom property to this schema.
+ *
+ * @param key the property key
+ * @param value the property value
+ */
+ public void addProp(String key, Object value) {
+ ValidationUtils.checkArgument(key != null && !key.isEmpty(), "Property key cannot be null or empty");
+ avroSchema.addProp(key, value);
+ }
+
+ /**
+ * Checks if this is an error record schema.
+ *
+ * @return true if this is an error record
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public boolean isError() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot check error flag on non-record schema: " + type);
+ }
+ return avroSchema.isError();
+ }
+
+ /**
+ * Checks if this schema is nullable (union containing null).
+ *
+ * @return true if the schema is nullable
+ */
+ public boolean isNullable() {
+ if (type != HoodieSchemaType.UNION) {
+ return false;
+ }
+
+ return avroSchema.getTypes().stream()
+ .anyMatch(schema -> schema.getType() == Schema.Type.NULL);
+ }
+
+ /**
+ * If this is a union schema, returns the non-null type.
+ *
+ * @return the non-null schema from a union
+ * @throws IllegalStateException if this is not a nullable union
+ */
+ public HoodieSchema getNonNullType() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get non-null type from non-union schema: " + type);
+ }
+
+ List<Schema> types = avroSchema.getTypes();
Review Comment:
Yes, I will add this. That can simplify the logic a bit as well if we make that assumption.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560735028
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
}, {
"hash" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605)
* 3fabee1beb76f051d355511808f516b015866ef7 Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606)
* b87db1b9597bccda2d309b51de81eaa852a4da61 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560750415
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
}, {
"hash" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* 3fabee1beb76f051d355511808f516b015866ef7 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606)
* b87db1b9597bccda2d309b51de81eaa852a4da61 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3559585142
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2540202161
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
+
+ // Test without operation field
+ HoodieSchema writeSchemaNoOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, false);
+
+ assertNotNull(writeSchemaNoOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaNoOp.getType());
+
+ // Should have different number of fields
+ assertNotNull(writeSchemaNoOp.getFields());
+ }
+
+ @Test
+ public void testCreateHoodieWriteSchemaValidation() {
+ // Should throw on null schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema(null, true);
+ });
+
+ // Should throw on empty schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema("", true);
+ });
+
+ // Should throw on whitespace-only schema
+ assertThrows(IllegalArgumentException.class, () -> {
+ HoodieSchemaUtils.createHoodieWriteSchema(" ", true);
+ });
+ }
+
+ @Test
+ public void testAddMetadataFields() {
+ HoodieSchema baseSchema = HoodieSchema.parse(SIMPLE_SCHEMA);
+
+ // Test adding metadata fields with operation field
+ HoodieSchema schemaWithMeta = HoodieSchemaUtils.addMetadataFields(baseSchema, true);
+
+ assertNotNull(schemaWithMeta);
+ assertTrue(schemaWithMeta.getFields().size() > baseSchema.getFields().size());
Review Comment:
I updated this
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
Review Comment:
I've updated this as well
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
+
+ // Test without operation field
+ HoodieSchema writeSchemaNoOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, false);
+
+ assertNotNull(writeSchemaNoOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaNoOp.getType());
+
+ // Should have different number of fields
+ assertNotNull(writeSchemaNoOp.getFields());
Review Comment:
I've updated this as well
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaFactory.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Factory class for creating common HoodieSchema instances.
+ *
+ * <p>This factory provides convenient methods for creating commonly used
+ * schema patterns without requiring complex builder logic.</p>
+ *
+ * @since 1.2.0
+ */
+public final class HoodieSchemaFactory {
+
+ private HoodieSchemaFactory() {
+ // Utility class, no instantiation
+ }
+
+ /**
+ * Creates a record schema with the specified name and fields.
+ *
+ * @param recordName the name of the record
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ * @throws IllegalArgumentException if recordName is null/empty or fields is null/empty
+ */
+ public static HoodieSchema createRecord(String recordName, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, null, null, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified name, namespace, and fields.
+ *
+ * @param recordName the name of the record
+ * @param namespace the namespace for the record
+ * @param doc the documentation string
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String recordName, String namespace, String doc,
+ List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, doc, namespace, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates an enum schema with the specified name and symbols.
+ *
+ * @param enumName the name of the enum
+ * @param symbols the enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String enumName, List<String> symbols) {
+ ValidationUtils.checkArgument(enumName != null && !enumName.isEmpty(),
+ "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(),
+ "Symbols list cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(enumName, null, null, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified name and size.
+ *
+ * @param fixedName the name of the fixed schema
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String fixedName, int size) {
Review Comment:
I've moved these to the HoodieSchema to match the avro way of doing this for now
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,392 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = new HoodieSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name and schema.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema) {
+ return of(name, schema, null, null);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name, schema, and doc.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc) {
+ return of(name, schema, doc, null);
+ }
+
+ /**
+ * Creates a metadata field for Hudi internal use.
+ * This is a convenience method for creating fields that are part of Hudi's metadata.
+ *
+ * @param name the metadata field name
+ * @param schema the metadata field schema
+ * @return new HoodieSchemaField configured as a metadata field
+ * @throws IllegalArgumentException if name is null/empty or schema is null
+ */
+ public static HoodieSchemaField createMetadataField(String name, HoodieSchema schema) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Metadata field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Metadata field schema cannot be null");
+
+ return HoodieSchemaField.of(name, schema, "Hudi metadata field: " + name, HoodieJsonProperties.NULL_VALUE);
+ }
+
+ /**
+ * Returns the name of this field.
+ *
+ * @return the field name
+ */
+ public String name() {
+ return avroField.name();
+ }
+
+ /**
+ * Returns the schema of this field.
+ *
+ * @return the field schema as HoodieSchema
+ */
+ public HoodieSchema schema() {
+ return hoodieSchema;
+ }
+
+ /**
+ * Returns the documentation string for this field.
+ *
+ * @return Option containing the documentation string, or Option.empty() if none
+ */
+ public Option<String> doc() {
+ return Option.ofNullable(avroField.doc());
+ }
+
+ /**
+ * Returns the default value for this field.
+ *
+ * @return Option containing the default value, or Option.empty() if none
+ */
+ public Option<Object> defaultVal() {
+ if (avroField != null && avroField.hasDefaultValue()) {
+ return Option.of(avroField.defaultVal());
+ }
+ return Option.empty();
+ }
+
+ /**
+ * Returns the sort order for this field.
+ *
+ * @return the field order
+ */
+ public HoodieFieldOrder order() {
+ return HoodieFieldOrder.fromAvroOrder(avroField.order());
+ }
+
+ /**
+ * Returns the position of this field within its enclosing record.
+ *
+ * @return the field position (0-based index)
+ */
+ public int pos() {
+ return avroField.pos();
+ }
+
+ /**
+ * Checks if this field has a default value.
+ *
+ * @return true if the field has a default value
+ */
+ public boolean hasDefaultValue() {
+ return avroField.hasDefaultValue();
+ }
+
+ /**
+ * Returns custom properties attached to this field.
+ *
+ * @return map of custom properties
+ */
+ public java.util.Map<String, Object> getObjectProps() {
Review Comment:
I've updated this as well
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
Review Comment:
I added tests for this that match the existing compatibility tests we have
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2539050608
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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
+ */
+ public HoodieSchema(Schema avroSchema) {
Review Comment:
Sure.
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaFactory.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Factory class for creating common HoodieSchema instances.
+ *
+ * <p>This factory provides convenient methods for creating commonly used
+ * schema patterns without requiring complex builder logic.</p>
+ *
+ * @since 1.2.0
+ */
+public final class HoodieSchemaFactory {
+
+ private HoodieSchemaFactory() {
+ // Utility class, no instantiation
+ }
+
+ /**
+ * Creates a record schema with the specified name and fields.
+ *
+ * @param recordName the name of the record
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ * @throws IllegalArgumentException if recordName is null/empty or fields is null/empty
+ */
+ public static HoodieSchema createRecord(String recordName, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, null, null, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified name, namespace, and fields.
+ *
+ * @param recordName the name of the record
+ * @param namespace the namespace for the record
+ * @param doc the documentation string
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String recordName, String namespace, String doc,
+ List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, doc, namespace, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates an enum schema with the specified name and symbols.
+ *
+ * @param enumName the name of the enum
+ * @param symbols the enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String enumName, List<String> symbols) {
+ ValidationUtils.checkArgument(enumName != null && !enumName.isEmpty(),
+ "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(),
+ "Symbols list cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(enumName, null, null, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified name and size.
+ *
+ * @param fixedName the name of the fixed schema
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String fixedName, int size) {
Review Comment:
The goal was to make it easy to migrate the rest of the code. So you see the APIs structured like Avro code. If you find something redundant, we can cleanup the ones whose signature is not going to be used during migration.
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,392 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = new HoodieSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name and schema.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema) {
+ return of(name, schema, null, null);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name, schema, and doc.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc) {
+ return of(name, schema, doc, null);
+ }
+
+ /**
+ * Creates a metadata field for Hudi internal use.
+ * This is a convenience method for creating fields that are part of Hudi's metadata.
+ *
+ * @param name the metadata field name
+ * @param schema the metadata field schema
+ * @return new HoodieSchemaField configured as a metadata field
+ * @throws IllegalArgumentException if name is null/empty or schema is null
+ */
+ public static HoodieSchemaField createMetadataField(String name, HoodieSchema schema) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Metadata field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Metadata field schema cannot be null");
+
+ return HoodieSchemaField.of(name, schema, "Hudi metadata field: " + name, HoodieJsonProperties.NULL_VALUE);
+ }
+
+ /**
+ * Returns the name of this field.
+ *
+ * @return the field name
+ */
+ public String name() {
+ return avroField.name();
+ }
+
+ /**
+ * Returns the schema of this field.
+ *
+ * @return the field schema as HoodieSchema
+ */
+ public HoodieSchema schema() {
+ return hoodieSchema;
+ }
+
+ /**
+ * Returns the documentation string for this field.
+ *
+ * @return Option containing the documentation string, or Option.empty() if none
+ */
+ public Option<String> doc() {
+ return Option.ofNullable(avroField.doc());
+ }
+
+ /**
+ * Returns the default value for this field.
+ *
+ * @return Option containing the default value, or Option.empty() if none
+ */
+ public Option<Object> defaultVal() {
+ if (avroField != null && avroField.hasDefaultValue()) {
+ return Option.of(avroField.defaultVal());
+ }
+ return Option.empty();
+ }
+
+ /**
+ * Returns the sort order for this field.
+ *
+ * @return the field order
+ */
+ public HoodieFieldOrder order() {
+ return HoodieFieldOrder.fromAvroOrder(avroField.order());
+ }
+
+ /**
+ * Returns the position of this field within its enclosing record.
+ *
+ * @return the field position (0-based index)
+ */
+ public int pos() {
+ return avroField.pos();
+ }
+
+ /**
+ * Checks if this field has a default value.
+ *
+ * @return true if the field has a default value
+ */
+ public boolean hasDefaultValue() {
+ return avroField.hasDefaultValue();
+ }
+
+ /**
+ * Returns custom properties attached to this field.
+ *
+ * @return map of custom properties
+ */
+ public java.util.Map<String, Object> getObjectProps() {
Review Comment:
sure
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2543282049
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -88,28 +88,13 @@ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
* @return new HoodieSchemaField instance
*/
public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
- return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
- }
-
- /**
- * Creates a new HoodieSchemaField with the specified properties, including field order.
- *
- * @param name the name of the field
- * @param schema the schema of the field
- * @param doc the documentation string, can be null
- * @param defaultVal the default value, can be null
- * @param order the field order for sorting
- * @return new HoodieSchemaField instance
- */
- public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
- ValidationUtils.checkArgument(order != null, "Field order cannot be null");
Schema avroSchema = schema.getAvroSchema();
ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
- Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal);
Review Comment:
@bvaradar please see the other comments, consensus is that the order is not used so it will always be default value in Avro.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "voonhous (via GitHub)" <gi...@apache.org>.
voonhous commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2541521188
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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 {
Review Comment:
Got it. IIUC, there will be another separate abstraction for engine specific types to implement, which will map to the HoodieSchema equivalent right?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548094067
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = HoodieSchema.fromAvroSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
Review Comment:
Thanks for pointing this out, I have updated all the callers in this class
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2532399450
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
Review Comment:
Let's make sure there are tests for this class, even if it is just some copy of the existing tests. We may forget to add these when cleaning up the old code in the future and lose coverage.
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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
+ */
+ public HoodieSchema(Schema avroSchema) {
Review Comment:
There is another method exposed below `fromAvroSchema`. Should we keep that and make this constructor private so we can control the number of entrypoints?
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,392 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = new HoodieSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name and schema.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema) {
+ return of(name, schema, null, null);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name, schema, and doc.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc) {
+ return of(name, schema, doc, null);
+ }
+
+ /**
+ * Creates a metadata field for Hudi internal use.
+ * This is a convenience method for creating fields that are part of Hudi's metadata.
+ *
+ * @param name the metadata field name
+ * @param schema the metadata field schema
+ * @return new HoodieSchemaField configured as a metadata field
+ * @throws IllegalArgumentException if name is null/empty or schema is null
+ */
+ public static HoodieSchemaField createMetadataField(String name, HoodieSchema schema) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Metadata field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Metadata field schema cannot be null");
+
+ return HoodieSchemaField.of(name, schema, "Hudi metadata field: " + name, HoodieJsonProperties.NULL_VALUE);
+ }
+
+ /**
+ * Returns the name of this field.
+ *
+ * @return the field name
+ */
+ public String name() {
+ return avroField.name();
+ }
+
+ /**
+ * Returns the schema of this field.
+ *
+ * @return the field schema as HoodieSchema
+ */
+ public HoodieSchema schema() {
+ return hoodieSchema;
+ }
+
+ /**
+ * Returns the documentation string for this field.
+ *
+ * @return Option containing the documentation string, or Option.empty() if none
+ */
+ public Option<String> doc() {
+ return Option.ofNullable(avroField.doc());
+ }
+
+ /**
+ * Returns the default value for this field.
+ *
+ * @return Option containing the default value, or Option.empty() if none
+ */
+ public Option<Object> defaultVal() {
+ if (avroField != null && avroField.hasDefaultValue()) {
+ return Option.of(avroField.defaultVal());
+ }
+ return Option.empty();
+ }
+
+ /**
+ * Returns the sort order for this field.
+ *
+ * @return the field order
+ */
+ public HoodieFieldOrder order() {
+ return HoodieFieldOrder.fromAvroOrder(avroField.order());
+ }
+
+ /**
+ * Returns the position of this field within its enclosing record.
+ *
+ * @return the field position (0-based index)
+ */
+ public int pos() {
+ return avroField.pos();
+ }
+
+ /**
+ * Checks if this field has a default value.
+ *
+ * @return true if the field has a default value
+ */
+ public boolean hasDefaultValue() {
+ return avroField.hasDefaultValue();
+ }
+
+ /**
+ * Returns custom properties attached to this field.
+ *
+ * @return map of custom properties
+ */
+ public java.util.Map<String, Object> getObjectProps() {
Review Comment:
Can this be imported instead of using the FQDN?
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaFactory.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Factory class for creating common HoodieSchema instances.
+ *
+ * <p>This factory provides convenient methods for creating commonly used
+ * schema patterns without requiring complex builder logic.</p>
+ *
+ * @since 1.2.0
+ */
+public final class HoodieSchemaFactory {
+
+ private HoodieSchemaFactory() {
+ // Utility class, no instantiation
+ }
+
+ /**
+ * Creates a record schema with the specified name and fields.
+ *
+ * @param recordName the name of the record
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ * @throws IllegalArgumentException if recordName is null/empty or fields is null/empty
+ */
+ public static HoodieSchema createRecord(String recordName, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, null, null, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified name, namespace, and fields.
+ *
+ * @param recordName the name of the record
+ * @param namespace the namespace for the record
+ * @param doc the documentation string
+ * @param fields the fields for the record
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String recordName, String namespace, String doc,
+ List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(recordName != null && !recordName.isEmpty(),
+ "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null && !fields.isEmpty(),
+ "Fields list cannot be null or empty");
+
+ List<Schema.Field> avroFields = new ArrayList<>();
+ for (HoodieSchemaField field : fields) {
+ avroFields.add(field.getAvroField());
+ }
+
+ Schema recordSchema = Schema.createRecord(recordName, doc, namespace, false, avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates an enum schema with the specified name and symbols.
+ *
+ * @param enumName the name of the enum
+ * @param symbols the enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String enumName, List<String> symbols) {
+ ValidationUtils.checkArgument(enumName != null && !enumName.isEmpty(),
+ "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(),
+ "Symbols list cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(enumName, null, null, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified name and size.
+ *
+ * @param fixedName the name of the fixed schema
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String fixedName, int size) {
Review Comment:
A lot of similar methods are exposed in HoodieSchema. Should we move all of those to the factory or the other way around?
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaType.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of all schema types supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Type to provide a consistent interface
+ * for schema type handling while maintaining binary compatibility with Avro.
+ * Each Hudi schema type corresponds directly to an Avro schema type.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * HoodieSchemaType type = HoodieSchemaType.STRING;
+ * Schema.Type avroType = type.toAvroType();
+ * HoodieSchemaType backToHudi = HoodieSchemaType.fromAvroType(avroType);
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieSchemaType {
Review Comment:
It may be out of scope for this PR but how will the existing avro logical types like Timestamp be covered in the HoodieSchema?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2532393381
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
Is that really necessary though? I don't think we expose any option to set this to another value?
What is significance of field order?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3543514259
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3544677619
> > And then after that, we can break away from the avro implementation backing these objects entirely which provides the flexibility to add new types like variant and blob.
>
> @the-other-tim-brown so do you mean the Avro based `HoodieSchema` in this pr is kind of temporal solution for efficient migration away from Avro schema apis, and after migration is done, the `HoodieSchema` will be refactored to decouple from AVRO, or just introduce something like `HoodieSchemaV2`?
@cshuo it is a tool for migrating but it is not temporary. It is an abstraction that will be able to evolve from this basic/naive implementation into the implementation that we need for our goals of decoupling from Avro.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2539598796
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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 {
Review Comment:
The HoodieSchema is our schema abstraction. It will not be tied to avro in the future. It also will not be extended since we want a single definition of a schema that can be translated to other formats.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2528690738
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
We use this in couple of places. One of them is AvroSchemaComparatorForSchemaEvolution where we check for equality for order.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3532149720
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560881893
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
}, {
"hash" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9607",
"triggerID" : "b87db1b9597bccda2d309b51de81eaa852a4da61",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* 3fabee1beb76f051d355511808f516b015866ef7 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606)
* b87db1b9597bccda2d309b51de81eaa852a4da61 Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9607)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2538952948
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
It's only referenced to determine equality based on my understanding so I am wondering what is its role inside of Hudi?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2539600878
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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
+ */
+ public 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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
+ return version != null ? version : "unknown";
+ }
+
+ /**
+ * Creates a Hudi write schema from a given schema string with optional operation field.
+ * This is equivalent to HoodieAvroUtils.createHoodieWriteSchema() but returns HoodieSchema.
+ *
+ * @param schemaStr the schema string to convert
+ * @param withOperationField whether to include operation field metadata
+ * @return HoodieSchema configured for write operations
+ * @throws IllegalArgumentException if schema string is invalid
+ */
+ public static HoodieSchema createHoodieWriteSchema(String schemaStr, boolean withOperationField) {
+ ValidationUtils.checkArgument(schemaStr != null && !schemaStr.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+ Schema avroSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaStr, withOperationField);
+ return HoodieSchema.fromAvroSchema(avroSchema);
+ }
+
+ /**
+ * Adds metadata fields to an existing HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.addMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the base schema to add metadata fields to
+ * @param withOperationField whether to include operation field metadata
+ * @return new HoodieSchema with metadata fields added
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema addMetadataFields(HoodieSchema schema, boolean withOperationField) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.addMetadataFields(avroSchema, withOperationField);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Removes metadata fields from a HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.removeMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the schema to remove metadata fields from
+ * @return new HoodieSchema without metadata fields
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema removeMetadataFields(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.removeMetadataFields(avroSchema);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Returns the type of this schema.
+ *
+ * @return the schema type
+ */
+ public HoodieSchemaType getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this schema, if it has one.
+ *
+ * @return Option containing the schema name, or Option.empty() if none
+ */
+ public Option<String> getName() {
+ return Option.ofNullable(avroSchema.getName());
+ }
+
+ /**
+ * Returns the namespace of this schema, if it has one.
+ *
+ * @return Option containing the schema namespace, or Option.empty() if none
+ */
+ public Option<String> getNamespace() {
+ return Option.ofNullable(avroSchema.getNamespace());
+ }
+
+ /**
+ * Returns the full name of this schema (namespace + name).
+ *
+ * @return Option containing the full schema name, or Option.empty() if none
+ */
+ public Option<String> getFullName() {
+ return Option.ofNullable(avroSchema.getFullName());
+ }
+
+ /**
+ * Returns the documentation string for this schema, if any.
+ *
+ * @return Option containing the documentation, or Option.empty() if none
+ */
+ public Option<String> getDoc() {
+ return Option.ofNullable(avroSchema.getDoc());
+ }
+
+ /**
+ * Returns the fields of this record schema.
+ *
+ * @return list of HoodieSchemaField objects
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public List<HoodieSchemaField> getFields() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get fields from non-record schema: " + type);
+ }
+
+ return avroSchema.getFields().stream()
Review Comment:
This is a good point but I think we can hold off on the optimizations until we refactor the inner workings of this class to remove the avro dependency.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "voonhous (via GitHub)" <gi...@apache.org>.
voonhous commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2538908521
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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 {
Review Comment:
Maybe we can rename this to `AvroBasedHoodieSchema` and have it extend an abstract class / implement an interface? Future implementations, e.g. `ParquetBasedHoodieSchema` can be added later on. Or is the API not defined yet and is subjected to change?
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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
+ */
+ public 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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
+ return version != null ? version : "unknown";
+ }
+
+ /**
+ * Creates a Hudi write schema from a given schema string with optional operation field.
+ * This is equivalent to HoodieAvroUtils.createHoodieWriteSchema() but returns HoodieSchema.
+ *
+ * @param schemaStr the schema string to convert
+ * @param withOperationField whether to include operation field metadata
+ * @return HoodieSchema configured for write operations
+ * @throws IllegalArgumentException if schema string is invalid
+ */
+ public static HoodieSchema createHoodieWriteSchema(String schemaStr, boolean withOperationField) {
+ ValidationUtils.checkArgument(schemaStr != null && !schemaStr.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+ Schema avroSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaStr, withOperationField);
+ return HoodieSchema.fromAvroSchema(avroSchema);
+ }
+
+ /**
+ * Adds metadata fields to an existing HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.addMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the base schema to add metadata fields to
+ * @param withOperationField whether to include operation field metadata
+ * @return new HoodieSchema with metadata fields added
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema addMetadataFields(HoodieSchema schema, boolean withOperationField) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.addMetadataFields(avroSchema, withOperationField);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Removes metadata fields from a HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.removeMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the schema to remove metadata fields from
+ * @return new HoodieSchema without metadata fields
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema removeMetadataFields(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.removeMetadataFields(avroSchema);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Returns the type of this schema.
+ *
+ * @return the schema type
+ */
+ public HoodieSchemaType getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this schema, if it has one.
+ *
+ * @return Option containing the schema name, or Option.empty() if none
+ */
+ public Option<String> getName() {
+ return Option.ofNullable(avroSchema.getName());
+ }
+
+ /**
+ * Returns the namespace of this schema, if it has one.
+ *
+ * @return Option containing the schema namespace, or Option.empty() if none
+ */
+ public Option<String> getNamespace() {
+ return Option.ofNullable(avroSchema.getNamespace());
+ }
+
+ /**
+ * Returns the full name of this schema (namespace + name).
+ *
+ * @return Option containing the full schema name, or Option.empty() if none
+ */
+ public Option<String> getFullName() {
+ return Option.ofNullable(avroSchema.getFullName());
+ }
+
+ /**
+ * Returns the documentation string for this schema, if any.
+ *
+ * @return Option containing the documentation, or Option.empty() if none
+ */
+ public Option<String> getDoc() {
+ return Option.ofNullable(avroSchema.getDoc());
+ }
+
+ /**
+ * Returns the fields of this record schema.
+ *
+ * @return list of HoodieSchemaField objects
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public List<HoodieSchemaField> getFields() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get fields from non-record schema: " + type);
+ }
+
+ return avroSchema.getFields().stream()
Review Comment:
Wrapper should lazy-load and cache the list of HoodieSchemaField wrappers so they are created only once per schema instance to reduce instance creation and GC pressure. IIRC, `getFields` is called multiple times.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2540206496
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
Will it make sense to have this as a per field value or does it make sense as a table property though?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3550314389
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3533323745
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2542147993
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
I agree, it makes sense to only add this if it is needed instead of maintaining it.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3553075522
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2539060254
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaType.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of all schema types supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Type to provide a consistent interface
+ * for schema type handling while maintaining binary compatibility with Avro.
+ * Each Hudi schema type corresponds directly to an Avro schema type.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * HoodieSchemaType type = HoodieSchemaType.STRING;
+ * Schema.Type avroType = type.toAvroType();
+ * HoodieSchemaType backToHudi = HoodieSchemaType.fromAvroType(avroType);
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieSchemaType {
Review Comment:
Good point. Can we add this in the follow up PR.
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchema.java:
##########
@@ -0,0 +1,579 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.exception.HoodieAvroSchemaException;
+
+import org.apache.avro.JsonProperties;
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test class for {@link HoodieSchema}.
+ */
+public class TestHoodieSchema {
+
+ private static final String SAMPLE_RECORD_SCHEMA =
Review Comment:
Sure
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.avro.HoodieAvroUtils;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieSchemaUtils}.
+ */
+public class TestHoodieSchemaUtils {
+
+ private static final String SIMPLE_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"}"
+ + "]}";
+
+ private static final String EVOLVED_SCHEMA = "{"
+ + "\"type\":\"record\","
+ + "\"name\":\"TestRecord\","
+ + "\"fields\":["
+ + "{\"name\":\"id\",\"type\":\"string\"},"
+ + "{\"name\":\"name\",\"type\":\"string\"},"
+ + "{\"name\":\"age\",\"type\":[\"null\",\"int\"],\"default\":null}"
+ + "]}";
+
+ @Test
+ public void testCreateHoodieWriteSchema() {
+ // Test with operation field
+ HoodieSchema writeSchemaWithOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, true);
+
+ assertNotNull(writeSchemaWithOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaWithOp.getType());
+
+ // Should have original fields plus metadata fields
+ List<HoodieSchemaField> fields = writeSchemaWithOp.getFields();
+ assertTrue(fields.size() > 2); // Original 2 fields + metadata fields
+
+ // Test without operation field
+ HoodieSchema writeSchemaNoOp = HoodieSchemaUtils.createHoodieWriteSchema(SIMPLE_SCHEMA, false);
+
+ assertNotNull(writeSchemaNoOp);
+ assertEquals(HoodieSchemaType.RECORD, writeSchemaNoOp.getType());
+
+ // Should have different number of fields
+ assertNotNull(writeSchemaNoOp.getFields());
Review Comment:
yes, I agree
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
@the-other-tim-brown As we are using composition pattern (and not inheritance), the order would need to be supported. right ? Anyways, when you take over this PR, can you look into if this is needed and remove it if not needed.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "danny0405 (via GitHub)" <gi...@apache.org>.
danny0405 commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3544720269
> we can break away from the avro implementation backing these objects entirely
@the-other-tim-brown Sounds like the elimination of Avro backing impl would be in the near last step of the migration, so we gonna finally get rid of the avro dependency anyway, is that what you mean?
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560590218
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599)
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2543238781
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaType.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of all schema types supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Type to provide a consistent interface
+ * for schema type handling while maintaining binary compatibility with Avro.
+ * Each Hudi schema type corresponds directly to an Avro schema type.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * HoodieSchemaType type = HoodieSchemaType.STRING;
+ * Schema.Type avroType = type.toAvroType();
+ * HoodieSchemaType backToHudi = HoodieSchemaType.fromAvroType(avroType);
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieSchemaType {
Review Comment:
Added GH issue : https://github.com/apache/hudi/issues/14263?issue=apache%7Chudi%7C14302
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3542378434
@danny0405 the main point of this initial PR is to serve as a way to start migrating to a new schema that will allow us to achieve all of the points raised. We must start somewhere and provide a way to begin the migration. The avro schema is used directly in many places today so this PR is setting up a replacement that can be swapped into these places without breaking functionality. After this is merged, it is then possible for multiple contributors to take on that migration work in parallel. And then after that, we can break away from the avro implementation backing these objects entirely which provides the flexibility to add new types like variant and blob.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548018818
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
+
+ // Prevent instantiation
+ private HoodieSchemaCompatibility() {
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema for write operations.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() but operates on HoodieSchemas.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ Collections.emptySet()); // Default to no partition columns
Review Comment:
Makes sense, it is updated in the latest commit.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "yihua (via GitHub)" <gi...@apache.org>.
yihua commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2547892369
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
+
+ // Prevent instantiation
+ private HoodieSchemaCompatibility() {
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema for write operations.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() but operates on HoodieSchemas.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ Collections.emptySet()); // Default to no partition columns
Review Comment:
Should this call `checkSchemaCompatible(tableSchema, writerSchema, shouldValidate, allowProjection, Collections.emptySet())` directly to avoid duplication?
##########
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:
Replace this with the wrapper class `new Parser().parse(jsonSchema)` so the usage is Avro-agnostic?
##########
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());
+ }
+ }
+
+ /**
+ * Creates a schema for the specified primitive type.
+ *
+ * @param type the primitive schema type
+ * @return new HoodieSchema for the primitive type
+ * @throws IllegalArgumentException if type is not a primitive type
+ */
+ public static HoodieSchema create(HoodieSchemaType type) {
+ ValidationUtils.checkArgument(type != null, "Schema type cannot be null");
+ ValidationUtils.checkArgument(type.isPrimitive(), "Only primitive types are supported: " + type);
+
+ Schema.Type avroType = type.toAvroType();
+ ValidationUtils.checkState(avroType != null, "Converted Avro type cannot be null");
+
+ Schema avroSchema = Schema.create(avroType);
+ return new HoodieSchema(avroSchema);
+ }
+
+ /**
+ * Creates a nullable schema (union of null and the specified schema).
+ *
+ * @param schema the schema to make nullable
+ * @return new HoodieSchema representing a nullable version of the input schema
+ */
+ public static HoodieSchema createNullable(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+
+ Schema inputAvroSchema = schema.avroSchema;
+ ValidationUtils.checkState(inputAvroSchema != null, "Input schema's Avro schema cannot be null");
+
+ if (schema.getType() == HoodieSchemaType.UNION) {
+ // Already a union, check if it contains null
+ List<Schema> unionTypes = inputAvroSchema.getTypes();
+ boolean hasNull = unionTypes.stream().anyMatch(s -> s.getType() == Schema.Type.NULL);
+
+ if (hasNull) {
+ return schema; // Already nullable
+ }
+
+ // Add null to existing union
+ List<Schema> newUnionTypes = new ArrayList<>();
+ newUnionTypes.add(Schema.create(Schema.Type.NULL));
+ newUnionTypes.addAll(unionTypes);
+ Schema nullableSchema = Schema.createUnion(newUnionTypes);
+ return new HoodieSchema(nullableSchema);
+ } else {
+ // Create new union with null
+ List<Schema> unionTypes = new ArrayList<>();
+ unionTypes.add(Schema.create(Schema.Type.NULL));
+ unionTypes.add(inputAvroSchema);
+ Schema nullableSchema = Schema.createUnion(unionTypes);
+ return new HoodieSchema(nullableSchema);
+ }
+ }
+
+ /**
+ * Creates an array schema with the specified element schema.
+ *
+ * @param elementSchema the schema for array elements
+ * @return new HoodieSchema representing an array
+ */
+ public static HoodieSchema createArray(HoodieSchema elementSchema) {
+ ValidationUtils.checkArgument(elementSchema != null, "Element schema cannot be null");
+
+ Schema elementAvroSchema = elementSchema.avroSchema;
+ ValidationUtils.checkState(elementAvroSchema != null, "Element schema's Avro schema cannot be null");
+
+ Schema arraySchema = Schema.createArray(elementAvroSchema);
+ return new HoodieSchema(arraySchema);
+ }
+
+ /**
+ * Creates a map schema with the specified value schema.
+ *
+ * @param valueSchema the schema for map values
+ * @return new HoodieSchema representing a map
+ */
+ public static HoodieSchema createMap(HoodieSchema valueSchema) {
+ ValidationUtils.checkArgument(valueSchema != null, "Value schema cannot be null");
+
+ Schema valueAvroSchema = valueSchema.avroSchema;
+ ValidationUtils.checkState(valueAvroSchema != null, "Value schema's Avro schema cannot be null");
+
+ Schema mapSchema = Schema.createMap(valueAvroSchema);
+ return new HoodieSchema(mapSchema);
+ }
+
+ /**
+ * Creates a record schema with the specified properties.
+ *
+ * @param name the record name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String namespace, String doc, List<HoodieSchemaField> fields) {
+ return createRecord(name, doc, namespace, false, fields);
+ }
+
+ /**
+ * Creates a record schema with the specified properties, including error flag.
+ *
+ * @param name the record name
+ * @param doc the documentation (can be null)
+ * @param namespace the namespace (can be null)
+ * @param isError whether this is an error record
+ * @param fields the list of fields
+ * @return new HoodieSchema representing a record
+ */
+ public static HoodieSchema createRecord(String name, String doc, String namespace, boolean isError, List<HoodieSchemaField> fields) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Record name cannot be null or empty");
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ // Convert HoodieSchemaFields to Avro Fields
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ Schema recordSchema = Schema.createRecord(name, doc, namespace, isError);
+ recordSchema.setFields(avroFields);
+ return new HoodieSchema(recordSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas.
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(List<HoodieSchema> schemas) {
+ ValidationUtils.checkArgument(schemas != null && !schemas.isEmpty(), "Union schemas cannot be null or empty");
+
+ List<Schema> avroSchemas = schemas.stream()
+ .map(HoodieSchema::getAvroSchema)
+ .collect(Collectors.toList());
+
+ Schema unionSchema = Schema.createUnion(avroSchemas);
+ return new HoodieSchema(unionSchema);
+ }
+
+ /**
+ * Creates a union schema from multiple schemas (varargs version).
+ *
+ * @param schemas the schemas to include in the union
+ * @return new HoodieSchema representing the union
+ */
+ public static HoodieSchema createUnion(HoodieSchema... schemas) {
+ ValidationUtils.checkArgument(schemas != null && schemas.length > 0, "Union schemas cannot be null or empty");
+ return createUnion(Arrays.asList(schemas));
+ }
+
+ /**
+ * Creates an enum schema with the specified properties.
+ *
+ * @param name the enum name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param symbols the list of enum symbols
+ * @return new HoodieSchema representing an enum
+ */
+ public static HoodieSchema createEnum(String name, String namespace, String doc, List<String> symbols) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Enum name cannot be null or empty");
+ ValidationUtils.checkArgument(symbols != null && !symbols.isEmpty(), "Enum symbols cannot be null or empty");
+
+ Schema enumSchema = Schema.createEnum(name, doc, namespace, symbols);
+ return new HoodieSchema(enumSchema);
+ }
+
+ /**
+ * Creates a fixed schema with the specified properties.
+ *
+ * @param name the fixed type name
+ * @param namespace the namespace (can be null)
+ * @param doc the documentation (can be null)
+ * @param size the size in bytes
+ * @return new HoodieSchema representing a fixed type
+ */
+ public static HoodieSchema createFixed(String name, String namespace, String doc, int size) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Fixed name cannot be null or empty");
+ ValidationUtils.checkArgument(size > 0, "Fixed size must be positive: " + size);
+
+ Schema fixedSchema = Schema.createFixed(name, doc, namespace, size);
+ return new HoodieSchema(fixedSchema);
+ }
+
+ /**
+ * Returns the Hudi schema version information.
+ *
+ * @return version string of the Hudi schema system
+ */
+ public static String getHudiSchemaVersion() {
+ Package pkg = HoodieSchema.class.getPackage();
+ String version = pkg != null ? pkg.getImplementationVersion() : null;
+ return version != null ? version : "unknown";
+ }
+
+ /**
+ * Creates a Hudi write schema from a given schema string with optional operation field.
+ * This is equivalent to HoodieAvroUtils.createHoodieWriteSchema() but returns HoodieSchema.
+ *
+ * @param schemaStr the schema string to convert
+ * @param withOperationField whether to include operation field metadata
+ * @return HoodieSchema configured for write operations
+ * @throws IllegalArgumentException if schema string is invalid
+ */
+ public static HoodieSchema createHoodieWriteSchema(String schemaStr, boolean withOperationField) {
+ ValidationUtils.checkArgument(schemaStr != null && !schemaStr.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+ Schema avroSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaStr, withOperationField);
+ return HoodieSchema.fromAvroSchema(avroSchema);
+ }
+
+ /**
+ * Adds metadata fields to an existing HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.addMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the base schema to add metadata fields to
+ * @param withOperationField whether to include operation field metadata
+ * @return new HoodieSchema with metadata fields added
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema addMetadataFields(HoodieSchema schema, boolean withOperationField) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.addMetadataFields(avroSchema, withOperationField);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Removes metadata fields from a HoodieSchema.
+ * This is equivalent to HoodieAvroUtils.removeMetadataFields() but operates on HoodieSchemas.
+ *
+ * @param schema the schema to remove metadata fields from
+ * @return new HoodieSchema without metadata fields
+ * @throws IllegalArgumentException if schema is null
+ */
+ public static HoodieSchema removeMetadataFields(HoodieSchema schema) {
+ ValidationUtils.checkArgument(schema != null, "Schema cannot be null");
+ Schema avroSchema = schema.toAvroSchema();
+ Schema resultAvro = HoodieAvroUtils.removeMetadataFields(avroSchema);
+ return HoodieSchema.fromAvroSchema(resultAvro);
+ }
+
+ /**
+ * Creates a nullable union (null + specified type).
+ *
+ * @param type the non-null type
+ * @return new HoodieSchema representing a nullable union
+ */
+ public static HoodieSchema createNullableSchema(HoodieSchema type) {
+ ValidationUtils.checkArgument(type != null, "Type cannot be null");
+
+ HoodieSchema nullSchema = HoodieSchema.create(HoodieSchemaType.NULL);
+ return createUnion(nullSchema, type);
+ }
+
+ /**
+ * Returns the type of this schema.
+ *
+ * @return the schema type
+ */
+ public HoodieSchemaType getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this schema, if it has one.
+ *
+ * @return Option containing the schema name, or Option.empty() if none
+ */
+ public Option<String> getName() {
+ return Option.ofNullable(avroSchema.getName());
+ }
+
+ /**
+ * Returns the namespace of this schema, if it has one.
+ *
+ * @return Option containing the schema namespace, or Option.empty() if none
+ */
+ public Option<String> getNamespace() {
+ return Option.ofNullable(avroSchema.getNamespace());
+ }
+
+ /**
+ * Returns the full name of this schema (namespace + name).
+ *
+ * @return Option containing the full schema name, or Option.empty() if none
+ */
+ public Option<String> getFullName() {
+ return Option.ofNullable(avroSchema.getFullName());
+ }
+
+ /**
+ * Returns the documentation string for this schema, if any.
+ *
+ * @return Option containing the documentation, or Option.empty() if none
+ */
+ public Option<String> getDoc() {
+ return Option.ofNullable(avroSchema.getDoc());
+ }
+
+ /**
+ * Returns the fields of this record schema.
+ *
+ * @return list of HoodieSchemaField objects
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public List<HoodieSchemaField> getFields() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get fields from non-record schema: " + type);
+ }
+
+ return avroSchema.getFields().stream()
+ .map(HoodieSchemaField::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Sets the fields for this record schema.
+ *
+ * @param fields the list of fields to set
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public void setFields(List<HoodieSchemaField> fields) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot set fields on non-record schema: " + type);
+ }
+ ValidationUtils.checkArgument(fields != null, "Fields cannot be null");
+
+ List<Schema.Field> avroFields = fields.stream()
+ .map(HoodieSchemaField::getAvroField)
+ .collect(Collectors.toList());
+
+ avroSchema.setFields(avroFields);
+ }
+
+ /**
+ * Returns the field with the specified name.
+ *
+ * @param name the field name to look up
+ * @return Option containing the field, or Option.empty() if not found
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public Option<HoodieSchemaField> getField(String name) {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot get field from non-record schema: " + type);
+ }
+
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+
+ Schema.Field avroField = avroSchema.getField(name);
+ return avroField != null ? Option.of(new HoodieSchemaField(avroField)) : Option.empty();
+ }
+
+ /**
+ * Returns the element schema for array types.
+ *
+ * @return the element schema
+ * @throws IllegalStateException if this is not an array schema
+ */
+ public HoodieSchema getElementType() {
+ if (type != HoodieSchemaType.ARRAY) {
+ throw new IllegalStateException("Cannot get element type from non-array schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getElementType());
+ }
+
+ /**
+ * Returns the value schema for map types.
+ *
+ * @return the value schema
+ * @throws IllegalStateException if this is not a map schema
+ */
+ public HoodieSchema getValueType() {
+ if (type != HoodieSchemaType.MAP) {
+ throw new IllegalStateException("Cannot get value type from non-map schema: " + type);
+ }
+
+ return new HoodieSchema(avroSchema.getValueType());
+ }
+
+ /**
+ * Returns the schemas in a union type.
+ *
+ * @return list of schemas in the union
+ * @throws IllegalStateException if this is not a union schema
+ */
+ public List<HoodieSchema> getTypes() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get types from non-union schema: " + type);
+ }
+
+ return avroSchema.getTypes().stream()
+ .map(HoodieSchema::new)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * Returns the size of a fixed schema.
+ *
+ * @return the fixed size in bytes
+ * @throws IllegalStateException if this is not a fixed schema
+ */
+ public int getFixedSize() {
+ if (type != HoodieSchemaType.FIXED) {
+ throw new IllegalStateException("Cannot get fixed size from non-fixed schema: " + type);
+ }
+
+ return avroSchema.getFixedSize();
+ }
+
+ /**
+ * Returns the symbols for an enum schema.
+ *
+ * @return list of enum symbols
+ * @throws IllegalStateException if this is not an enum schema
+ */
+ public List<String> getEnumSymbols() {
+ if (type != HoodieSchemaType.ENUM) {
+ throw new IllegalStateException("Cannot get symbols from non-enum schema: " + type);
+ }
+
+ return new ArrayList<>(avroSchema.getEnumSymbols());
+ }
+
+ /**
+ * Returns custom properties attached to this schema.
+ *
+ * @return map of custom properties
+ */
+ public Map<String, Object> getObjectProps() {
+ return avroSchema.getObjectProps();
+ }
+
+ /**
+ * Returns the value of a custom property.
+ *
+ * @param key the property key
+ * @return the property value, or null if not found
+ */
+ public Object getProp(String key) {
+ return avroSchema.getProp(key);
+ }
+
+ /**
+ * Adds a custom property to this schema.
+ *
+ * @param key the property key
+ * @param value the property value
+ */
+ public void addProp(String key, Object value) {
+ ValidationUtils.checkArgument(key != null && !key.isEmpty(), "Property key cannot be null or empty");
+ avroSchema.addProp(key, value);
+ }
+
+ /**
+ * Checks if this is an error record schema.
+ *
+ * @return true if this is an error record
+ * @throws IllegalStateException if this is not a record schema
+ */
+ public boolean isError() {
+ if (type != HoodieSchemaType.RECORD) {
+ throw new IllegalStateException("Cannot check error flag on non-record schema: " + type);
+ }
+ return avroSchema.isError();
+ }
+
+ /**
+ * Checks if this schema is nullable (union containing null).
+ *
+ * @return true if the schema is nullable
+ */
+ public boolean isNullable() {
+ if (type != HoodieSchemaType.UNION) {
+ return false;
+ }
+
+ return avroSchema.getTypes().stream()
+ .anyMatch(schema -> schema.getType() == Schema.Type.NULL);
+ }
+
+ /**
+ * If this is a union schema, returns the non-null type.
+ *
+ * @return the non-null schema from a union
+ * @throws IllegalStateException if this is not a nullable union
+ */
+ public HoodieSchema getNonNullType() {
+ if (type != HoodieSchemaType.UNION) {
+ throw new IllegalStateException("Cannot get non-null type from non-union schema: " + type);
+ }
+
+ List<Schema> types = avroSchema.getTypes();
+ Schema nonNullType = types.stream()
+ .filter(schema -> schema.getType() != Schema.Type.NULL)
+ .findFirst()
+ .orElseThrow(() -> new IllegalStateException("Union does not contain non-null type"));
+
+ return new HoodieSchema(nonNullType);
+ }
+
+ /**
+ * Returns the underlying Avro schema for compatibility purposes.
+ *
+ * <p>This method is provided for gradual migration and should be used
+ * sparingly. New code should prefer the HoodieSchema API.</p>
+ *
+ * @return the wrapped Avro Schema
+ */
+ public Schema getAvroSchema() {
+ return avroSchema;
+ }
+
+ /**
+ * Converts this HoodieSchema to an Avro Schema.
+ * This is an alias for getAvroSchema() provided for API consistency.
+ *
+ * @return the equivalent Avro Schema
+ */
+ public Schema toAvroSchema() {
+ return getAvroSchema();
+ }
+
+ /**
+ * Returns the JSON representation of this schema.
+ *
+ * @return JSON schema string
+ */
+ public String toString() {
+ return avroSchema.toString();
+ }
+
+ /**
+ * Returns a pretty-printed JSON representation of this schema.
+ *
+ * @return pretty-printed JSON schema string
+ */
+ public String toString(boolean pretty) {
+ return avroSchema.toString(pretty);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ HoodieSchema that = (HoodieSchema) obj;
+ return Objects.equals(avroSchema, that.avroSchema);
+ }
+
+ // Static utility methods for table schema operations
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(avroSchema);
+ }
+
+ /**
+ * Parser class for parsing JSON schemas, equivalent to Avro's Schema.Parser.
+ *
+ * <p>This provides a Hudi-specific way to parse JSON schema strings while
+ * maintaining compatibility with Avro's parsing behavior.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * HoodieSchema schema = new HoodieSchema.Parser().parse(jsonSchemaString);
+ * }</pre></p>
+ */
+ public static class Parser {
+
+ private final Schema.Parser avroParser;
+
+ /**
+ * Creates a new HoodieSchema Parser.
+ */
+ public Parser() {
+ this.avroParser = new Schema.Parser();
+ }
+
+ /**
+ * Parses a JSON schema string.
+ *
+ * @param jsonSchema the JSON schema string to parse
+ * @return parsed HoodieSchema
+ * @throws HoodieAvroSchemaException if the schema string is invalid
+ */
+ public HoodieSchema parse(String jsonSchema) {
+ ValidationUtils.checkArgument(jsonSchema != null && !jsonSchema.trim().isEmpty(),
+ "Schema string cannot be null or empty");
+
+ try {
+ Schema avroSchema = avroParser.parse(jsonSchema);
+ return new HoodieSchema(avroSchema);
+ } catch (Exception e) {
+ throw new HoodieAvroSchemaException("Failed to parse schema: " + jsonSchema + ". Error: " + e.getMessage());
Review Comment:
nit: same here on wrapping `e` in the `HoodieAvroSchemaException`
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "yihua (via GitHub)" <gi...@apache.org>.
yihua commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2547898812
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
+
+ // Prevent instantiation
+ private HoodieSchemaCompatibility() {
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema for write operations.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() but operates on HoodieSchemas.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ Collections.emptySet()); // Default to no partition columns
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema, excluding specified partition columns.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() with partition column exclusions.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @param partitionColumns set of partition column names to exclude from compatibility checks
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection,
+ Set<String> partitionColumns) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ if (!shouldValidate) {
+ return;
+ }
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ partitionColumns != null ? partitionColumns : Collections.emptySet());
+ }
+
+ /**
+ * Validates that the incoming schema represents a valid evolution of the table schema.
+ * This is equivalent to AvroSchemaUtils.checkValidEvolution() but operates on HoodieSchemas.
+ *
+ * @param incomingSchema the new schema being introduced
+ * @param tableSchema the existing table schema
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if evolution is invalid
+ */
+ public static void checkValidEvolution(HoodieSchema incomingSchema, HoodieSchema tableSchema) {
+ ValidationUtils.checkArgument(incomingSchema != null, "Incoming schema cannot be null");
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for evolution validation
+ AvroSchemaUtils.checkValidEvolution(incomingSchema.toAvroSchema(), tableSchema.toAvroSchema());
+ }
+
+ /**
+ * Checks if two schemas are compatible in terms of data reading.
+ * This uses the same logic as AvroSchemaUtils.isSchemaCompatible() but for HoodieSchemas.
+ *
+ * @param readerSchema the schema used to read the data
+ * @param writerSchema the schema used to write the data
+ * @return true if reader schema can read data written with writer schema
+ * @throws IllegalArgumentException if schemas are null
+ */
+ public static boolean isSchemaCompatible(HoodieSchema readerSchema, HoodieSchema writerSchema) {
+ ValidationUtils.checkArgument(readerSchema != null, "Reader schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Use HoodieSchemaUtils delegation for consistency
+ return AvroSchemaUtils.isSchemaCompatible(readerSchema.toAvroSchema(), writerSchema.toAvroSchema());
Review Comment:
`isSchemaCompatible(readerSchema, writerSchema, true)` to avoid duplication?
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
Review Comment:
```suggestion
private final HoodieSchema fieldSchema;
```
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = HoodieSchema.fromAvroSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
Review Comment:
There is a compatibility issue with `new Schema.Field` constructor on Avro 1.12.0 used by Spark 4.0, so `HoodieAvroUtils.createNewSchemaField` needs to be used which handles the compatibility issue: https://github.com/apache/hudi/blob/89f622f8244974c56c6c9ec72377a4c3f7bec3f2/hudi-common/src/main/java/org/apache/hudi/avro/HoodieAvroUtils.java#L356
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.common.util.Option;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import org.apache.avro.Schema;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Wrapper class for Avro Schema.Field that provides Hudi-specific field functionality
+ * while maintaining binary compatibility with Avro.
+ *
+ * <p>This class encapsulates an Avro Schema.Field and provides a consistent interface
+ * for field operations across the Hudi codebase. It maintains full compatibility with
+ * Avro by delegating all operations to the underlying Avro field.</p>
+ *
+ * <p>Usage example:
+ * <pre>{@code
+ * // Create from Avro field
+ * Schema.Field avroField = new Schema.Field("name", Schema.create(Schema.Type.STRING));
+ * HoodieSchemaField hoodieField = HoodieSchemaField.fromAvroField(avroField);
+ *
+ * // Access field properties
+ * String name = hoodieField.name();
+ * HoodieSchema schema = hoodieField.schema();
+ * Option<Object> defaultValue = hoodieField.defaultVal();
+ * }</pre></p>
+ *
+ * @since 1.2.0
+ */
+public class HoodieSchemaField implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Schema.Field avroField;
+ private final HoodieSchema hoodieSchema;
+
+ /**
+ * Creates a new HoodieSchemaField wrapping the given Avro field.
+ *
+ * @param avroField the Avro field to wrap, cannot be null
+ */
+ public HoodieSchemaField(Schema.Field avroField) {
+ ValidationUtils.checkArgument(avroField != null, "Avro field cannot be null");
+ this.avroField = avroField;
+ this.hoodieSchema = HoodieSchema.fromAvroSchema(avroField.schema());
+ }
+
+ /**
+ * Factory method to create HoodieSchemaField from an Avro field.
+ *
+ * @param avroField the Avro field to wrap
+ * @return new HoodieSchemaField instance
+ * @throws IllegalArgumentException if avroField is null
+ */
+ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
+ return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified properties, including field order.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string, can be null
+ * @param defaultVal the default value, can be null
+ * @param order the field order for sorting
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
+ ValidationUtils.checkArgument(order != null, "Field order cannot be null");
+
+ Schema avroSchema = schema.getAvroSchema();
+ ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
+
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ return new HoodieSchemaField(avroField);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name and schema.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema) {
+ return of(name, schema, null, null);
+ }
+
+ /**
+ * Creates a new HoodieSchemaField with the specified name, schema, and doc.
+ *
+ * @param name the name of the field
+ * @param schema the schema of the field
+ * @param doc the documentation string
+ * @return new HoodieSchemaField instance
+ */
+ public static HoodieSchemaField of(String name, HoodieSchema schema, String doc) {
+ return of(name, schema, doc, null);
+ }
+
+ /**
+ * Creates a metadata field for Hudi internal use.
+ * This is a convenience method for creating fields that are part of Hudi's metadata.
+ *
+ * @param name the metadata field name
+ * @param schema the metadata field schema
+ * @return new HoodieSchemaField configured as a metadata field
+ * @throws IllegalArgumentException if name is null/empty or schema is null
+ */
+ public static HoodieSchemaField createMetadataField(String name, HoodieSchema schema) {
+ ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Metadata field name cannot be null or empty");
+ ValidationUtils.checkArgument(schema != null, "Metadata field schema cannot be null");
+
+ return HoodieSchemaField.of(name, schema, "Hudi metadata field: " + name, HoodieJsonProperties.NULL_VALUE);
+ }
+
+ /**
+ * Returns the name of this field.
+ *
+ * @return the field name
+ */
+ public String name() {
+ return avroField.name();
+ }
+
+ /**
+ * Returns the schema of this field.
+ *
+ * @return the field schema as HoodieSchema
+ */
+ public HoodieSchema schema() {
+ return hoodieSchema;
+ }
+
+ /**
+ * Returns the documentation string for this field.
+ *
+ * @return Option containing the documentation string, or Option.empty() if none
+ */
+ public Option<String> doc() {
+ return Option.ofNullable(avroField.doc());
+ }
+
+ /**
+ * Returns the default value for this field.
+ *
+ * @return Option containing the default value, or Option.empty() if none
+ */
+ public Option<Object> defaultVal() {
+ if (avroField != null && avroField.hasDefaultValue()) {
+ return Option.of(avroField.defaultVal());
+ }
+ return Option.empty();
+ }
+
+ /**
+ * Returns the sort order for this field.
+ *
+ * @return the field order
+ */
+ public HoodieFieldOrder order() {
+ return HoodieFieldOrder.fromAvroOrder(avroField.order());
+ }
+
+ /**
+ * Returns the position of this field within its enclosing record.
+ *
+ * @return the field position (0-based index)
+ */
+ public int pos() {
+ return avroField.pos();
+ }
+
+ /**
+ * Checks if this field has a default value.
+ *
+ * @return true if the field has a default value
+ */
+ public boolean hasDefaultValue() {
+ return avroField.hasDefaultValue();
+ }
+
+ /**
+ * Returns custom properties attached to this field.
+ *
+ * @return map of custom properties
+ */
+ public Map<String, Object> getObjectProps() {
+ return avroField.getObjectProps();
+ }
+
+ /**
+ * Returns the value of a custom property.
+ *
+ * @param key the property key
+ * @return the property value, or null if not found
+ */
+ public Object getProp(String key) {
+ return avroField.getProp(key);
+ }
+
+ /**
+ * Adds a custom property to this field.
+ *
+ * @param key the property key
+ * @param value the property value
+ */
+ public void addProp(String key, Object value) {
+ ValidationUtils.checkArgument(key != null && !key.isEmpty(), "Property key cannot be null or empty");
+ avroField.addProp(key, value);
+ }
+
+ /**
+ * Returns the underlying Avro field for compatibility purposes.
+ *
+ * <p>This method is provided for gradual migration and should be used
+ * sparingly. New code should prefer the HoodieSchemaField API.</p>
+ *
+ * @return the wrapped Avro Schema.Field
+ */
+ public Schema.Field getAvroField() {
+ return avroField;
+ }
+
+ /**
+ * Creates a copy of this field with a new name.
+ *
+ * @param newName the new name for the field
+ * @return new HoodieSchemaField with the specified name
+ */
+ public HoodieSchemaField withName(String newName) {
+ ValidationUtils.checkArgument(newName != null && !newName.isEmpty(), "Field name cannot be null or empty");
+
+ Schema.Field newAvroField = new Schema.Field(newName, avroField.schema(), avroField.doc(),
Review Comment:
Same on all constructor usage of `Schema.Field`
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "voonhous (via GitHub)" <gi...@apache.org>.
voonhous commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2542855161
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
+1, ordering here dictates how records are sorted and not ordering of fields/columns within a table:
i.e. schema with:
```json
{
"type": "record",
"name": "UserRecord",
"namespace": "com.example",
"fields": [
{"name": "score", "type": "int", "order": "descending"},
{"name": "lastName", "type": "string", "order": "ascending"},
{"name": "firstName", "type": "string"},
{"name": "internal_id", "type": "string", "order": "ignore"}
]
}
Gives a sort order when sort is performed on the data below:
```
Record score lastName firstName internal_id
A 100 Smith Alice X001
B 100 Jones Bob X002
C 90 Zeller Cathy
```
Don't think this is really required for now, we can add it in later if it's absolutely required.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "bvaradar (via GitHub)" <gi...@apache.org>.
bvaradar commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2543270952
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchemaField.java:
##########
@@ -245,36 +244,17 @@ public void testFieldToString() {
assertTrue(fieldString.contains("true")); // hasDefault
}
- @Test
- public void testFieldOrderHandling() {
- Schema stringSchema = Schema.create(Schema.Type.STRING);
-
- // Test different field orders
- Schema.Field ascendingField = new Schema.Field("asc", stringSchema, null, null, Schema.Field.Order.ASCENDING);
- Schema.Field descendingField = new Schema.Field("desc", stringSchema, null, null, Schema.Field.Order.DESCENDING);
- Schema.Field ignoreField = new Schema.Field("ignore", stringSchema, null, null, Schema.Field.Order.IGNORE);
-
- HoodieSchemaField hoodieAsc = new HoodieSchemaField(ascendingField);
- HoodieSchemaField hoodieDesc = new HoodieSchemaField(descendingField);
- HoodieSchemaField hoodieIgnore = new HoodieSchemaField(ignoreField);
-
- assertEquals(HoodieFieldOrder.ASCENDING, hoodieAsc.order());
- assertEquals(HoodieFieldOrder.DESCENDING, hoodieDesc.order());
- assertEquals(HoodieFieldOrder.IGNORE, hoodieIgnore.order());
- }
-
@Test
public void testFieldCreationWithOrder() {
HoodieSchema stringSchema = HoodieSchema.create(HoodieSchemaType.STRING);
// Test creation with explicit order
Review Comment:
Same here.
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaField.java:
##########
@@ -88,28 +88,13 @@ public static HoodieSchemaField fromAvroField(Schema.Field avroField) {
* @return new HoodieSchemaField instance
*/
public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal) {
- return of(name, schema, doc, defaultVal, HoodieFieldOrder.ASCENDING);
- }
-
- /**
- * Creates a new HoodieSchemaField with the specified properties, including field order.
- *
- * @param name the name of the field
- * @param schema the schema of the field
- * @param doc the documentation string, can be null
- * @param defaultVal the default value, can be null
- * @param order the field order for sorting
- * @return new HoodieSchemaField instance
- */
- public static HoodieSchemaField of(String name, HoodieSchema schema, String doc, Object defaultVal, HoodieFieldOrder order) {
ValidationUtils.checkArgument(name != null && !name.isEmpty(), "Field name cannot be null or empty");
ValidationUtils.checkArgument(schema != null, "Field schema cannot be null");
- ValidationUtils.checkArgument(order != null, "Field order cannot be null");
Schema avroSchema = schema.getAvroSchema();
ValidationUtils.checkState(avroSchema != null, "Schema's Avro schema cannot be null");
- Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal, order.toAvroOrder());
+ Schema.Field avroField = new Schema.Field(name, avroSchema, doc, defaultVal);
Review Comment:
Not passing the order here means there is delta between what the current code and new code. Lets not do that. Lets keep the order as-is.
##########
hudi-common/src/test/java/org/apache/hudi/common/schema/TestHoodieSchema.java:
##########
@@ -638,11 +638,6 @@ public void testCreateRecordWithInvalidParameters() {
assertThrows(IllegalArgumentException.class, () -> {
HoodieSchema.createRecord("TestRecord", null, null, null);
}, "Should throw exception for null fields");
-
- // Test empty fields
Review Comment:
@the-other-tim-brown : If the empty list is a valid test, case, then can you change it so that it is not throwing the exception instead of removing it.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "danny0405 (via GitHub)" <gi...@apache.org>.
danny0405 commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2540260554
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
If we want to inherit the notions from Avro, I prefer to put it as per field value, while I checked the definition of Iceberg [`NestedField`](https://github.com/apache/iceberg/blob/b6b89268de26b119817db9ce20b540baa349c541/api/src/main/java/org/apache/iceberg/types/Types.java#L862), there is no ordering definition:
```java
private NestedField(
boolean isOptional,
int id,
String name,
Type type,
String doc,
Object initialDefault,
Object writeDefault) {
```
If we have no usage of it, maybe we just simplify the `Field` abstraction to drop it from our impl and just put a default value when converted to Avro, and add it back if we have a real use case there.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2540057296
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
What does ordering do in Hudi? This shouldn't be hard to answer @bvaradar. I would appreciate straight forward answers instead of "I want some design pattern to be followed." These patterns are tools we can use but let's not blindly recreate functionality if it is not providing value to the project.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "danny0405 (via GitHub)" <gi...@apache.org>.
danny0405 commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3550218157
> commandeer
sure, thanks for the clarification.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3547898262
> > we can break away from the avro implementation backing these objects entirely
>
> @the-other-tim-brown For decoupling from Avro, you mean we gonna finally get rid of avro backing stuff and migrate to our own right?
@danny0405 yes that is correct. Once we have the foundational PR merged to build the abstraction, the work to translate from HoodieSchema directly to Spark's StructType, Parquet's MessageType, and any other schema can start in parallel to the migration to this new HoodieSchema.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3544012966
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "danny0405 (via GitHub)" <gi...@apache.org>.
danny0405 commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2540158392
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
@the-other-tim-brown I didn't see any usage of it as of now from my understanding, may be utilized for query optimizating after we introducing pk-sorted tables or LSM layout that requires sorting on columns.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "danny0405 (via GitHub)" <gi...@apache.org>.
danny0405 commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3550222054
> @danny0405 I can commandeer this. Can you please remove your block on the review.
>
> The goal for the PR is not to define the new type system. but to first clean up and prepare code to be able to easily.
sure, thanks for the clarification.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "balaji-varadarajan-ai (via GitHub)" <gi...@apache.org>.
balaji-varadarajan-ai commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2534807015
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieFieldOrder.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.avro.Schema;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Enumeration of field sort orders supported by Hudi schema system.
+ *
+ * <p>This enum wraps Avro's Schema.Field.Order to provide a consistent interface
+ * for field ordering while maintaining binary compatibility with Avro.</p>
+ *
+ * @since 1.2.0
+ */
+public enum HoodieFieldOrder {
Review Comment:
@the-other-tim-brown : THe goal is to encapsulate every avro call with Hudi Schema call. As we see there is reference to the order in the code, we have an HudiSchema equivalent.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2542134718
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchema.java:
##########
@@ -0,0 +1,931 @@
+/*
+ * 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 {
Review Comment:
Yes, that is correct
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3558978612
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579)
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548012794
##########
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:
Right now we don't have any other representation of the schema as a string. Once we get to that phase, we can replace this with a proper parser.
##########
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:
Fixed this in the latest commit
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548012794
##########
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:
Right now we don't have any other representation of the schema as a string. Once we get to that phase, we can replace this with a proper parser.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [HUDI-14263] New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560603213
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "SUCCESS",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3 Azure: [SUCCESS](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599)
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "the-other-tim-brown (via GitHub)" <gi...@apache.org>.
the-other-tim-brown commented on code in PR #14265:
URL: https://github.com/apache/hudi/pull/14265#discussion_r2548093632
##########
hudi-common/src/main/java/org/apache/hudi/common/schema/HoodieSchemaCompatibility.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.AvroSchemaUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Utility class for checking HoodieSchema compatibility and evolution rules.
+ * This provides HoodieSchema-native methods that delegate to existing AvroSchemaUtils
+ * functionality while maintaining the architectural separation.
+ *
+ * <p>This class handles schema compatibility checking, which is crucial for:
+ * <ul>
+ * <li>Table schema evolution validation</li>
+ * <li>Writer schema compatibility with table schema</li>
+ * <li>Projection compatibility for query optimization</li>
+ * <li>Metadata field handling during schema checks</li>
+ * </ul>
+ */
+public final class HoodieSchemaCompatibility {
+
+ // Prevent instantiation
+ private HoodieSchemaCompatibility() {
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema for write operations.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() but operates on HoodieSchemas.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ Collections.emptySet()); // Default to no partition columns
+ }
+
+ /**
+ * Checks if writer schema is compatible with table schema, excluding specified partition columns.
+ * This is equivalent to AvroSchemaUtils.checkSchemaCompatible() with partition column exclusions.
+ *
+ * @param tableSchema the table schema to check against
+ * @param writerSchema the writer schema to validate
+ * @param shouldValidate whether to perform compatibility validation
+ * @param allowProjection whether to allow projection (fewer fields in writer)
+ * @param partitionColumns set of partition column names to exclude from compatibility checks
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if schemas are incompatible
+ */
+ public static void checkSchemaCompatible(HoodieSchema tableSchema, HoodieSchema writerSchema,
+ boolean shouldValidate, boolean allowProjection,
+ Set<String> partitionColumns) {
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ if (!shouldValidate) {
+ return;
+ }
+
+ // Delegate to AvroSchemaUtils for the actual compatibility check
+ AvroSchemaUtils.checkSchemaCompatible(
+ tableSchema.toAvroSchema(),
+ writerSchema.toAvroSchema(),
+ shouldValidate,
+ allowProjection,
+ partitionColumns != null ? partitionColumns : Collections.emptySet());
+ }
+
+ /**
+ * Validates that the incoming schema represents a valid evolution of the table schema.
+ * This is equivalent to AvroSchemaUtils.checkValidEvolution() but operates on HoodieSchemas.
+ *
+ * @param incomingSchema the new schema being introduced
+ * @param tableSchema the existing table schema
+ * @throws IllegalArgumentException if schemas are null
+ * @throws org.apache.hudi.exception.SchemaCompatibilityException if evolution is invalid
+ */
+ public static void checkValidEvolution(HoodieSchema incomingSchema, HoodieSchema tableSchema) {
+ ValidationUtils.checkArgument(incomingSchema != null, "Incoming schema cannot be null");
+ ValidationUtils.checkArgument(tableSchema != null, "Table schema cannot be null");
+
+ // Delegate to AvroSchemaUtils for evolution validation
+ AvroSchemaUtils.checkValidEvolution(incomingSchema.toAvroSchema(), tableSchema.toAvroSchema());
+ }
+
+ /**
+ * Checks if two schemas are compatible in terms of data reading.
+ * This uses the same logic as AvroSchemaUtils.isSchemaCompatible() but for HoodieSchemas.
+ *
+ * @param readerSchema the schema used to read the data
+ * @param writerSchema the schema used to write the data
+ * @return true if reader schema can read data written with writer schema
+ * @throws IllegalArgumentException if schemas are null
+ */
+ public static boolean isSchemaCompatible(HoodieSchema readerSchema, HoodieSchema writerSchema) {
+ ValidationUtils.checkArgument(readerSchema != null, "Reader schema cannot be null");
+ ValidationUtils.checkArgument(writerSchema != null, "Writer schema cannot be null");
+
+ // Use HoodieSchemaUtils delegation for consistency
+ return AvroSchemaUtils.isSchemaCompatible(readerSchema.toAvroSchema(), writerSchema.toAvroSchema());
Review Comment:
Makes sense, I updated this in the latest commit.
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(schema): New Hudi Schema Class - Initial implementation. Also, Add new APIs based on current usage of Avro schema [hudi]
Posted by "hudi-bot (via GitHub)" <gi...@apache.org>.
hudi-bot commented on PR #14265:
URL: https://github.com/apache/hudi/pull/14265#issuecomment-3560723802
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "ed24939debdf5947e3ed61e03dcccb68b712eb6d",
"triggerType" : "PUSH"
}, {
"hash" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9579",
"triggerID" : "3cefa1a0b4df24fb533a6beb20d7232f6a0ecd2a",
"triggerType" : "PUSH"
}, {
"hash" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "0623f45c586c743a00936ec4b2f82b5a4c6ab3dd",
"triggerType" : "PUSH"
}, {
"hash" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "1cfe56d7b872434d820e468124fbf8b5ebee7263",
"triggerType" : "PUSH"
}, {
"hash" : "84676245ea31479283982dad221ba84768ccac4d",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "84676245ea31479283982dad221ba84768ccac4d",
"triggerType" : "PUSH"
}, {
"hash" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"status" : "DELETED",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9599",
"triggerID" : "81eb74f96ebea7f07b65e6ac2e79b3a30ea8b1a3",
"triggerType" : "PUSH"
}, {
"hash" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "88e7f4b5326d1dd54883205fe9497bba9bf9e5d6",
"triggerType" : "PUSH"
}, {
"hash" : "c61db0c0bb599a59ef32e531175f223884334a37",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605",
"triggerID" : "c61db0c0bb599a59ef32e531175f223884334a37",
"triggerType" : "PUSH"
}, {
"hash" : "3fabee1beb76f051d355511808f516b015866ef7",
"status" : "PENDING",
"url" : "https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606",
"triggerID" : "3fabee1beb76f051d355511808f516b015866ef7",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* ed24939debdf5947e3ed61e03dcccb68b712eb6d UNKNOWN
* 0623f45c586c743a00936ec4b2f82b5a4c6ab3dd UNKNOWN
* 1cfe56d7b872434d820e468124fbf8b5ebee7263 UNKNOWN
* 84676245ea31479283982dad221ba84768ccac4d UNKNOWN
* 88e7f4b5326d1dd54883205fe9497bba9bf9e5d6 UNKNOWN
* c61db0c0bb599a59ef32e531175f223884334a37 Azure: [FAILURE](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9605)
* 3fabee1beb76f051d355511808f516b015866ef7 Azure: [PENDING](https://dev.azure.com/apachehudi/a1a51da7-8592-47d4-88dc-fd67bed336bb/_build/results?buildId=9606)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
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: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org