Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-47415][SQL] Add collation support for Levenshtein expression #46788

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2240,8 +2240,8 @@ case class Levenshtein(
}

override def inputTypes: Seq[AbstractDataType] = threshold match {
case Some(_) => Seq(StringType, StringType, IntegerType)
case _ => Seq(StringType, StringType)
case Some(_) => Seq(StringTypeAnyCollation, StringTypeAnyCollation, IntegerType)
case _ => Seq(StringTypeAnyCollation, StringTypeAnyCollation)
}

override def children: Seq[Expression] = threshold match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,28 @@ class CollationStringExpressionsSuite
})
}

test("Levenshtein string expression with collation") {
// Supported collations
case class LevenshteinTestCase(
left: String, right: String, collationName: String, threshold: Option[Int], result: Int
)
val testCases = Seq(
LevenshteinTestCase("kitten", "sitTing", "UTF8_BINARY", None, result = 4),
LevenshteinTestCase("kitten", "sitTing", "UTF8_BINARY_LCASE", None, result = 4),
LevenshteinTestCase("kitten", "sitTing", "UNICODE", Some(3), result = -1),
LevenshteinTestCase("kitten", "sitTing", "UNICODE_CI", Some(3), result = -1)
)
testCases.foreach(t => {
withSQLConf(SQLConf.DEFAULT_COLLATION.key -> t.collationName) {
val th = if (t.threshold.isDefined) s", ${t.threshold.get}" else ""
val query = s"select levenshtein('${t.left}', '${t.right}'$th)"
// Result & data type
checkAnswer(sql(query), Row(t.result))
assert(sql(query).schema.fields.head.dataType.sameType(IntegerType))
}
})
}

test("Support Left/Right/Substr with collation") {
case class SubstringTestCase(
method: String,
Expand Down