-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-8023] [SQL] Add a deterministic method to Expression. #6570
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 all commits
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 |
---|---|---|
|
@@ -179,8 +179,17 @@ object ColumnPruning extends Rule[LogicalPlan] { | |
* expressions into one single expression. | ||
*/ | ||
object ProjectCollapsing extends Rule[LogicalPlan] { | ||
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. ideally we should have a unit test on the optimizer rule itself also |
||
|
||
/** Returns true if any expression in projectList is non-deterministic. */ | ||
private def hasNondeterministic(projectList: Seq[NamedExpression]): Boolean = { | ||
projectList.exists(expr => expr.find(!_.deterministic).isDefined) | ||
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 won't match on rand() + 1 will this? 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 will match rand() + 1. |
||
} | ||
|
||
def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
case Project(projectList1, Project(projectList2, child)) => | ||
// We only collapse these two Projects if the child Project's expressions are all | ||
// deterministic. | ||
case Project(projectList1, Project(projectList2, child)) | ||
if !hasNondeterministic(projectList2) => | ||
// Create a map of Aliases to their values from the child projection. | ||
// e.g., 'SELECT ... FROM (SELECT a + b AS c, d ...)' produces Map(c -> Alias(a + b, c)). | ||
val aliasMap = AttributeMap(projectList2.collect { | ||
|
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.
Shouldn't this only be true if all the children are
deterministic
(if there are any).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 it depends on how you interpret this. The definition here is whether this expression itself is deterministic or not (i.e. assuming fixed input, is the output deterministic?)
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.
Oh I understand the TODO now... It seems better be holistic about expressing an expression's determinism. Otherwise, you leave the burden of tree traversal to the user, which could be more error prone. But I agree that either could work given proper documentation.