Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12708#discussion_r61822276
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala ---
    @@ -335,6 +358,19 @@ trait Row extends Serializable {
       def getAs[T](fieldName: String): T = getAs[T](fieldIndex(fieldName))
     
       /**
    +   * Returns the value of a given fieldName as a scala.util.Try object.
    +   */
    +  def attempt[T](fieldName: String): Try[T] = 
Try(getAs[T](fieldIndex(fieldName)))
    --- End diff --
    
    There are two types of row (external) `org.apache.spark.sql.Row` and 
`org.apache.spark.sql.catalyst.InternalRow`.  In general, I don't think that 
users consume Row objects that they constructed.  Instead I think they 
construct rows to pass in to Spark or they consume rows produced by Spark.  
Given that, I think this is a more realistic use case:
    
    ```scala
    import org.apache.spark.sql.types._
    val rows = sc.parallelize(Row.fromSeq("string" :: Nil) :: Nil)
    val schema = StructType(StructField("id", IntegerType) :: Nil)
    val df = sqlContext.createDataFrame(rows, schema)
    df.collect()
    ```
    
    This is going to fail before Spark returns the invalid row to you:
    
    ```
    java.lang.ClassCastException: java.lang.String cannot be cast to 
java.lang.Integer
        at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:106)
        at 
org.apache.spark.sql.catalyst.expressions.BaseGenericInternalRow$class.getInt(rows.scala:41)
        at 
org.apache.spark.sql.catalyst.expressions.GenericInternalRow.getInt(rows.scala:221)
        at 
org.apache.spark.sql.catalyst.CatalystTypeConverters$IntConverter$.toScalaImpl(CatalystTypeConverters.scala:363)
        at 
org.apache.spark.sql.catalyst.CatalystTypeConverters$IntConverter$.toScalaImpl(CatalystTypeConverters.scala:362)
        at 
org.apache.spark.sql.catalyst.CatalystTypeConverters$CatalystTypeConverter.toScala(CatalystTypeConverters.scala:110)
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to