spark-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mich Talebzadeh <mich.talebza...@gmail.com>
Subject UDF on lpad
Date Thu, 25 Aug 2016 11:06:56 GMT
Hi,

This UDF on substring works

scala> val SubstrUDF = udf { (s: String, start: Int, end: Int) =>
s.substring(start, end) }
SubstrUDF: org.apache.spark.sql.expressions.UserDefinedFunction =
UserDefinedFunction(<function3>,StringType,Some(List(StringType,
IntegerType, IntegerType)))

I want something similar to this

scala> sql("""select lpad("str", 10, "0")""").show
+----------------+
|lpad(str, 10, 0)|
+----------------+
|      0000000str|
+----------------+

scala> val SubstrUDF = udf { (s: String, len: Int, chars: String) =>
lpad(s, len, chars) }
<console>:40: error: type mismatch;
 found   : String
 required: org.apache.spark.sql.Column
       val SubstrUDF = udf { (s: String, len: Int, chars: String) =>
lpad(s, len, chars) }


Any ideas?

Thanks

Dr Mich Talebzadeh



LinkedIn * https://www.linkedin.com/profile/view?id=AAEAAAAWh2gBxianrbJd6zP6AcPCCdOABUrV8Pw
<https://www.linkedin.com/profile/view?id=AAEAAAAWh2gBxianrbJd6zP6AcPCCdOABUrV8Pw>*



http://talebzadehmich.wordpress.com


*Disclaimer:* Use it at your own risk. Any and all responsibility for any
loss, damage or destruction of data or any other property which may arise
from relying on this email's technical content is explicitly disclaimed.
The author will in no case be liable for any monetary damages arising from
such loss, damage or destruction.

Mime
View raw message