Skip to content

Commit

Permalink
Cosmos Spark: Fixes conversion for MapType schema (#22291)
Browse files Browse the repository at this point in the history
* Missing toMap

* Adding test case
  • Loading branch information
ealsur committed Jun 16, 2021
1 parent 3b91c34 commit 77f6412
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit 77f6412

Please sign in to comment.