zhedoubushishi opened a new pull request #1406: [HUDI-713] Fix conversion of Spark array of struct type to Avro schema URL: https://github.com/apache/incubator-hudi/pull/1406 ## *Tips* - *Thank you very much for contributing to Apache Hudi.* - *Please review https://hudi.apache.org/contributing.html before opening a pull request.* ## What is the purpose of the pull request With migration of Hudi to spark 2.4.4 and to using native spark-avro, there is an issue with conversion of array of struct fields because of the way spark-avro handles avro schema conversion vs databricks-avro. This issue is similar to #1223. Current struct namespace uses the [way](https://github.com/apache/incubator-hudi/blob/master/hudi-spark/src/main/scala/org/apache/hudi/AvroConversionHelper.scala#L346) how databricks/spark-avro does, the namespace for elements of an array will contain the array name. But for spark/spark-avro, each field in a struct has the same child [namespace](https://github.com/apache/spark/blob/v2.4.4/external/avro/src/main/scala/org/apache/spark/sql/avro/SchemaConverters.scala#L176). So the namespace for elements of an array won't contain the name of array but will contain the struct name. For example, the expected avro schema is: ``` { "type" : "record", "name" : "AWS_TEST_record", "namespace" : "hoodie.AWS_TEST", "fields" : [ { "name" : "offset", "type" : [ "long", "null" ] }, { "name" : "partition", "type" : [ "long", "null" ] }, { "name" : "value", "type" : [ { "type" : "record", "name" : "value", "namespace" : "hoodie.AWS_TEST.AWS_TEST_record", "fields" : [ { "name" : "prop1", "type" : [ "string", "null" ] }, { "name" : "prop2", "type" : [ { "type" : "array", "items" : [ { "type" : "record", "name" : "prop2", "namespace" : "hoodie.AWS_TEST.AWS_TEST_record.value", "fields" : [ { "name" : "withinProp1", "type" : [ "string", "null" ] }, { "name" : "withinProp2", "type" : [ "long", "null" ] } ] }, "null" ] }, "null" ] } ] }, "null" ] }, { "name" : "op_ts", "type" : [ "string", "null" ] }, { "name" : "year_partition", "type" : [ "int", "null" ] }, { "name" : "id", "type" : [ "string", "null" ] } ] } ``` The element of array has this namespace ```hoodie.AWS_TEST.AWS_TEST_record.value```, but in current Hudi code, Hudi would create a element with this namespace ```hoodie.AWS_TEST.AWS_TEST_record.prop2``` which would cause the ```not in union``` error. ## Brief change log - Fix conversion of Spark array of struct type to Avro schema - Modify the schema of data used in unit tests to have array of struct type data as well, so that any issue with array of struct type can be caught earlier ## Verify this pull request *(Please pick either of the following options)* This pull request is a trivial rework / code cleanup without any test coverage. *(or)* This pull request is already covered by existing tests, such as *(please describe tests)*. (or) This change added tests and can be verified as follows: *(example:)* - *Added integration tests for end-to-end.* - *Added HoodieClientWriteTest to verify the change.* - *Manually verified the change by running a job locally.* ## Committer checklist - [x] Has a corresponding JIRA in PR title & commit - [x] Commit message is descriptive of the change - [ ] CI is green - [ ] Necessary doc changes done or have another open PR - [ ] For large changes, please consider breaking it into sub-tasks under an umbrella JIRA.
---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
