Spark SQL case sensitive filter on pattern for column

Viewed 1637

How to use spark sql filter as a case sensitive filter on a column basis of a Pattern.

For example, I have a pattern:

'Aaaa AA'

And my column has data like this:

adaz
LssA ss 
Leds ST 
Pear QA 
Lear QA

I want to retrieve rows that have the "Aaaa AA" pattern, with respect to letter case. This means that the desired rows will be 'Leds ST' , 'Pear QA', 'Lear QA'.

"Aaaa AA" => 'Leds ST' , 'Pear QA', 'Lear QA'
"AaaA aa" => 'LssA ss'
"aaaa" => 'adaz'

How can I get this result using spark sql? Or can we write any regex sql query for this result ?

3 Answers

We can use the Spark SQL function translate() to create a grouping column for your strings.

With PySpark:

Sample dataframe for testing

from pyspark.sql.types import StringType

df = spark.createDataFrame(["adaz", "LssA ss", "Leds ST", "Pear QA","Lear QA"], StringType())

Actual transofrmation

from pyspark.sql.functions import translate, collect_list, col
import string

lowercases = string.ascii_lowercase
uppercases = string.ascii_uppercase
length_alphabet = len(uppercases)

ones = "1" * length_alphabet
zeroes = "0" * length_alphabet

old = uppercases + lowercases
new = ones + zeroes

df.withColumn("group", translate(df.value, old, new)) \
  .groupBy(col("group")).agg(collect_list(df.value).alias("strings")) \
  .show(truncate = False)

Result:

+-------+---------------------------+
|group  |strings                    |
+-------+---------------------------+
|1000 11|[Leds ST, Pear QA, Lear QA]|
|0000   |[adaz]                     |
|1001 00|[LssA ss]                  |
+-------+---------------------------+

With Scala Spark:

import org.apache.spark.sql.functions.{translate, col, collect_list}

val lower = 'a' to 'z'
val upper = 'A' to 'Z'
val length_alphabet = upper.size

val lowercases = lower.mkString("")
val uppercases = upper.mkString("")

val ones = "1" * length_alphabet
val zeroes = "0" * length_alphabet

val old = uppercases + lowercases
val news = ones + zeroes

df.withColumn("group", translate($"value", old, news)) 
  .groupBy(col("group")).agg(collect_list($"value").alias("strings")) 
  .show(truncate = false)

With "regexp_extract":

val df=List(
"adaz",
"LssA ss",
  "Leds ST",
  "Pear QA",
  "Lear QA"
).toDF("value")
df.filter(regexp_extract($"value","^[A-Z][a-z]{3} [A-Z]{2}$",0)=!=lit("")).show(false)

Output:

+-------+
|value  |
+-------+
|Leds ST|
|Pear QA|
|Lear QA|
+-------+

I'm extending on @pasha701.

scala> val df=List(
     | "adaz",
     | "LssA ss",
     |   "Leds ST",
     |   "Pear QA",
     |   "Lear QA"
     | ).toDF("value")
df: org.apache.spark.sql.DataFrame = [value: string]

scala> val df2= df.withColumn("reg1", regexp_extract($"value","^[A-Z][a-z]{3} [A-Z]{2}$",0)=!=lit("")).withColumn("reg2",regexp_extract($"value","^[a-z]{4}$",0)=!=lit("")).withColumn("reg3", regexp_extract($"value","^[A-Z][a-z]{2}[A-Z] [a-z]{2}$",0)=!=lit(""))
df2: org.apache.spark.sql.DataFrame = [value: string, reg1: boolean ... 2 more fields]

scala> val df3=df2.withColumn("reg_patt", when('reg1,"1000 11").when('reg2,"0000").when('reg3,"1001 00").otherwise("9"))
df3: org.apache.spark.sql.DataFrame = [value: string, reg1: boolean ... 3 more fields]

scala> df3.groupBy("reg_patt").agg(collect_list('value) as "newval").show(false)
+--------+---------------------------+
|reg_patt|newval                     |
+--------+---------------------------+
|1000 11 |[Leds ST, Pear QA, Lear QA]|
|0000    |[adaz]                     |
|1001 00 |[LssA ss]                  |
+--------+---------------------------+


scala>
Related