Spark UDFs to migrate from other SQL dialects
Bill Schneider Jan 08, 2018
This article originally appeared on my blog
I found it helpful to create Spark UDFs to make it easier to migrate logic in SQL from another database like SQL Server.
SQL Server defines several string functions like
CHARINDEX, which are not available in Spark by default. Fortunately these are easy to implement in Spark with UDFs:
These are all thin wrappers around native Scala string functions. These will now be available for use in Spark SQL queries:
This makes it a little easier to copy-paste queries from SQL Server to Spark, if the syntax is otherwise standard.
The one downside is that Spark UDFs are functions, not methods, and as such do not allow for default argument values. So you would have to explicitly add a
0 as the third argument for
CHARINDEX wherever it's missing.