I have a hive avro table where the avro schema is stored on s3 next to the avro files.
In the table definiton the avro.schema.url always points to the latest partition's _schema.avsc file which is always the lates schema. (Avro schemas are backward and forward compatible in a table)
When new data comes in, I always add a new partition where the avro.schema.url properties also set to the _schema.avsc which was used when it was added and of course I always update the table avro.schema.url property to the latest one.
Querying this table works fine until the schema evolves in a way that a new optional property is added in the middle.
When this happens then after the spark sql query the columns in the old partition gets mixed up and it shows the wrong data for the columns.
If I query the table with Hive then everything is perfectly fine and it gives me back the correct columns for the partitions which were created the old schema and for the new which was created the evolved schema.
Here is how I could reproduce with the doctors.avro example data in sql test suite.
- I have created two partition folder:
Here the first partition had data which was created with the schema before evolving and the second one had the evolved one. (the evolved schema is the same as in your testcase except I moved the extra_field column to the last from the second and I generated two lines of avro data with the evolved schema.
- I have created a hive table with the following command:
Here as you can see the table schema url points to the latest schema
3. I ran an msck repair table to pick up all the partitions.
Fyi: If I run my select * query from here then everything is fine and no columns switch happening.
4. Then I changed the first partition's avro.schema.url url to points to the schema which is under the partition folder (non-evolved one -> s3://somelocation/doctors/
Then if you ran a select * from default.spark_test then the columns will be mixed up (on the data below the first name column becomes the extra_field column. I guess because in the latest schema it is the second column):
If I try the same query from Hive and not from spark sql then everything is fine and it never switches the columns.