cloud-fan commented on a change in pull request #31181:
URL: https://github.com/apache/spark/pull/31181#discussion_r557855366



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala
##########
@@ -208,53 +209,60 @@ object CharVarcharUtils extends Logging {
     RaiseError(Literal(errMsg, StringType), StringType)
   }
 
-  private def stringLengthCheck(expr: Expression, dt: DataType): Expression = 
dt match {
-    case CharType(length) =>
-      val trimmed = StringTrimRight(expr)
-      // Trailing spaces do not count in the length check. We don't need to 
retain the trailing
-      // spaces, as we will pad char type columns/fields at read time.
-      If(
-        GreaterThan(Length(trimmed), Literal(length)),
-        raiseError("char", length),
-        trimmed)
-
-    case VarcharType(length) =>
-      val trimmed = StringTrimRight(expr)
-      // Trailing spaces do not count in the length check. We need to retain 
the trailing spaces
-      // (truncate to length N), as there is no read-time padding for varchar 
type.
-      // TODO: create a special TrimRight function that can trim to a certain 
length.
-      If(
-        LessThanOrEqual(Length(expr), Literal(length)),
-        expr,
+  private def stringLengthCheck(expr: Expression, dt: DataType, needTrim: 
Boolean): Expression = {
+    dt match {
+      case CharType(length) =>
+        val trimmed = if (needTrim) StringTrimRight(expr) else expr
+        // Trailing spaces do not count in the length check. We don't need to 
retain the trailing
+        // spaces, as we will pad char type columns/fields at read time.
         If(
           GreaterThan(Length(trimmed), Literal(length)),
-          raiseError("varchar", length),
-          StringRPad(trimmed, Literal(length))))
+          raiseError("char", length),
+          trimmed)
+
+      case VarcharType(length) =>
+        if (needTrim) {
+          val trimmed = StringTrimRight(expr)
+          // Trailing spaces do not count in the length check. We need to 
retain the trailing spaces
+          // (truncate to length N), as there is no read-time padding for 
varchar type.
+          // TODO: create a special TrimRight function that can trim to a 
certain length.

Review comment:
       let's keep it as it's not added by this PR: 
https://github.com/apache/spark/pull/31181/files#diff-16753285e80505e04c445ea8ccee1dde7ae601ed7bae224e212d90d395f57928L225




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to