cloud-fan commented on code in PR #57346: URL: https://github.com/apache/spark/pull/57346#discussion_r3766860109
########## sql/core/src/main/scala/org/apache/spark/sql/execution/window/SlidingWindowMinMaxFunctionFrame.scala: ########## @@ -0,0 +1,280 @@ +/* + * 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.execution.metric.SQLMetric +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]]. + * + * This frame is only instantiated when `isMinMaxOnly` is true (all window functions are Min or + * Max and no FILTER clause is used), enforced upstream in [[WindowEvaluatorFactoryBase]]. + */ +private[window] final class SlidingWindowMinMaxFunctionFrame( + target: InternalRow, + processor: AggregateProcessor, + lbound: BoundOrdering, + ubound: BoundOrdering, + functions: Array[Expression], + inputSchema: Seq[Attribute], + numMonotonicDequeFrames: Option[SQLMetric] = None) + extends WindowFunctionFrame { + + /** Rows of the partition currently being processed. */ + private[this] var input: ExternalAppendOnlyUnsafeRowArray = null + + // Spill-safety: when `input` (ExternalAppendOnlyUnsafeRowArray) spills, its + // iterator reuses a single UnsafeRow whose pointer is rebound on each next(). + // This is safe because both cursors follow a read-before-advance pattern: + // `lowerRow`/`nextRow` are used for comparison *before* calling getNextOrNull. + // Values are extracted from the row via `evaluateAndCopy` before advancing. + // DO NOT cache a historical row without an explicit .copy(); the shared + // reusable UnsafeRow would silently mutate. + 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 + + // `sourceRow` is used as the `source` argument to `processor.evaluate(source, target)`. + // Layout compatibility is guaranteed because Min/Max each contribute exactly one + // `aggBufferAttributes` entry typed `child.dataType`, which equals `Min/Max.dataType`. + // Neither is a `SizeBasedWindowFunction`, so no extra slot is prepended. + // `isMinMaxOnly` (enforced in WindowEvaluatorFactoryBase) ensures this invariant holds. + private[this] val sourceRow = new SpecificInternalRow(functions.map(_.dataType).toIndexedSeq) + + // Each deque is addressed by its position in this array (one entry per Min/Max function), Review Comment: **Nit:** This comment is now history-dependent: `bufferIndex` and its `zipWithIndex` producer have been removed, and the positional access is evident from the array. Please remove the stale comment as well. ########## sql/core/src/test/scala/org/apache/spark/sql/execution/window/WindowSegmentTreeAllowlistSuite.scala: ########## @@ -30,22 +30,26 @@ import org.apache.spark.sql.test.SharedSparkSession * Coverage for the explicit segment-tree aggregate allowlist * ([[WindowSegmentTree.EligibleAggregates]]): * - allowlisted aggregates route to segtree (`numSegmentTreeFrames` bumps). - * - non-allowlisted aggregates fall through to the sliding path without - * crashing or producing wrong results (segtree counters stay at 0). - * Eligibility gating only; exhaustive equivalence lives in - * [[SegmentTreeWindowFunctionSuite]]. + * - non-allowlisted aggregates fall through to the sliding path without crashing or producing Review Comment: **Non-blocking:** This suite claims that non-allowlisted aggregates fall through without producing wrong results, but each negative test only collects the query and checks that the segment-tree metric is zero. That proves routing and absence of a crash, not correctness. Please compare the returned rows with expected values or with the same query under the optimization disabled for the enumerated negative cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
