Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos Spark: Fixes conversion for MapType schema #22291

Merged
merged 3 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private object CosmosRowConverter
jsonNode.fields().asScala
.map(element => (
element.getKey,
convertToSparkDataType(map.valueType, element.getValue, schemaConversionMode)))
convertToSparkDataType(map.valueType, element.getValue, schemaConversionMode))).toMap
case (arrayNode: ArrayNode, array: ArrayType) =>
arrayNode.elements().asScala
.map(convertToSparkDataType(array.elementType, _, schemaConversionMode)).toArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,12 +705,25 @@ class CosmosRowConverterSpec extends UnitSpec with CosmosLoggingTrait {
val nestedObjectNode: ObjectNode = objectNode.putObject(colName1)
nestedObjectNode.put(colName1, colVal1)
objectNode.put(colName2, colVal2)

// with struct
val schema = StructType(Seq(StructField(colName1, StructType(Seq(StructField(colName1, StringType)))),
StructField(colName2, StringType)))
val row = CosmosRowConverter.fromObjectNodeToRow(schema, objectNode, SchemaConversionModes.Relaxed)
val asStruct = row.getStruct(0)
asStruct.getString(0) shouldEqual colVal1
row.getString(1) shouldEqual colVal2

// with map
val schemaWithMap = StructType(Seq(
StructField(colName1, MapType(keyType = StringType, valueType = StringType, valueContainsNull = false)),
StructField(colName2, StringType)))

val rowWithMap = CosmosRowConverter.fromObjectNodeToRow(schemaWithMap, objectNode, SchemaConversionModes.Relaxed)

val convertedMap = rowWithMap.getMap[String, String](0)
convertedMap(colName1) shouldEqual colVal1
rowWithMap.getString(1) shouldEqual colVal2
}

"raw in ObjectNode" should "translate to Row" in {
Expand Down