-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-35912][SQL] Fix cast struct contains null value to string/struct #33146
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
3 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 |
|---|---|---|
|
|
@@ -406,19 +406,21 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| if (row.numFields > 0) { | ||
| val st = fields.map(_.dataType) | ||
| val toUTF8StringFuncs = st.map(castToString) | ||
| if (row.isNullAt(0)) { | ||
| if (fields(0).nullable && row.isNullAt(0)) { | ||
| if (!legacyCastToStr) builder.append("null") | ||
| } else { | ||
| builder.append(toUTF8StringFuncs(0)(row.get(0, st(0))).asInstanceOf[UTF8String]) | ||
| val accessor = InternalRow.getAccessor(fields(0).dataType, fields(0).nullable) | ||
| builder.append(toUTF8StringFuncs(0)(accessor(row, 0)).asInstanceOf[UTF8String]) | ||
| } | ||
| var i = 1 | ||
| while (i < row.numFields) { | ||
| builder.append(",") | ||
| if (row.isNullAt(i)) { | ||
| if (fields(i).nullable && row.isNullAt(i)) { | ||
| if (!legacyCastToStr) builder.append(" null") | ||
| } else { | ||
| builder.append(" ") | ||
| builder.append(toUTF8StringFuncs(i)(row.get(i, st(i))).asInstanceOf[UTF8String]) | ||
| val accessor = InternalRow.getAccessor(fields(i).dataType, fields(i).nullable) | ||
| builder.append(toUTF8StringFuncs(i)(accessor(row, i)).asInstanceOf[UTF8String]) | ||
| } | ||
| i += 1 | ||
| } | ||
|
|
@@ -868,8 +870,13 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| val newRow = new GenericInternalRow(from.fields.length) | ||
| var i = 0 | ||
| while (i < row.numFields) { | ||
| newRow.update(i, | ||
| if (row.isNullAt(i)) null else castFuncs(i)(row.get(i, from.apply(i).dataType))) | ||
| val value = if (from.fields(i).nullable && row.isNullAt(i)) { | ||
| null | ||
| } else { | ||
| val accessor = InternalRow.getAccessor(from.fields(i).dataType, from.fields(i).nullable) | ||
| castFuncs(i)(accessor(row, i)) | ||
| } | ||
| newRow.update(i, value) | ||
| i += 1 | ||
| } | ||
| newRow | ||
|
|
@@ -1098,29 +1105,37 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| } | ||
|
|
||
| private def writeStructToStringBuilder( | ||
| st: Seq[DataType], | ||
| st: Seq[StructField], | ||
| row: ExprValue, | ||
| buffer: ExprValue, | ||
| ctx: CodegenContext): Block = { | ||
| val structToStringCode = st.zipWithIndex.map { case (ft, i) => | ||
| val fieldToStringCode = castToStringCode(ft, ctx) | ||
| val field = ctx.freshVariable("field", ft) | ||
| val fieldStr = ctx.freshVariable("fieldStr", StringType) | ||
| val javaType = JavaCode.javaType(ft) | ||
| code""" | ||
| |${if (i != 0) code"""$buffer.append(",");""" else EmptyBlock} | ||
| |if ($row.isNullAt($i)) { | ||
|
||
| | ${appendIfNotLegacyCastToStr(buffer, if (i == 0) "null" else " null")} | ||
| |} else { | ||
| | ${if (i != 0) code"""$buffer.append(" ");""" else EmptyBlock} | ||
| | | ||
| | // Append $i field into the string buffer | ||
| | $javaType $field = ${CodeGenerator.getValue(row, ft, s"$i")}; | ||
| | UTF8String $fieldStr = null; | ||
| | ${fieldToStringCode(field, fieldStr, null /* resultIsNull won't be used */)} | ||
| | $buffer.append($fieldStr); | ||
| |} | ||
| """.stripMargin | ||
| val structToStringCode = st.zipWithIndex.map { | ||
| case (StructField(_, dataType, nullable, _), i) => | ||
| val fieldToStringCode = castToStringCode(dataType, ctx) | ||
| val field = ctx.freshVariable("field", dataType) | ||
| val fieldStr = ctx.freshVariable("fieldStr", StringType) | ||
| val javaType = JavaCode.javaType(dataType) | ||
|
|
||
| val isNull = if (nullable) { | ||
| code"$row.isNullAt($i)" | ||
| } else { | ||
| code"false" | ||
| } | ||
|
|
||
| code""" | ||
| |${if (i != 0) code"""$buffer.append(",");""" else EmptyBlock} | ||
| |if ($isNull) { | ||
| | ${appendIfNotLegacyCastToStr(buffer, if (i == 0) "null" else " null")} | ||
| |} else { | ||
| | ${if (i != 0) code"""$buffer.append(" ");""" else EmptyBlock} | ||
| | | ||
| | // Append $i field into the string buffer | ||
| | $javaType $field = ${CodeGenerator.getValue(row, dataType, s"$i")}; | ||
| | UTF8String $fieldStr = null; | ||
| | ${fieldToStringCode(field, fieldStr, null /* resultIsNull won't be used */)} | ||
| | $buffer.append($fieldStr); | ||
| |} | ||
| """.stripMargin | ||
| } | ||
|
|
||
| val writeStructCode = ctx.splitExpressions( | ||
|
|
@@ -1184,7 +1199,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| val row = ctx.freshVariable("row", classOf[InternalRow]) | ||
| val buffer = ctx.freshVariable("buffer", classOf[UTF8StringBuilder]) | ||
| val bufferClass = JavaCode.javaType(classOf[UTF8StringBuilder]) | ||
| val writeStructCode = writeStructToStringBuilder(fields.map(_.dataType), row, buffer, ctx) | ||
| val writeStructCode = writeStructToStringBuilder(fields, row, buffer, ctx) | ||
| code""" | ||
| |InternalRow $row = $c; | ||
| |$bufferClass $buffer = new $bufferClass(); | ||
|
|
@@ -1890,8 +1905,15 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| val toFieldNull = ctx.freshVariable("tfn", BooleanType) | ||
| val fromType = JavaCode.javaType(from.fields(i).dataType) | ||
| val setColumn = CodeGenerator.setColumn(tmpResult, to.fields(i).dataType, i, toFieldPrim) | ||
|
|
||
| val isNull = if (from.fields(i).nullable) { | ||
| code"boolean $fromFieldNull = $tmpInput.isNullAt($i);" | ||
| } else { | ||
| code"boolean $fromFieldNull = false;" | ||
| } | ||
|
|
||
| code""" | ||
| boolean $fromFieldNull = $tmpInput.isNullAt($i); | ||
| $isNull | ||
| if ($fromFieldNull) { | ||
| $tmpResult.setNullAt($i); | ||
| } else { | ||
|
|
||
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.
if
fields(0).nullableis false, how canrow.isNullAt(0)be true?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 have the same question)
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.
If user create dataframe from
spark.internalCreateDataFrame(), therow.isNullAt()may be true even though the schema nullable is false.For instance:
Although the
spark.internalCreateDataFrame()is sql package private API, butspark.read.json()andspark.read.csv()call it without null value handled.(the example show in pr description)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.
Then we need to fix the nullability. There are so many places in the Spark codebase that relies on nullability to do optimizations. It's not possible to change all of them to not trust the nullability anymore.
Can we fix
spark.read.json()to set the nullability correctly?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.
Ok, let me try.