-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16749][SQL] Simplify processing logic in LEAD/LAG processing. #14376
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 |
|---|---|---|
|
|
@@ -209,7 +209,8 @@ case class WindowExec( | |
| new OffsetWindowFunctionFrame( | ||
| target, | ||
| ordinal, | ||
| functions, | ||
| // OFFSET frame functions are guaranteed be OffsetWindowFunctions. | ||
| functions.map(_.asInstanceOf[OffsetWindowFunction]), | ||
| child.output, | ||
| (expressions, schema) => | ||
| newMutableProjection(expressions, schema, subexpressionEliminationEnabled), | ||
|
|
@@ -557,6 +558,9 @@ private[execution] abstract class WindowFunctionFrame { | |
| * The offset window frame calculates frames containing LEAD/LAG statements. | ||
| * | ||
| * @param target to write results to. | ||
| * @param ordinal the ordinal is the starting offset at which the results of the window frame get | ||
| * written into the (shared) target row. The result of the frame expression with | ||
| * index 'i' will be written to the 'ordinal' + 'i' position in the target row. | ||
| * @param expressions to shift a number of rows. | ||
| * @param inputSchema required for creating a projection. | ||
| * @param newMutableProjection function used to create the projection. | ||
|
|
@@ -565,7 +569,7 @@ private[execution] abstract class WindowFunctionFrame { | |
| private[execution] final class OffsetWindowFunctionFrame( | ||
| target: MutableRow, | ||
| ordinal: Int, | ||
|
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. Since we are changing this file, can you add comment to explain what is ordinal (with an example)?
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. Done. In the class doc. |
||
| expressions: Array[Expression], | ||
| expressions: Array[OffsetWindowFunction], | ||
| inputSchema: Seq[Attribute], | ||
| newMutableProjection: (Seq[Expression], Seq[Attribute]) => MutableProjection, | ||
| offset: Int) extends WindowFunctionFrame { | ||
|
|
@@ -576,25 +580,15 @@ private[execution] final class OffsetWindowFunctionFrame( | |
| /** Index of the input row currently used for output. */ | ||
| private[this] var inputIndex = 0 | ||
|
|
||
| /** Row used when there is no valid input. */ | ||
| private[this] val emptyRow = new GenericInternalRow(inputSchema.size) | ||
|
|
||
| /** Row used to combine the offset and the current row. */ | ||
| private[this] val join = new JoinedRow | ||
|
|
||
| /** | ||
| * Create the projection used when the offset row exists. | ||
| * Please note that this project always respect null input values (like PostgreSQL). | ||
| */ | ||
| private[this] val projection = { | ||
| // Collect the expressions and bind them. | ||
| val inputAttrs = inputSchema.map(_.withNullability(true)) | ||
| val boundExpressions = Seq.fill(ordinal)(NoOp) ++ expressions.toSeq.map { | ||
| case e: OffsetWindowFunction => | ||
| val input = BindReferences.bindReference(e.input, inputAttrs) | ||
| input | ||
| case e => | ||
| BindReferences.bindReference(e, inputAttrs) | ||
| val boundExpressions = Seq.fill(ordinal)(NoOp) ++ expressions.toSeq.map { e => | ||
| BindReferences.bindReference(e.input, inputAttrs) | ||
| } | ||
|
|
||
| // Create the projection. | ||
|
|
@@ -605,23 +599,14 @@ private[execution] final class OffsetWindowFunctionFrame( | |
| private[this] val fillDefaultValue = { | ||
| // Collect the expressions and bind them. | ||
| val inputAttrs = inputSchema.map(_.withNullability(true)) | ||
| val numInputAttributes = inputAttrs.size | ||
| val boundExpressions = Seq.fill(ordinal)(NoOp) ++ expressions.toSeq.map { | ||
| case e: OffsetWindowFunction => | ||
| if (e.default == null || e.default.foldable && e.default.eval() == null) { | ||
| // The default value is null. | ||
| Literal.create(null, e.dataType) | ||
| } else { | ||
| // The default value is an expression. | ||
| val default = BindReferences.bindReference(e.default, inputAttrs).transform { | ||
| // Shift the input reference to its default version. | ||
| case BoundReference(o, dataType, nullable) => | ||
| BoundReference(o + numInputAttributes, dataType, nullable) | ||
| } | ||
| default | ||
| } | ||
| case e => | ||
| BindReferences.bindReference(e, inputAttrs) | ||
| val boundExpressions = Seq.fill(ordinal)(NoOp) ++ expressions.toSeq.map { e => | ||
| if (e.default == null || e.default.foldable && e.default.eval() == null) { | ||
| // The default value is null. | ||
| Literal.create(null, e.dataType) | ||
| } else { | ||
| // The default value is an expression. | ||
| BindReferences.bindReference(e.default, inputAttrs) | ||
| } | ||
| } | ||
|
|
||
| // Create the projection. | ||
|
|
@@ -642,12 +627,10 @@ private[execution] final class OffsetWindowFunctionFrame( | |
| override def write(index: Int, current: InternalRow): Unit = { | ||
| if (inputIndex >= 0 && inputIndex < input.size) { | ||
| val r = input.next() | ||
| join(r, current) | ||
| projection(join) | ||
| projection(r) | ||
| } else { | ||
| join(emptyRow, current) | ||
| // Use default values since the offset row does not exist. | ||
| fillDefaultValue(join) | ||
| fillDefaultValue(current) | ||
| } | ||
| inputIndex += 1 | ||
| } | ||
|
|
||
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.
leave a comment here about why it is safe to blindly cast?