Skip to content
Open
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 @@ -5,8 +5,11 @@ import java.util.UUID

import com.datastax.spark.connector.ColumnRef
import com.datastax.spark.connector.cql.TableDef
import com.datastax.spark.connector.types.TypeConversionException
import org.apache.spark.sql.Row

import scala.util.{Failure, Success, Try}


/** A [[RowWriter]] that can write SparkSQL `Row` objects. */
class SqlRowWriter(val table: TableDef, val selectedColumns: IndexedSeq[ColumnRef])
Expand All @@ -23,9 +26,16 @@ class SqlRowWriter(val table: TableDef, val selectedColumns: IndexedSeq[ColumnRe
require(row.size == columnNames.size, s"Invalid row size: ${row.size} instead of ${columnNames.size}.")
for (i <- 0 until row.size) {
val colValue = row(i)
buffer(i) = converters(i).convert(colValue)
buffer(i) = Try(converters(i).convert(colValue)) match {
case Success(value) => value
case Failure(ex: IllegalArgumentException) =>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most exceptions I've seen that were hard to track down were either IllegalArgumentException or its subclass.

val columnType = columnTypes(i).scalaTypeName
throw new TypeConversionException(
s"Cannot convert value $colValue of column ${columnNames(i)} to type $columnType", ex)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message will look like Cannot convert value somevalue of column somecolumn to type Int without any wrapping of somevalue (like single or double quotes). Any suggestions on how to improve readability?

case Failure(ex) => throw ex
}
}
}
}


Expand Down