in connector/src/main/scala/com/datastax/spark/connector/writer/TableWriter.scala [349:402]
private def checkCollectionBehaviors(table: TableDef, columnRefs: IndexedSeq[ColumnRef]) {
val tableCollectionColumns = table.columns.filter(cd => cd.isCollection)
val tableCollectionColumnNames = tableCollectionColumns.map(_.columnName)
val tableListColumnNames = tableCollectionColumns
.map(c => (c.columnName, c.columnType))
.collect { case (name, x: ListType[_]) => name }
val tableMapColumnNames = tableCollectionColumns
.map(c => (c.columnName, c.columnType))
.collect { case (name, x: MapType[_, _]) => name }
val refsWithCollectionBehavior = columnRefs collect {
case columnName: CollectionColumnName => columnName
}
val collectionBehaviorColumnNames = refsWithCollectionBehavior.map(_.columnName)
//Check for non-collection columns with a collection Behavior
val collectionBehaviorNormalColumn =
collectionBehaviorColumnNames.toSet -- tableCollectionColumnNames.toSet
if (collectionBehaviorNormalColumn.nonEmpty)
throw new IllegalArgumentException(
s"""Collection behaviors (add/remove/append/prepend) are only allowed on collection columns.
|Normal Columns with illegal behavior: ${collectionBehaviorNormalColumn.mkString}"""
.stripMargin
)
//Check that prepend is used only on lists
val prependBehaviorColumnNames = refsWithCollectionBehavior
.filter(_.collectionBehavior == CollectionPrepend)
.map(_.columnName)
val prependOnNonList = prependBehaviorColumnNames.toSet -- tableListColumnNames.toSet
if (prependOnNonList.nonEmpty)
throw new IllegalArgumentException(
s"""The prepend collection behavior only applies to Lists. Prepend used on:
|${prependOnNonList.mkString}""".stripMargin
)
//Check that remove is not used on Maps
val removeBehaviorColumnNames = refsWithCollectionBehavior
.filter(_.collectionBehavior == CollectionRemove)
.map(_.columnName)
val removeOnMap = removeBehaviorColumnNames.toSet & tableMapColumnNames.toSet
if (removeOnMap.nonEmpty)
throw new IllegalArgumentException(
s"The remove operation is currently not supported for Maps. Remove used on: ${removeOnMap
.mkString}"
)
}