-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-8993][SQL] More comprehensive type checking in expressions. #7348
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
13 commits
Select commit
Hold shift + click to select a range
d017861
Improve expression type checking.
rxin e4727cc
BinaryOperator should not be doing implicit cast.
rxin d061691
Rename existing ExpectsInputTypes -> ImplicitCastInputTypes.
rxin 4932d57
Style fix.
rxin 2e22330
Fixed unit tests.
rxin fb66657
Convert NullType into some accepted type for BinaryOperators.
rxin 360d124
Fixed the rule.
rxin d55c9e5
Removes NullTypes.
rxin 438ea07
space
rxin aa7790e
Moved RemoveNullTypes into ImplicitTypeCasts.
rxin f45408f
Comment update.
rxin 3bb63e7
Style fix.
rxin 8fcf814
Fixed ordering of cases.
rxin 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
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 |
|---|---|---|
|
|
@@ -24,8 +24,20 @@ import org.apache.spark.sql.catalyst.trees | |
| import org.apache.spark.sql.catalyst.trees.TreeNode | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // This file defines the basic expression abstract classes in Catalyst, including: | ||
| // Expression: the base expression abstract class | ||
| // LeafExpression | ||
| // UnaryExpression | ||
| // BinaryExpression | ||
| // BinaryOperator | ||
| // | ||
| // For details, see their classdocs. | ||
| //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /** | ||
| * An expression in Catalyst. | ||
| * | ||
| * If an expression wants to be exposed in the function registry (so users can call it with | ||
| * "name(arguments...)", the concrete implementation must be a case class whose constructor | ||
| * arguments are all Expressions types. | ||
|
|
@@ -335,15 +347,41 @@ abstract class BinaryExpression extends Expression with trees.BinaryNode[Express | |
|
|
||
|
|
||
| /** | ||
| * An expression that has two inputs that are expected to the be same type. If the two inputs have | ||
| * different types, the analyzer will find the tightest common type and do the proper type casting. | ||
| * A [[BinaryExpression]] that is an operator, with two properties: | ||
| * | ||
| * 1. The string representation is "x symbol y", rather than "funcName(x, y)". | ||
| * 2. Two inputs are expected to the be same type. If the two inputs have different types, | ||
| * the analyzer will find the tightest common type and do the proper type casting. | ||
| */ | ||
| abstract class BinaryOperator extends BinaryExpression { | ||
| abstract class BinaryOperator extends BinaryExpression with ExpectsInputTypes { | ||
| self: Product => | ||
|
|
||
| /** | ||
| * Expected input type from both left/right child expressions, similar to the | ||
| * [[ImplicitCastInputTypes]] trait. | ||
| */ | ||
| def inputType: AbstractDataType | ||
|
|
||
| def symbol: String | ||
|
|
||
| override def toString: String = s"($left $symbol $right)" | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(inputType, inputType) | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| // First call the checker for ExpectsInputTypes, and then check whether left and right have | ||
| // the same type. | ||
| super.checkInputDataTypes() match { | ||
| case TypeCheckResult.TypeCheckSuccess => | ||
| if (left.dataType != right.dataType) { | ||
| TypeCheckResult.TypeCheckFailure(s"differing types in '$prettyString' " + | ||
| s"(${left.dataType.simpleString} and ${right.dataType.simpleString}).") | ||
| } else { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } | ||
| case TypeCheckResult.TypeCheckFailure(msg) => TypeCheckResult.TypeCheckFailure(msg) | ||
|
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. Can we simplify this to
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. that's a good idea |
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
What if
expected.acceptsType(in.dateType) == false, probably we'd better to raise a TypeChecking exception?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.
that happens during CheckAnalysis when we report errors.