Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null check for Double/Int Value in Spark

I am new in Spark, How can I check for for Null value in Double and Int value in scala or Spark.

Like for String We can do like this :

val value = (FirstString.isEmpty()) match {
          case true => SecondString
          case _    => FirstString
        }

I searched for it a lot but i found only for String value. Can you please suggest me for other datatype as well.

Thanks in advance.

like image 741
Darshan Avatar asked Dec 31 '25 21:12

Darshan


2 Answers

null is only applicable for AnyRef (i.e non primitive types) types in Scala. AnyVal types can not be set to null.

For example:

// the below are AnyVal(s) and wont compile
val c: Char = null
val i: Int = null
val d: Double = null  

String is an AnyRef and so can be null:

// this is ok!
val c: String = null 

That's why pattern matching nulls to Int/Double types is not possible:

// wont compile!
null match {
        case a:Int => "is a null Int"
        case _ => "something else"
        }
like image 72
Samar Avatar answered Jan 02 '26 13:01

Samar


May be you can simply use Options. So like:

val d: Double = ...

val isNull = Option(d).isDefined

Or you can use pattern matching:

val d: Double = ...

Option(d) match {
  case Some(v) => use v
  case _ => you got Null
}
like image 20
codejitsu Avatar answered Jan 02 '26 14:01

codejitsu