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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
