Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal Type with Precision Equivalent in Spark SQL

What is Spark SQL datatype Equivalent to DecimalType(2,9) in SQL?

For example: print(column.dataType==X) => should give me True. Where Column's datatype in SQL is DecimalType(2,9)

Tried: X= DataTypes.createDecimalType(2,9), works fine

I am looking for a generalized DecimalType Class to filter all DecimalType columns from the dataframe irrespective of the precision and scale.

like image 743
Karan Sharma Avatar asked May 22 '26 22:05

Karan Sharma


1 Answers

Each DecimalType type is an instance of DecimalType class:

from pyspark.sql.types import DecimalType

df = (spark
  .createDataFrame(["1.32"], "string")
  .selectExpr("CAST(value AS DECIMAL(4, 2)) AS value"))

isinstance(df.schema["value"].dataType, DecimalType)
# True
import org.apache.spark.sql.types._

val df = Seq("1.32").toDF("value")  
  .selectExpr("CAST(value AS DECIMAL(4, 2)) AS value")

df.schema("value").dataType match {
  case _: DecimalType => true
  case _ => false
}
// Boolean = true

Of course, you'll never encounter DECIMAL(2,9), as it is not possible to have scale greater than precision.

like image 116
Aaron Makubuya Avatar answered May 25 '26 22:05

Aaron Makubuya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!