From 06b8b6c9f4e7d82608d0507860b0dbe7acb6af54 Mon Sep 17 00:00:00 2001 From: Marco Gaido Date: Mon, 14 May 2018 15:03:38 +0200 Subject: [PATCH] address comments --- .../expressions/MaskExpressionsUtils.java | 2 +- .../expressions/maskExpressions.scala | 22 +++++++++---------- .../expressions/MaskExpressionsSuite.scala | 8 +++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/MaskExpressionsUtils.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/MaskExpressionsUtils.java index 02f3a3f0c5343..0caf0054b75ca 100644 --- a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/MaskExpressionsUtils.java +++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/MaskExpressionsUtils.java @@ -24,7 +24,7 @@ public class MaskExpressionsUtils { final static int UNMASKED_VAL = -1; /** - * + * Returns the masking character for {@param c} or {@param c} is it should not be masked. * @param c the character to transform * @param maskedUpperChar the character to use instead of a uppercase letter * @param maskedLowerChar the character to use instead of a lowercase letter diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/maskExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/maskExpressions.scala index 4ce396ea9d4a3..cdf788d8a5d0f 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/maskExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/maskExpressions.scala @@ -79,7 +79,7 @@ trait MaskLike { } def appendMaskedToStringBuffer( - sb: StringBuffer, + sb: java.lang.StringBuilder, inputString: String, startOffset: Int, numChars: Int): Int = { @@ -98,7 +98,7 @@ trait MaskLike { } def appendUnchangedToStringBuffer( - sb: StringBuffer, + sb: java.lang.StringBuilder, inputString: String, startOffset: Int, numChars: Int): Int = { @@ -128,16 +128,16 @@ object MaskLike { val defaultMaskedOther: Int = MaskExpressionsUtils.UNMASKED_VAL def extractCharCount(e: Expression): Int = e match { - case Literal(i, IntegerType|NullType) => + case Literal(i, IntegerType | NullType) => if (i == null) defaultCharCount else i.asInstanceOf[Int] - case Literal(_, dt) => throw new AnalysisException(s"Expected literal expression of type " + + case Literal(_, dt) => throw new AnalysisException("Expected literal expression of type " + s"${IntegerType.simpleString}, but got literal of ${dt.simpleString}") case _ => defaultCharCount } def extractReplacement(e: Expression): String = e match { - case Literal(s, StringType|NullType) => if (s == null) null else s.toString - case Literal(_, dt) => throw new AnalysisException(s"Expected literal expression of type " + + case Literal(s, StringType | NullType) => if (s == null) null else s.toString + case Literal(_, dt) => throw new AnalysisException("Expected literal expression of type " + s"${StringType.simpleString}, but got literal of ${dt.simpleString}") case _ => null } @@ -173,7 +173,7 @@ case class Mask(child: Expression, upper: String, lower: String, digit: String) override def nullSafeEval(input: Any): Any = { val str = input.asInstanceOf[UTF8String].toString val length = str.codePointCount(0, str.length()) - val sb = new StringBuffer(length) + val sb = new java.lang.StringBuilder(length) appendMaskedToStringBuffer(sb, str, 0, length) UTF8String.fromString(sb.toString) } @@ -249,7 +249,7 @@ case class MaskFirstN( val str = input.asInstanceOf[UTF8String].toString val length = str.codePointCount(0, str.length()) val endOfMask = if (charCount > length) length else charCount - val sb = new StringBuffer(length) + val sb = new java.lang.StringBuilder(length) val offset = appendMaskedToStringBuffer(sb, str, 0, endOfMask) appendUnchangedToStringBuffer(sb, str, offset, length - endOfMask) UTF8String.fromString(sb.toString) @@ -332,7 +332,7 @@ case class MaskLastN( val str = input.asInstanceOf[UTF8String].toString val length = str.codePointCount(0, str.length()) val startOfMask = if (charCount >= length) 0 else length - charCount - val sb = new StringBuffer(length) + val sb = new java.lang.StringBuilder(length) val offset = appendUnchangedToStringBuffer(sb, str, 0, startOfMask) appendMaskedToStringBuffer(sb, str, offset, length - startOfMask) UTF8String.fromString(sb.toString) @@ -416,7 +416,7 @@ case class MaskShowFirstN( val str = input.asInstanceOf[UTF8String].toString val length = str.codePointCount(0, str.length()) val startOfMask = if (charCount > length) length else charCount - val sb = new StringBuffer(length) + val sb = new java.lang.StringBuilder(length) val offset = appendUnchangedToStringBuffer(sb, str, 0, startOfMask) appendMaskedToStringBuffer(sb, str, offset, length - startOfMask) UTF8String.fromString(sb.toString) @@ -499,7 +499,7 @@ case class MaskShowLastN( val str = input.asInstanceOf[UTF8String].toString val length = str.codePointCount(0, str.length()) val endOfMask = if (charCount >= length) 0 else length - charCount - val sb = new StringBuffer(length) + val sb = new java.lang.StringBuilder(length) val offset = appendMaskedToStringBuffer(sb, str, 0, endOfMask) appendUnchangedToStringBuffer(sb, str, offset, length - endOfMask) UTF8String.fromString(sb.toString) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MaskExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MaskExpressionsSuite.scala index 45ed081af35ac..ae49488a9f0bc 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MaskExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MaskExpressionsSuite.scala @@ -55,8 +55,8 @@ class MaskExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { } test("mask_first_n") { - checkEvaluation(MaskFirstN(Literal("abcd-EFGH-8765-4321"), 6, "U", "l", "#"), - "llll-UFGH-8765-4321") + checkEvaluation(MaskFirstN(Literal("aB3d-EFGH-8765"), 6, "U", "l", "#"), + "lU#l-UFGH-8765") checkEvaluation(new MaskFirstN( Literal("abcd-EFGH-8765-4321"), Literal(6), Literal("U"), Literal("l"), Literal("#")), "llll-UFGH-8765-4321") @@ -135,8 +135,8 @@ class MaskExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { "abcd-EFGH-8765-4321") // scalastyle:off nonascii checkEvaluation(MaskLastN(Literal("Ul9U"), 2, "\u2200", null, null), "Uln\u2200") - checkEvaluation(new MaskLastN(Literal("あ, 𠀋, Hello World"), Literal(10)), - "あ, 𠀋, Hxxxx Xxxxx") + checkEvaluation(new MaskLastN(Literal("あ, 𠀋, Hello World あ 𠀋"), Literal(10)), + "あ, 𠀋, Hello Xxxxx あ 𠀋") // scalastyle:on nonascii }