Skip to content

Commit

Permalink
Remove trimming when casting char to string
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanm-db committed Jan 8, 2025
1 parent 0d8d108 commit be3a26a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode, FalseLiteral}
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.types.{CharType, DataType, StringType}
import org.apache.spark.sql.types.{DataType, StringType}
import org.apache.spark.unsafe.types.UTF8String

/**
Expand Down Expand Up @@ -51,10 +51,7 @@ case class ToPrettyString(child: Expression, timeZoneId: Option[String] = None)

override protected val binaryFormatter: BinaryFormatter = ToStringBase.getBinaryFormatter

private[this] lazy val castFunc: Any => UTF8String = castToString(child.dataType match {
case CharType(_) => StringType
case dt => dt
})
private[this] lazy val castFunc: Any => UTF8String = castToString(child.dataType)

override def eval(input: InternalRow): Any = {
val v = child.eval(input)
Expand All @@ -63,10 +60,7 @@ case class ToPrettyString(child: Expression, timeZoneId: Option[String] = None)

override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val childCode = child.genCode(ctx)
val toStringCode = castToStringCode(child.dataType match {
case CharType(_) => StringType
case dt => dt
}, ctx).apply(childCode.value, ev.value)
val toStringCode = castToStringCode(child.dataType, ctx).apply(childCode.value, ev.value)
val finalCode =
code"""
|${childCode.code}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ trait ToStringBase { self: UnaryExpression with TimeZoneAwareExpression =>
IntervalUtils.toDayTimeIntervalString(i, ANSI_STYLE, startField, endField)))
case _: DecimalType if useDecimalPlainString =>
acceptAny[Decimal](d => UTF8String.fromString(d.toPlainString))
case CharType(_) => acceptAny[UTF8String](s => { s.trimRight() })
case _: StringType => acceptAny[UTF8String](identity[UTF8String])
case _ => o => UTF8String.fromString(o.toString)
}
Expand Down Expand Up @@ -283,8 +282,6 @@ trait ToStringBase { self: UnaryExpression with TimeZoneAwareExpression =>
// notation if an exponent is needed.
case _: DecimalType if useDecimalPlainString =>
(c, evPrim) => code"$evPrim = UTF8String.fromString($c.toPlainString());"
case CharType(_) =>
(c, evPrim) => code"$evPrim = $c.trimRight();"
case _: StringType =>
(c, evPrim) => code"$evPrim = $c;"
case _ =>
Expand Down

0 comments on commit be3a26a

Please sign in to comment.