attilapiros commented on code in PR #53458:
URL: https://github.com/apache/spark/pull/53458#discussion_r2756219303
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1380,7 +1414,41 @@ case class Cast(
val fromArg = ctx.addReferenceObj("from", from)
(c, evPrim, evNull) => code"$evPrim = $cls.castToVariant($c, $fromArg);"
case s: StringType =>
- (c, evPrim, _) => castToStringCode(from, ctx, s.constraint).apply(c,
evPrim)
+ // BinaryType to StringType cast with UTF-8 validation
+ from match {
+ case BinaryType =>
+ val tmp = ctx.freshVariable("tmp", classOf[UTF8String])
+
+ if (!validateUtf8) {
+ // Old behavior: no validation
+ (c, evPrim, evNull) =>
+ code"$evPrim = UTF8String.fromBytes($c);"
+ } else if (ansiEnabled) {
+ // ANSI mode: throw on invalid UTF-8
+ val errorContext = getContextOrNullCode(ctx)
+ (c, evPrim, evNull) =>
+ code"""
+ UTF8String $tmp = UTF8String.fromBytes($c);
+ if (!$tmp.isValid()) {
+ throw QueryExecutionErrors.invalidUtf8InBinaryCastError($c,
$errorContext);
+ }
+ $evPrim = $tmp;
Review Comment:
Same as above (length constraint is missing).
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1380,7 +1414,41 @@ case class Cast(
val fromArg = ctx.addReferenceObj("from", from)
(c, evPrim, evNull) => code"$evPrim = $cls.castToVariant($c, $fromArg);"
case s: StringType =>
- (c, evPrim, _) => castToStringCode(from, ctx, s.constraint).apply(c,
evPrim)
+ // BinaryType to StringType cast with UTF-8 validation
+ from match {
+ case BinaryType =>
+ val tmp = ctx.freshVariable("tmp", classOf[UTF8String])
+
+ if (!validateUtf8) {
+ // Old behavior: no validation
+ (c, evPrim, evNull) =>
+ code"$evPrim = UTF8String.fromBytes($c);"
+ } else if (ansiEnabled) {
+ // ANSI mode: throw on invalid UTF-8
+ val errorContext = getContextOrNullCode(ctx)
+ (c, evPrim, evNull) =>
+ code"""
+ UTF8String $tmp = UTF8String.fromBytes($c);
+ if (!$tmp.isValid()) {
+ throw QueryExecutionErrors.invalidUtf8InBinaryCastError($c,
$errorContext);
+ }
+ $evPrim = $tmp;
+ """
+ } else {
+ // LEGACY mode: return null on invalid UTF-8
+ (c, evPrim, evNull) =>
+ code"""
+ UTF8String $tmp = UTF8String.fromBytes($c);
+ if ($tmp.isValid()) {
+ $evPrim = $tmp;
Review Comment:
Same as above (length constraint is missing).
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]