Definição
Função definida pelo utilizador aplicada coluna a coluna.
Prefira funções nativas; a UDF impede otimizações do Catalyst.
Exemplos
1. Definir e usar
val cleanTaxId = udf((s: String) => if (s == null) null else s.replaceAll("[^0-9]", ""))
df.withColumn("tax_id", cleanTaxId($"tax_id_raw"))2. Registar para SQL
spark.udf.register("clean_tax_id", (s: String) => s.replaceAll("[^0-9]", ""))
spark.sql("SELECT clean_tax_id(tax_id_raw) FROM stg_customers")3. Equivalente nativo (preferível)
df.withColumn("tax_id", regexp_replace($"tax_id_raw", "[^0-9]", ""))