Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ object SchemaConverters {
case MapType(StringType, vt, valueContainsNull) =>
builder.map().values(toAvroType(vt, valueContainsNull, recordName, prevNameSpace))
case st: StructType =>
val nameSpace = s"$prevNameSpace.$recordName"
val nameSpace = prevNameSpace match {
case "" => recordName
case _ => s"$prevNameSpace.$recordName"
}

val fieldsAssembler = builder.record(recordName).namespace(nameSpace).fields()
st.foreach { f =>
val fieldAvroType = toAvroType(f.dataType, f.nullable, f.name, nameSpace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,23 @@ class AvroSuite extends QueryTest with SharedSQLContext with SQLTestUtils {

case class NestedTop(id: Int, data: NestedMiddle)

test("Validate namespace in avro file that has nested records with the same name") {
withTempPath { dir =>
val writeDf = spark.createDataFrame(List(NestedTop(1, NestedMiddle(2, NestedBottom(3, "1")))))
writeDf.write.format("avro").save(dir.toString)
val file = new File(dir.toString)
.listFiles()
.filter(_.isFile)
.filter(_.getName.endsWith("avro"))
.head
val reader = new DataFileReader(file, new GenericDatumReader[Any]())
val schema = reader.getSchema.toString()
assert(schema.contains("\"namespace\":\"topLevelRecord\""))
assert(schema.contains("\"namespace\":\"topLevelRecord.data\""))
assert(schema.contains("\"namespace\":\"topLevelRecord.data.data\""))
}
}

test("saving avro that has nested records with the same name") {
withTempPath { tempDir =>
// Save avro file on output folder path
Expand Down