Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4465,6 +4465,15 @@ object SQLConf {
.booleanConf
.createWithDefault(false)

val WINDOW_MONOTONIC_DEQUE_ENABLED =
buildConf("spark.sql.window.monotonicDeque.enabled")
.withBindingPolicy(ConfigBindingPolicy.NOT_APPLICABLE)
.doc("Use monotonic deques for sliding min/max aggregate window frames. " +
"This provides O(N) complexity instead of O(N * W) or O(N log W).")
.version("5.0.0")
.booleanConf
.createWithDefault(true)

val WINDOW_SEGMENT_TREE_MIN_PARTITION_ROWS =
buildConf("spark.sql.window.segmentTree.minPartitionRows")
.withBindingPolicy(ConfigBindingPolicy.NOT_APPLICABLE)
Expand Down Expand Up @@ -8778,6 +8787,8 @@ class SQLConf extends Serializable with Logging with SqlApiConf {

def windowSegmentTreeEnabled: Boolean = getConf(WINDOW_SEGMENT_TREE_ENABLED)

def windowMonotonicDequeEnabled: Boolean = getConf(WINDOW_MONOTONIC_DEQUE_ENABLED)

def windowSegmentTreeMinPartitionRows: Int = getConf(WINDOW_SEGMENT_TREE_MIN_PARTITION_ROWS)

def windowSegmentTreeBlockSize: Int = getConf(WINDOW_SEGMENT_TREE_BLOCK_SIZE)
Expand Down
179 changes: 119 additions & 60 deletions sql/core/benchmarks/WindowBenchmark-results.txt

Large diffs are not rendered by default.

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(

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Author

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.

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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import scala.collection.mutable.ArrayBuffer
import org.apache.spark.{SparkException, TaskContext}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Add, AggregateWindowFunction, Ascending, Attribute, BoundReference, CurrentRow, DateAdd, DateAddYMInterval, DecimalAddNoOverflowCheck, Descending, Expression, ExtractANSIIntervalDays, FrameLessOffsetWindowFunction, FrameType, IdentityProjection, IntegerLiteral, MutableProjection, NamedExpression, OffsetWindowFunction, PythonFuncExpression, RangeFrame, RowFrame, RowOrdering, SortOrder, SpecifiedWindowFrame, TimestampAddInterval, TimestampAddYMInterval, UnaryMinus, UnboundedFollowing, UnboundedPreceding, UnsafeProjection, WindowExpression}
import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, DeclarativeAggregate}
import org.apache.spark.sql.catalyst.expressions.aggregate.{
AggregateExpression, DeclarativeAggregate, Max, Min
}
import org.apache.spark.sql.execution.metric.SQLMetric
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{CalendarIntervalType, DateType, DayTimeIntervalType, DecimalType, IntegerType, TimestampNTZType, TimestampType, YearMonthIntervalType}
Expand Down Expand Up @@ -281,7 +283,19 @@ trait WindowEvaluatorFactoryBase {

// Shrinking Frame.
case ("AGGREGATE", frameType, lower, UnboundedFollowing, _) =>
if (eligibleForSegTree(functions, aggFilters, frameType, conf)) {
val isMinMaxOnly = conf.windowMonotonicDequeEnabled &&
functions.nonEmpty && functions.forall {
case _: Min | _: Max => true
case _ => false
} && aggFilters.forall(_.isEmpty)

if (isMinMaxOnly) {
target: InternalRow => {
val lb = createBoundOrdering(frameType, lower, timeZone)
new SlidingWindowMinMaxFunctionFrame(
target, processor, lb, None, functions, childOutput)
}
} else if (eligibleForSegTree(functions, aggFilters, frameType, conf)) {
val segFns = functions.map(_.asInstanceOf[DeclarativeAggregate])
// Shrinking-frame queries `[lower, n)` on `WindowSegmentTree` touch the LRU
// for exactly two blocks per query: (1) the lower-edge partial block, and
Expand Down Expand Up @@ -334,7 +348,20 @@ trait WindowEvaluatorFactoryBase {

// Moving Frame.
case ("AGGREGATE", frameType, lower, upper, _) =>
if (eligibleForSegTree(functions, aggFilters, frameType, conf)) {
val isMinMaxOnly = conf.windowMonotonicDequeEnabled &&
functions.nonEmpty && functions.forall {
case _: Min | _: Max => true
case _ => false
} && aggFilters.forall(_.isEmpty)

if (isMinMaxOnly) {
target: InternalRow => {
val lb = createBoundOrdering(frameType, lower, timeZone)
val ub = createBoundOrdering(frameType, upper, timeZone)
new SlidingWindowMinMaxFunctionFrame(
target, processor, lb, Some(ub), functions, childOutput)
}
} else if (eligibleForSegTree(functions, aggFilters, frameType, conf)) {
val segFns = functions.map(_.asInstanceOf[DeclarativeAggregate])
val cacheHint = estimateMaxCachedBlocks(lower, upper, frameType, blockSize)
target: InternalRow => {
Expand Down
Loading