Nikhil Utane
Nikhil Utane

Reputation: 1221

Spark SQL: Specify column name resulting from UDF in WHERE clause

I have written a UDF function which will return a column (0 or 1) after processing 2 columns. I need to have my select query such that it returns those records for which this value is 1. I wrote the query as below:

SELECT number, myUDF(col1, col2) as result
    FROM mytable 
    WHERE result is not null 

However it doesn't recognize the column name 'result'. Is there any special syntax needed so that it recognizes this new output column? Thanks.

Upvotes: 0

Views: 323

Answers (1)

G one
G one

Reputation: 2729

CASE statement should solve the problem here:

SELECT number, CASE when myUDF(col1, col2) = 1 then myUDF(col1, col2) END as result FROM mytable

Upvotes: 1

Related Questions