Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Spark when function without otherwise but keep column values

I frequently find myself replacing values in columns using

when($"myCol".isNull,myCrazyFunction).otherwise($"myCol")

To me the .otherwise($"myCol") feels kind of redundant.

Is there a better way to replace some values under some condition and otherwise just leave everything as it is without using the otherwise?

like image 848
nik Avatar asked May 13 '26 21:05

nik


1 Answers

I think you can use coalesce() for that.

select(coalesce($"myCol", myCrazyFunction))

Just remember that myCrazyFunction should return a Column type.

like image 156
Travis Hegner Avatar answered May 16 '26 12:05

Travis Hegner