Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark SQL Split with Period (.)

I encountered a problem in spark 2.2 while using pyspark sql, I tried to split a column with period (.) and it did not behave well even after providing escape chars:

>>> spark.sql("select split('a.aaa','.')").show()
+---------------+
|split(a.aaa, .)|
+---------------+
|   [, , , , , ]|
+---------------+

>>> spark.sql("select split('a.aaa','\\.')").show()
+---------------+
|split(a.aaa, .)|
+---------------+
|   [, , , , , ]|
+---------------+

>>> spark.sql("select split('a.aaa','[.]')").show()
+-----------------+
|split(a.aaa, [.])|
+-----------------+
|         [a, aaa]|
+-----------------+

It uses period only when we provide it like [.] while it should also be working with escape seq '\.'. Am I doing something wrong here ?

like image 514
some_user Avatar asked Jul 17 '26 15:07

some_user


2 Answers

Looks like you need to escape the \\:

spark.sql("""select split('a.aa', '\\\\.')""").show()

If you were to run it directly in SparkSQL it would just be

select split('a.aa', '\\.')
like image 172
Silvio Avatar answered Jul 24 '26 07:07

Silvio


As @swdev commented above you can just do this:

spark.sql("select SPLIT(value,'[.]') from demo").show
like image 22
German Schiavon Matteo Avatar answered Jul 24 '26 08:07

German Schiavon Matteo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!