srowen commented on a change in pull request #31199:
URL: https://github.com/apache/spark/pull/31199#discussion_r558582512



##########
File path: 
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/util/CharVarcharCodegenUtils.java
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.util;
+
+import org.apache.spark.unsafe.types.UTF8String;
+
+public class CharVarcharCodegenUtils {
+  private static final UTF8String SPACE = UTF8String.fromString(" ");
+
+  public static UTF8String lengthCheck(UTF8String inputStr, int limit) {
+    if (inputStr == null) return null;
+    if (inputStr.numChars() > limit) {

Review comment:
       Not sure if it helps code size or speed, but you can skip the if/return 
above and add "inputStr != null &&" here for the same result.

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala
##########
@@ -200,69 +212,60 @@ object CharVarcharUtils extends Logging {
    */
   def stringLengthCheck(expr: Expression, targetAttr: Attribute): Expression = 
{
     getRawType(targetAttr.metadata).map { rawType =>
-      stringLengthCheck(expr, rawType, needTrim = true)
+      stringLengthCheck(expr, rawType)
     }.getOrElse(expr)
   }
 
   private def raiseError(typeName: String, length: Int): Expression = {
     val errMsg = UTF8String.fromString(s"Exceeds $typeName type length 
limitation: $length")
-    RaiseError(Literal(errMsg, StringType), StringType)
+      RaiseError(Literal(errMsg, StringType), StringType)
   }
 
-  private def stringLengthCheck(expr: Expression, dt: DataType, needTrim: 
Boolean): Expression = {
+  private def stringLengthCheck(expr: Expression, dt: DataType): Expression = {
     dt match {
       case CharType(length) =>
-        val trimmed = if (needTrim) StringTrimRight(expr) else expr
+        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)
+        If(GreaterThan(Length(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.
-          If(
-            LessThanOrEqual(Length(expr), Literal(length)),
-            expr,
-            If(
-              GreaterThan(Length(trimmed), Literal(length)),
-              raiseError("varchar", length),
-              StringRPad(trimmed, Literal(length))))
-        } else {
-          If(GreaterThan(Length(expr), Literal(length)), raiseError("varchar", 
length), expr)
-        }
+        val trimmed = StringTrimRight(expr)

Review comment:
       Why does the needTrim check go away here?




----------------------------------------------------------------
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