Github user culler commented on a diff in the pull request:
https://github.com/apache/spark/pull/3066#discussion_r19733162
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Row.scala
---
@@ -42,6 +44,31 @@ object Row {
* This method can be used to construct a [[Row]] from a [[Seq]] of
values.
*/
def fromSeq(values: Seq[Any]): Row = new GenericRow(values.toArray)
+
+ /**
+ * This method can be used to construct a [[Row]] from a [[Seq]] of
Strings,
+ * converting each item to the type specified in a [[StructType]] schema.
+ * Only primitive types can be used.
+ */
+ def fromStringsBySchema(strings: Seq[String], schema: StructType): Row =
{
+ val values = for {
+ (field, str) <- schema.fields zip strings
+ item = field.dataType match {
+ case IntegerType => str.toInt
+ case LongType => str.toLong
+ case DoubleType => str.toDouble
+ case FloatType => str.toFloat
+ case ByteType => str.toByte
+ case ShortType => str.toShort
+ case StringType => str
+ case BooleanType => (str != "")
+ case DateType => Date.valueOf(str)
+ case TimestampType => Timestamp.valueOf(str)
+ case DecimalType() => new BigDecimal(str)
+ }
+ } yield item
+ new GenericRow(values.toArray)
+ }
--- End diff --
It doesn't need to be a Row method. I will move it outside ( and worry
about the Boolean case.)
There is an issue lurking here, however. A SchemaRDD should have Rows
which match its schema. But mismatches generate no errors until the last
stage, e.g. when one collects the results of a query. My thought was that it
would be helpful to have a "safe" Row constructor which would check its data
against a schema and raise an error if they do not match (after applying
whatever implicit conversions are available. (Yes, this is a separate issue
and does not belong here.)
---
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]