-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22981][SQL] Fix incorrect results of Casting Struct to String #20176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,6 +259,29 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| builder.append("]") | ||
| builder.build() | ||
| }) | ||
| case StructType(fields) => | ||
| buildCast[InternalRow](_, row => { | ||
| val builder = new UTF8StringBuilder | ||
| builder.append("[") | ||
| if (row.numFields > 0) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, it seems we have no chance to hit |
||
| val st = fields.map(_.dataType) | ||
| val toUTF8StringFuncs = st.map(castToString) | ||
| if (!row.isNullAt(0)) { | ||
| builder.append(toUTF8StringFuncs(0)(row.get(0, st(0))).asInstanceOf[UTF8String]) | ||
| } | ||
| var i = 1 | ||
| while (i < row.numFields) { | ||
| builder.append(",") | ||
| if (!row.isNullAt(i)) { | ||
| builder.append(" ") | ||
| builder.append(toUTF8StringFuncs(i)(row.get(i, st(i))).asInstanceOf[UTF8String]) | ||
| } | ||
| i += 1 | ||
| } | ||
| } | ||
| builder.append("]") | ||
| builder.build() | ||
| }) | ||
| case _ => buildCast[Any](_, o => UTF8String.fromString(o.toString)) | ||
| } | ||
|
|
||
|
|
@@ -732,6 +755,41 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| """.stripMargin | ||
| } | ||
|
|
||
| private def writeStructToStringBuilder( | ||
| st: Seq[DataType], | ||
| row: String, | ||
| buffer: String, | ||
| ctx: CodegenContext): String = { | ||
| val structToStringCode = st.zipWithIndex.map { case (ft, i) => | ||
| val fieldToStringCode = castToStringCode(ft, ctx) | ||
| val field = ctx.freshName("field") | ||
| val fieldStr = ctx.freshName("fieldStr") | ||
| s""" | ||
| |${if (i != 0) s"""$buffer.append(",");""" else ""} | ||
| |if (!$row.isNullAt($i)) { | ||
| | ${if (i != 0) s"""$buffer.append(" ");""" else ""} | ||
| | | ||
| | // Append $i field into the string buffer | ||
| | ${ctx.javaType(ft)} $field = ${ctx.getValue(row, ft, s"$i")}; | ||
| | UTF8String $fieldStr = null; | ||
| | ${fieldToStringCode(field, fieldStr, null /* resultIsNull won't be used */)} | ||
| | $buffer.append($fieldStr); | ||
| |} | ||
| """.stripMargin | ||
| } | ||
|
|
||
| val writeStructCode = ctx.splitExpressions( | ||
| expressions = structToStringCode, | ||
| funcName = "fieldToString", | ||
| arguments = ("InternalRow", row) :: (classOf[UTF8StringBuilder].getName, buffer) :: Nil) | ||
|
|
||
| s""" | ||
| |$buffer.append("["); | ||
| |$writeStructCode | ||
| |$buffer.append("]"); | ||
| """.stripMargin | ||
| } | ||
|
|
||
| private[this] def castToStringCode(from: DataType, ctx: CodegenContext): CastFunction = { | ||
| from match { | ||
| case BinaryType => | ||
|
|
@@ -765,6 +823,19 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| |$evPrim = $buffer.build(); | ||
| """.stripMargin | ||
| } | ||
| case StructType(fields) => | ||
| (c, evPrim, evNull) => { | ||
| val row = ctx.freshName("row") | ||
| val buffer = ctx.freshName("buffer") | ||
| val bufferClass = classOf[UTF8StringBuilder].getName | ||
| val writeStructCode = writeStructToStringBuilder(fields.map(_.dataType), row, buffer, ctx) | ||
| s""" | ||
| |InternalRow $row = $c; | ||
| |$bufferClass $buffer = new $bufferClass(); | ||
| |$writeStructCode | ||
| |$evPrim = $buffer.build(); | ||
| """.stripMargin | ||
| } | ||
| case _ => | ||
| (c, evPrim, evNull) => s"$evPrim = UTF8String.fromString(String.valueOf($c));" | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any reasons for using the same brackets
[]for Struct, Map and Array?How about
[]for arrays,{}for structs and<>for maps or somehow distinguish them. At least, it is not consistent withtoHiveStringwhich prints{}for structs and maps.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have no strong reason. In this PR, I just followed the existing format. Looks like we can change it to
{for consistency (But, we might need to update the migration doc cuz it change the casting behaivour). cc: @cloud-fanThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with the new format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the PR #29308 . Please, review it.