wangyum commented on code in PR #59027: URL: https://github.com/apache/spark/pull/59027#discussion_r4257188404
########## sql/core/src/main/scala/org/apache/spark/sql/execution/joins/RangeIndex.scala: ########## @@ -0,0 +1,162 @@ +/* + * 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.joins + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.types.DataType +import org.apache.spark.util.SizeEstimator + +/** + * Broadcast payload of a range join. An index only names candidate rows. + * [[BroadcastRangeJoinExec]] evaluates the original join condition to filter candidates. + * + * - [[IntervalIndex]] answers "which ranges overlap this window?" + * - [[PointIndex]] answers "which points lie on this side of a bound?" + */ +private[execution] sealed trait RangeRelation extends Serializable { + def estimatedSize(): Long +} + +private[execution] object RangeIndex { + + /** Accessor reading a single field from `InternalRow` as a boxed value (or null). */ + def getValue(dt: DataType, ordinal: Int): InternalRow => Any = { + val accessor = InternalRow.getAccessor(dt) + (input: InternalRow) => accessor(input, ordinal) + } +} + +/** + * Augmented interval tree of build-side intervals, stored as flat arrays. + * + * Intervals are sorted by low bound. For any span `[start, end)`, its midpoint + * is the subtree root, and `maxHighs(mid)` records the maximum high bound in + * that span. Subtrees whose max high is below the probe window, or whose low + * starts after it, are pruned during search. + */ +private[execution] object IntervalIndex { + + def build( + ordering: Ordering[Any], + intervals: Array[(Any, Any, InternalRow)]): IntervalIndex = { + val valid = intervals + .filter(t => t._1 != null && t._2 != null) + .map { case (lo, hi, r) => + if (ordering.lteq(lo, hi)) (lo, hi, r) else (hi, lo, r) + } + java.util.Arrays.sort(valid, Ordering.by[(Any, Any, InternalRow), Any](_._1)(ordering)) + + val length = valid.length + val maxHighs = new Array[Any](length) + + def fillMaxHigh(start: Int, end: Int): Any = { + if (start >= end) return null + val mid = (start + end) >>> 1 + var max = valid(mid)._2 + val left = fillMaxHigh(start, mid) + val right = fillMaxHigh(mid + 1, end) + if (left != null) max = ordering.max(max, left) + if (right != null) max = ordering.max(max, right) + maxHighs(mid) = max + max + } + + fillMaxHigh(0, length) + new IntervalIndex(ordering, valid, maxHighs) + } +} + +private[execution] class IntervalIndex private[joins] ( + private[this] val ordering: Ordering[Any], + private[this] val intervals: Array[(Any, Any, InternalRow)], + private[this] val maxHighs: Array[Any]) + extends RangeRelation { + + override def estimatedSize(): Long = + SizeEstimator.estimate(intervals) + SizeEstimator.estimate(maxHighs) + + def overlapping(low: Any, high: Any): Iterator[InternalRow] = { + if (intervals.isEmpty || low == null || high == null) return Iterator.empty + val (probeLow, probeHigh) = if (ordering.lteq(low, high)) (low, high) else (high, low) + + val buffer = ArrayBuffer.empty[InternalRow] + def search(start: Int, end: Int): Unit = { + if (start >= end) return + val mid = (start + end) >>> 1 + if (ordering.gteq(maxHighs(mid), probeLow)) { + search(start, mid) + val (lo, hi, row) = intervals(mid) Review Comment: Fixed: Strengthened the test to assert that OverlapIterator.steps remains bounded ($< 50$) under 10,000 fully overlapping intervals, which fails immediately if eager $O(K)$ buffering is reintroduced. https://github.com/apache/spark/pull/59027/changes#diff-330540ce98783fb41808da6f0c9fa44e2e2a4aaf2ba8be04cc433852e8fc47d3R461-R475 -- 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]
