Like we have SQL ISNUMERIC Function which validates whether the expression is numeric or not , I need if there is any equivalent function in Spark SQL, I have tried to find it but couldn't get it. Please if someone can help or suggest for the same ?
- So, do you need to filter the Rows after made the SQL execution??Kenry Sanchez– Kenry Sanchez2019-06-23 18:15:46 +00:00Commented Jun 23, 2019 at 18:15
- Possible duplicate of Possible to filter Spark dataframe by ISNUMERIC function?Ged– Ged2019-06-23 18:18:11 +00:00Commented Jun 23, 2019 at 18:18
- 1Yes but, that solution is in scale. I guess he is wondering in Java. Well, he should specific which language.Kenry Sanchez– Kenry Sanchez2019-06-23 19:28:49 +00:00Commented Jun 23, 2019 at 19:28
- I need to use isnumeric function in Azure databricks where I am using spark sqlPushkar– Pushkar2019-07-07 19:16:21 +00:00Commented Jul 7, 2019 at 19:16
Add a comment |
2 Answers
For anyone coming here by way of Google :) , there is an alternative answer by regex for isnumeric in spark sql
select OldColumn, CASE WHEN OldColumn not rlike '[^0-9]' THEN 1 ELSE 0 END AS OldColumnIsNumeric from table The regex simply checks if the column is numeric or not.
You can modify to fit for substrings of the column you are checking too.
Comments
Try using spark udf, this approach will help you clone any function -
scala> spark.udf.register("IsNumeric", (inpColumn: Int) => BigInt(inpColumn).isInstanceOf[BigInt]) res46: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,BooleanType,Some(List(IntegerType))) scala> spark.sql(s""" select "ABC", IsNumeric(123) as IsNumeric_1 """).show(false) +---+-----------+ |ABC|IsNumeric_1| +---+-----------+ |ABC|true | +---+-----------+ scala> spark.sql(s""" select "ABC", IsNumeric("ABC") as IsNumeric_1 """).show(false) +---+-----------+ |ABC|IsNumeric_1| +---+-----------+ |ABC|null | +---+-----------+ Here, above function will return null if column value is not integer.
Hope this will be helpful.
2 Comments
Ged
In where clause as well?
Ajay Ahuja
Yes you can use spark udf in where clause.