How can I get a higher order function in Scala to properly accept a spark filter predicate? I.e.
val df = Seq(1,2,3,4).toDF("value")
df.filter(col("value")> 2).show
df.filter(col("value")< 2).show
works just fine. But when I try to refactor it into a function which accepts a filter predicate (note: same signature as > operator) the compiler no longer finds the left/right part to submit to predicate.
def myFilter =(predicate:Any =>Column)(df:DataFrame)= {
df.filter(col("value") predicate 2).show // WARN this does not compile
}
df.transform(myFilter(>)).show
How can this be made to work?
a x bmeansa.x(b), socol("value") predicate 2meanscol("value").predicate(2)which is not what you want. Perhaps something more likepredicate(col("value"), 2)? - Timdf.filter(col("value").predicate(2))also does not work. - Georg Heilerpredicate(col("value"), 2). You also need to change the signature ofpredicate. I don't know enough about Spark to give the full answer (which is why I am using comments) but it should be something like(Column, Int) => Boolean. Then you pass_ > _rather than just>tomyFilter. - Tim