Skip to content

Commit

Permalink
add codegen soundex
Browse files Browse the repository at this point in the history
  • Loading branch information
hujy committed Jul 27, 2015
1 parent d15d329 commit 2538908
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,9 @@ case class SoundEx(child: Expression) extends UnaryExpression with ExpectsInputT
override def nullSafeEval(input: Any): Any = {
input.asInstanceOf[UTF8String].soundex()
}
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
nullSafeCodeGen(ctx, ev, c => s"${ev.primitive} = $c.soundex();")
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,12 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(SoundEx(Literal("ZIN")), "Z500", create_row("s1"))
checkEvaluation(SoundEx(Literal("SU")), "S000", create_row("s2"))
checkEvaluation(SoundEx(Literal("")), "", create_row("s3"))
checkEvaluation(SoundEx(Literal(null)), null, create_row("s4"))
checkEvaluation(SoundEx(Literal.create(null, StringType)), null, create_row("s4"))

// scalastyle:off
// non ascii characters are not allowed in the code, so we disable the scalastyle here.
checkEvaluation(SoundEx(Literal("再见")), "再见", create_row("s5"))
checkEvaluation(SoundEx(Literal("z再見")), "z再見", create_row("s6"))
checkEvaluation(SoundEx(Literal("测试")), "测试", create_row("s5"))
checkEvaluation(SoundEx(Literal("z測試")), "z測試", create_row("s6"))
checkEvaluation(SoundEx(Literal("Tschüss")), "Tschüss", create_row("s7"))
// scalastyle:on
checkEvaluation(SoundEx(Literal("zZ")), "z000", create_row("s8"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,9 @@ public int hashCode() {
* https://en.wikipedia.org/wiki/Soundex
*/
public UTF8String soundex() {
if(this == null) {
return null;
}
if (numBytes == 0) {
return UTF8String.fromBytes(new byte[0]);
}
Expand Down

0 comments on commit 2538908

Please sign in to comment.