-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,39 @@ 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 funcName = ctx.freshName("fieldToString") | ||
| ctx.addNewFunction(funcName, | ||
|
Contributor
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. no need to create a function, it's called only once.
Contributor
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. Actually we can create functions, for data types that appeared more than once among struct fields.
Contributor
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. BTW here we may hit 64kb compile error if there are a lot of fields in this struct. We should use
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. ok, l'll update soon |
||
| s""" | ||
| |private UTF8String $funcName(${ctx.javaType(ft)} field) { | ||
| | UTF8String fieldStr = null; | ||
| | ${fieldToStringCode("field", "fieldStr", null /* resultIsNull won't be used */)} | ||
| | return fieldStr; | ||
| |} | ||
| """.stripMargin) | ||
|
|
||
| s""" | ||
| |${if (i != 0) s"""$buffer.append(",");""" else ""} | ||
| |if (!$row.isNullAt($i)) { | ||
| | ${if (i != 0) s"""$buffer.append(" ");""" else ""} | ||
| | $buffer.append($funcName(${ctx.getValue(row, ft, s"$i")})); | ||
| |} | ||
| """.stripMargin | ||
| } | ||
|
|
||
| s""" | ||
| |$buffer.append("["); | ||
| |${structToStringCode.mkString("\n")} | ||
| |$buffer.append("]"); | ||
| """.stripMargin | ||
| } | ||
|
|
||
| private[this] def castToStringCode(from: DataType, ctx: CodegenContext): CastFunction = { | ||
| from match { | ||
| case BinaryType => | ||
|
|
@@ -765,6 +821,17 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| |$evPrim = $buffer.build(); | ||
| """.stripMargin | ||
| } | ||
| case StructType(fields) => | ||
| (c, evPrim, evNull) => { | ||
| val buffer = ctx.freshName("buffer") | ||
| val bufferClass = classOf[UTF8StringBuilder].getName | ||
| val writeStructCode = writeStructToStringBuilder(fields.map(_.dataType), c, buffer, ctx) | ||
| s""" | ||
| |$bufferClass $buffer = new $bufferClass(); | ||
| |$writeStructCode; | ||
| |$evPrim = $buffer.build(); | ||
| """.stripMargin | ||
| } | ||
| case _ => | ||
| (c, evPrim, evNull) => s"$evPrim = UTF8String.fromString(String.valueOf($c));" | ||
| } | ||
|
|
||
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.