Github user tarekauel commented on a diff in the pull request:
https://github.com/apache/spark/pull/7214#discussion_r33885133
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -298,3 +299,34 @@ case class StringLength(child: Expression) extends
UnaryExpression with ExpectsI
override def prettyName: String = "length"
}
+
+/**
+ * A function that return the Levenshtein distance between the two given
strings.
+ */
+case class Levenshtein(left: Expression, right: Expression) extends
BinaryExpression
+ with ExpectsInputTypes {
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(StringType,
StringType)
+
+ override def eval(input: InternalRow): Any = {
+ val leftValue = left.eval(input)
+ if (leftValue == null) {
+ null
+ } else {
+ val rightValue = right.eval(input)
+ if(rightValue == null) {
+ null
+ } else {
+ StringUtils.getLevenshteinDistance(leftValue.toString,
rightValue.toString)
+ }
+ }
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val stringUtils = classOf[StringUtils].getName
+ nullSafeCodeGen(ctx, ev, (res, left, right) =>
+ s"$res = $stringUtils.getLevenshteinDistance($left.toString(),
$right.toString());")
+ }
+
+ override def dataType: DataType = IntegerType
--- End diff --
sure
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]