From be3a26ad862037aaac37e4c7620d9697cd6f85c7 Mon Sep 17 00:00:00 2001 From: Jovan Markovic Date: Wed, 8 Jan 2025 11:47:51 +0100 Subject: [PATCH] Remove trimming when casting char to string --- .../sql/catalyst/expressions/ToPrettyString.scala | 12 +++--------- .../sql/catalyst/expressions/ToStringBase.scala | 3 --- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToPrettyString.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToPrettyString.scala index 599945b3071f6..f6fc9b3abd65b 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToPrettyString.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToPrettyString.scala @@ -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 /** @@ -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) @@ -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} diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToStringBase.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToStringBase.scala index 390fe0e460f3c..9b9c5354e4e29 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToStringBase.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToStringBase.scala @@ -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) } @@ -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 _ =>