Here is my sample code. I am expecting decimal(16,4) as return type from the UDF, but it is decimal(38,18).
Is there any better solution?
I am NOT expecting the answer "cast(price as decimal(16,4))", as I have some other business logic in my UDF other than just casting.
Thanks in advance.
import scala.util.Try
import org.apache.spark.sql.functions.udf
import org.apache.spark.sql.types.Decimal
val spark = SparkSession.builder().master("local[*]").appName("Test").getOrCreate()
import spark.implicits._
val stringToDecimal = udf((s:String, precision:Int, scale: Int) => {
Try(Decimal(BigDecimal(s), precision, scale)).toOption
})
spark.udf.register("stringToDecimal", stringToDecimal)
val inDf = Seq(
("1", "864.412"),
("2", "1.600"),
("3", "2,56")).toDF("id", "price")
val outDf = inDf.selectExpr("id", "stringToDecimal(price, 16, 4) as price")
outDf.printSchema()
outDf.show()
------------------output----------------
root
|-- id: string (nullable = true)
|-- price: decimal(38,18) (nullable = true)
+---+--------------------+
| id| price|
+---+--------------------+
| 1|864.4120000000000...|
| 2|1.600000000000000000|
| 3| null|
+---+--------------------+