-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-15657][SQL] RowEncoder should validate the data type of input object #13401
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
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
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 |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import org.apache.spark.SparkConf | |
| import org.apache.spark.serializer._ | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.encoders.RowEncoder | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||
| import org.apache.spark.sql.catalyst.util.GenericArrayData | ||
|
|
@@ -692,22 +693,17 @@ case class AssertNotNull(child: Expression, walkedTypePath: Seq[String]) | |
| case class GetExternalRowField( | ||
| child: Expression, | ||
| index: Int, | ||
| fieldName: String, | ||
| dataType: DataType) extends UnaryExpression with NonSQLExpression { | ||
| fieldName: String) extends UnaryExpression with NonSQLExpression { | ||
|
|
||
| override def nullable: Boolean = false | ||
|
|
||
| override def dataType: DataType = ObjectType(classOf[Object]) | ||
|
|
||
| override def eval(input: InternalRow): Any = | ||
| throw new UnsupportedOperationException("Only code-generated evaluation is supported") | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val row = child.genCode(ctx) | ||
|
|
||
| val getField = dataType match { | ||
| case ObjectType(x) if x == classOf[Row] => s"""${row.value}.getStruct($index)""" | ||
| case _ => s"""(${ctx.boxedType(dataType)}) ${row.value}.get($index)""" | ||
| } | ||
|
|
||
| val code = s""" | ||
| ${row.code} | ||
|
|
||
|
|
@@ -720,8 +716,55 @@ case class GetExternalRowField( | |
| "cannot be null."); | ||
| } | ||
|
|
||
| final ${ctx.javaType(dataType)} ${ev.value} = $getField; | ||
| final Object ${ev.value} = ${row.value}.get($index); | ||
| """ | ||
| ev.copy(code = code, isNull = "false") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates the actual data type of input expression at runtime. If it doesn't match the | ||
| * expectation, throw an exception. | ||
| */ | ||
| case class ValidateExternalType(child: Expression, expected: DataType) | ||
| extends UnaryExpression with NonSQLExpression with ExpectsInputTypes { | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(ObjectType(classOf[Object])) | ||
|
|
||
| override def nullable: Boolean = child.nullable | ||
|
|
||
| override def dataType: DataType = RowEncoder.externalDataTypeForInput(expected) | ||
|
|
||
| override def eval(input: InternalRow): Any = | ||
| throw new UnsupportedOperationException("Only code-generated evaluation is supported") | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val input = child.genCode(ctx) | ||
| val obj = input.value | ||
|
|
||
| val typeCheck = expected match { | ||
| case _: DecimalType => | ||
| Seq(classOf[java.math.BigDecimal], classOf[scala.math.BigDecimal], classOf[Decimal]) | ||
| .map(cls => s"$obj instanceof ${cls.getName}").mkString(" || ") | ||
| case _: ArrayType => | ||
| s"$obj instanceof ${classOf[Seq[_]].getName} || $obj.getClass().isArray()" | ||
| case _ => | ||
| s"$obj instanceof ${ctx.boxedType(dataType)}" | ||
| } | ||
|
|
||
| val code = s""" | ||
| ${input.code} | ||
| ${ctx.javaType(dataType)} ${ev.value} = ${ctx.defaultValue(dataType)}; | ||
|
Member
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.
Contributor
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. This is intentional, we can't cast an object to int directly, but have to cast to boxed int first. |
||
| if (!${input.isNull}) { | ||
| if ($typeCheck) { | ||
| ${ev.value} = (${ctx.boxedType(dataType)}) $obj; | ||
| } else { | ||
| throw new RuntimeException($obj.getClass().getName() + " is not a valid " + | ||
| "external type for schema of ${expected.simpleString}"); | ||
| } | ||
| } | ||
|
|
||
| """ | ||
| ev.copy(code = code, isNull = input.isNull) | ||
| } | ||
| } | ||
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.
Previously we completely rely on the correctness of provided schema. If the data doesn't match provided schema, we should see value converting error. I think this
ValidateExternalTypecan improve the experience of error handling. However, it looks like a performance regression as we do extra checking here. If we can trust the provided schema, should we do this checking?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.
The problem is we can't trust it.... When users call
createDataFrame(rows, schema), we should definitely validate the passed-in rows. I think performance doesn't matter too much here, as this only happens at the beginning of the data flow. One potential issue may be that,Dataset.mapcan return row and users will provide a schema we should trust. However, I don't think we should exposeRowEncoderto users andDataset.mapshould never return a row.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.
hmm, for the trust, it should be said that we leave the responsibility of data correctness to users. Actually when the data is in wrong type, we will not get wrong result. There is of course an exception regarding data converting will be thrown. However, better error handling is always good if as you said the performance is not big issue here.