-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-58201][SQL] Optimize sliding window MIN/MAX aggregate function using monotonic deque #57346
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
Open
pavan51
wants to merge
2
commits into
apache:master
Choose a base branch
from
pavan51:sliding-window-min-max-monotonic-deque
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
Large diffs are not rendered by default.
Oops, something went wrong.
210 changes: 210 additions & 0 deletions
210
...c/main/scala/org/apache/spark/sql/execution/window/SlidingWindowMinMaxFunctionFrame.scala
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 |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.window | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
| import org.apache.spark.sql.catalyst.util.TypeUtils | ||
| import org.apache.spark.sql.execution.ExternalAppendOnlyUnsafeRowArray | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * An optimized sliding window frame that calculates min and/or max aggregate functions | ||
| * using monotonic deques. This provides O(N) time complexity instead of O(N * W) of | ||
| * [[SlidingWindowFunctionFrame]] or O(N log W) of [[SegmentTreeWindowFunctionFrame]]. | ||
| */ | ||
| private[window] final class SlidingWindowMinMaxFunctionFrame( | ||
| target: InternalRow, | ||
| processor: AggregateProcessor, | ||
| lbound: BoundOrdering, | ||
| ubound: Option[BoundOrdering], | ||
| functions: Array[Expression], | ||
| inputSchema: Seq[Attribute]) | ||
| extends WindowFunctionFrame { | ||
|
|
||
| /** Rows of the partition currently being processed. */ | ||
| private[this] var input: ExternalAppendOnlyUnsafeRowArray = null | ||
|
|
||
| /** Iterators over the [[input]] */ | ||
| private[this] var lowerIterator: Iterator[UnsafeRow] = _ | ||
| private[this] var inputIterator: Iterator[UnsafeRow] = _ | ||
|
|
||
| /** The row at lowerBound. */ | ||
| private[this] var lowerRow: UnsafeRow = null | ||
|
|
||
| /** The next row from `input`. */ | ||
| private[this] var nextRow: InternalRow = null | ||
|
|
||
| /** Index of the first input row with a value equal to or greater than the lower bound of the | ||
| * current output row. | ||
| */ | ||
| private[this] var lowerBound = 0 | ||
|
|
||
| /** Index of the first input row with a value greater than the upper bound of the current | ||
| * output row. | ||
| */ | ||
| private[this] var upperBound = 0 | ||
|
|
||
| private[this] val sourceRow = new SpecificInternalRow(functions.map(_.dataType).toIndexedSeq) | ||
|
|
||
| private[this] val deques: Array[MinMaxDeque] = functions.zipWithIndex.map { | ||
| case (func, i) => | ||
| val isMin = func.isInstanceOf[Min] | ||
| val child = func match { | ||
| case m: Min => m.child | ||
| case m: Max => m.child | ||
| } | ||
| val boundChild = BindReferences.bindReference(child, inputSchema) | ||
| val ordering = TypeUtils.getInterpretedOrdering(child.dataType) | ||
| new MinMaxDeque(isMin, boundChild, child.dataType, ordering, i) | ||
| } | ||
|
|
||
| override def prepare(rows: ExternalAppendOnlyUnsafeRowArray): Unit = { | ||
| input = rows | ||
| lowerIterator = input.generateIterator() | ||
| lowerRow = WindowFunctionFrame.getNextOrNull(lowerIterator) | ||
| deques.foreach(_.deque.clear()) | ||
| lowerBound = 0 | ||
|
|
||
| if (ubound.isEmpty) { | ||
| val iter = input.generateIterator() | ||
| var idx = 0 | ||
| while (iter.hasNext) { | ||
| val row = iter.next() | ||
| deques.foreach(_.admit(row, idx)) | ||
| idx += 1 | ||
| } | ||
| upperBound = input.length | ||
| nextRow = null | ||
| inputIterator = null | ||
| } else { | ||
| inputIterator = input.generateIterator() | ||
| nextRow = WindowFunctionFrame.getNextOrNull(inputIterator) | ||
| upperBound = 0 | ||
| } | ||
| } | ||
|
|
||
| override def write(index: Int, current: InternalRow): Unit = { | ||
| var bufferUpdated = index == 0 | ||
|
|
||
| // Drop all rows from the buffer for which the input row value is smaller than | ||
| // the output row lower bound. | ||
| while (lowerBound < upperBound && lbound.compare(lowerRow, lowerBound, current, index) < 0) { | ||
| lowerBound += 1 | ||
| lowerRow = WindowFunctionFrame.getNextOrNull(lowerIterator) | ||
| bufferUpdated = true | ||
| } | ||
|
|
||
| // Add all rows to the buffer for which the input row value is equal to or less than | ||
| // the output row upper bound. | ||
| if (ubound.isDefined) { | ||
| val ub = ubound.get | ||
| while (nextRow != null && ub.compare(nextRow, upperBound, current, index) <= 0) { | ||
| if (lbound.compare(nextRow, lowerBound, current, index) < 0) { | ||
| lowerBound += 1 | ||
| lowerRow = WindowFunctionFrame.getNextOrNull(lowerIterator) | ||
| } else { | ||
| deques.foreach(_.admit(nextRow, upperBound)) | ||
| bufferUpdated = true | ||
| } | ||
| nextRow = WindowFunctionFrame.getNextOrNull(inputIterator) | ||
| upperBound += 1 | ||
| } | ||
| } | ||
|
|
||
| if (bufferUpdated) { | ||
| deques.foreach(_.dropBefore(lowerBound)) | ||
| } | ||
|
|
||
| // Write output values to target. | ||
| if (processor != null && bufferUpdated) { | ||
| var i = 0 | ||
| while (i < deques.length) { | ||
| sourceRow.update(i, deques(i).currentValue()) | ||
| i += 1 | ||
| } | ||
| processor.evaluate(sourceRow, target) | ||
| } | ||
| } | ||
|
|
||
| override def currentLowerBound(): Int = lowerBound | ||
|
|
||
| override def currentUpperBound(): Int = upperBound | ||
|
|
||
| private class ValueWithIndex(val value: Any, val index: Int) | ||
|
|
||
| private class MinMaxDeque( | ||
| val isMin: Boolean, | ||
| val boundChild: Expression, | ||
| val dataType: DataType, | ||
| val ordering: Ordering[Any], | ||
| val bufferIndex: Int) { | ||
| val deque = new java.util.ArrayDeque[ValueWithIndex]() | ||
| private val tempRow = new SpecificInternalRow(Seq(dataType)) | ||
| private val isPrimitive = dataType match { | ||
| case BooleanType | ByteType | ShortType | IntegerType | LongType | FloatType | DoubleType | | ||
| DateType | TimestampType | TimestampNTZType | _: YearMonthIntervalType | | ||
| _: DayTimeIntervalType => true | ||
| case _ => false | ||
| } | ||
|
|
||
| private def evaluateAndCopy(row: InternalRow): Any = { | ||
| val value = boundChild.eval(row) | ||
| if (value == null) { | ||
| null | ||
| } else if (isPrimitive) { | ||
| value | ||
| } else { | ||
| tempRow.update(0, value) | ||
| val copiedRow = tempRow.copy() | ||
| copiedRow.get(0, dataType) | ||
| } | ||
| } | ||
|
|
||
| def admit(row: InternalRow, index: Int): Unit = { | ||
| val value = evaluateAndCopy(row) | ||
| if (value != null) { | ||
| if (isMin) { | ||
| while (!deque.isEmpty && ordering.compare(deque.peekLast().value, value) >= 0) { | ||
| deque.pollLast() | ||
| } | ||
| } else { | ||
| while (!deque.isEmpty && ordering.compare(deque.peekLast().value, value) <= 0) { | ||
| deque.pollLast() | ||
| } | ||
| } | ||
| deque.offerLast(new ValueWithIndex(value, index)) | ||
| } | ||
| } | ||
|
|
||
| def dropBefore(boundary: Int): Unit = { | ||
| while (!deque.isEmpty && deque.peekFirst().index < boundary) { | ||
| deque.pollFirst() | ||
| } | ||
| } | ||
|
|
||
| def currentValue(): Any = { | ||
| if (deque.isEmpty) { | ||
| null | ||
| } else { | ||
| deque.peekFirst().value | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
This adds a new default-on execution path with no correctness test. I think it might be worth considering adding a differential test.
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.
Thanks for the suggestion! I've added a new differential correctness test suite (MonotonicDequeWindowFunctionSuite ) verifying that the Monotonic Deque matches both the Segment Tree and Naive baselines across primitives, reference types, range/row frames, and null values.