通过类型的大小写匹配,根据SparkSQL中的类型转换值

Rad*_*adu 9 scala apache-spark

是否可以匹配Scala中的参数类型?假设我有一个函数接收两个参数:a value和a type.我想使用模式匹配来进行类型转换.

像这样的东西:

datatype match {
  case IntegerType => return value.toInt
  case FloatType => return value.toFloat
  case StringType => return value
  case DecimalType(_,_) => return BigDecimal(value) // this is not working
  case _ => return strrepr
}
Run Code Online (Sandbox Code Playgroud)

这里DecimalType接受两个参数来指定精度所需的精度.它可以是例如:

org.apache.spark.sql.types.DecimalType = DecimalType(10,2)
Run Code Online (Sandbox Code Playgroud)

我尝试了几个选项,似乎没有任何工作:

  • 因为case DecimalType => return BigDecimal(value)我得到:

    error: pattern type is incompatible with expected type;
       found   : org.apache.spark.sql.types.DecimalType.type
       required: org.apache.spark.sql.types.DataType
       Note: if you intended to match against the class, try `case DecimalType(_,_)`
    
    Run Code Online (Sandbox Code Playgroud)
  • 因为case DecimalType(_,_) => return BigDecimal(value)我得到:

    error: result type Boolean of unapply defined in method unapply in object DecimalType does not conform to Option[_] or Boolean
    
    Run Code Online (Sandbox Code Playgroud)
  • 因为case DecimalType[_,_] => return BigDecimal(value)我得到:

    error: org.apache.spark.sql.types.DecimalType does not take type parameters
    
    Run Code Online (Sandbox Code Playgroud)

Kri*_*isP 5

问题是return您的代码中使用了。您说过您在某处的函数中使用了此代码段。该函数的返回类型是什么?显然,您打算有时是Integer,有时是String,有时是BigDecimal。但是,如果使用return,它将查找返回对象的类型以确定该函数的返回类型。通常,您应该强烈避免return在Scala代码中使用。返回函数主体中的最后一个评估值。使用a的唯一情况return是当您想强制在函数主体中的其他位置返回值时。但是,更好的方法是将返回对象保存在变量中,然后仅在函数体的最后一行求值该变量。永远不要使用return!

没有return它的作品

scala> val datatype = DecimalType(10, 2)
datatype: org.apache.spark.sql.types.DecimalType = DecimalType(10,2)

scala> val value = BigDecimal(10)
value: scala.math.BigDecimal = 10

scala> datatype match {case DecimalType(_,_) => value}
res150: scala.math.BigDecimal = 10
Run Code Online (Sandbox Code Playgroud)

**退货问题**

scala> def test = {datatype match {case DecimalType(_,_) => return value}}
<console>:138: error: method test has return statement; needs result type
       def test = {datatype match {case DecimalType(_,_) => return value}}

scala> def test:BigDecimal = {datatype match {case DecimalType(_,_) => return value}}
test: BigDecimal

scala> def test:DataType = {datatype match {case DecimalType(_,_) => return value}}
<console>:138: error: type mismatch;
 found   : scala.math.BigDecimal
 required: org.apache.spark.sql.types.DataType
       def test:DataType = {datatype match {case DecimalType(_,_) => return value}}

scala> def test3 = {datatype match {case DecimalType(_,_) => value}}
test3: scala.math.BigDecimal
Run Code Online (Sandbox Code Playgroud)


Bra*_*ley 5

原来只有DecimalType模式与零参数匹配:

  case DecimalType() => ...
Run Code Online (Sandbox Code Playgroud)

如果需要精度和小数位数,则必须定义案例的类型并手动提取它们:

datatype match {
  case dt: DecimalType =>
    val precision = dt.precision
    val scale = dt.scale
    ...
Run Code Online (Sandbox Code Playgroud)