wangyum commented on code in PR #59027: URL: https://github.com/apache/spark/pull/59027#discussion_r4123054352
########## sql/core/src/test/scala/org/apache/spark/sql/execution/joins/RangeIndexSuite.scala: ########## @@ -0,0 +1,334 @@ +/* + * 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 java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream} + +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{BoundReference, Projection} +import org.apache.spark.sql.catalyst.types.PhysicalDataType +import org.apache.spark.sql.types._ +import org.apache.spark.unsafe.types.UTF8String + +/** + * Unit tests for the two range-join indexes. [[IntervalIndex]] is the sweep used by + * point-in-range. [[PointIndex]] is the sorted array used by a single inequality. + * Operator-level behavior is covered by `RangeJoinSuite`. + */ +class RangeIndexSuite extends SparkFunSuite { + + private val intOrdering: Ordering[Any] = PhysicalDataType.ordering(IntegerType) + private val doubleOrdering: Ordering[Any] = PhysicalDataType.ordering(DoubleType) + + private def row(id: Int): InternalRow = InternalRow(id) + + private def interval(lo: Int, hi: Int, id: Int): Seq[RangeIndex.RangeEvent] = { + val stored = row(id) + RangeIndex.RangeEvent(lo, RangeIndex.RangeEvent.Start, stored, id) :: + RangeIndex.RangeEvent(hi, RangeIndex.RangeEvent.End, stored, id) :: Nil + } + + private def point(v: Int, id: Int): Seq[RangeIndex.RangeEvent] = + RangeIndex.RangeEvent(v, RangeIndex.RangeEvent.Point, row(id), id) :: Nil + + private def buildIntervals(events: Seq[RangeIndex.RangeEvent]): IntervalIndex = + IntervalIndex.build(intOrdering, events.toArray) + + private def ids(rows: Iterator[InternalRow]): List[Int] = rows.map(_.getInt(0)).toList + + private def probeIds(index: IntervalIndex, low: Any, high: Any): List[Int] = + ids(index.overlapping(low, high)) + + private def javaRoundTrip(index: IntervalIndex): (Array[Byte], IntervalIndex) = { + val bytes = new ByteArrayOutputStream() + val out = new ObjectOutputStream(bytes) + out.writeObject(index) + out.close() + val raw = bytes.toByteArray + val in = new ObjectInputStream(new ByteArrayInputStream(raw)) + try { + (raw, in.readObject().asInstanceOf[IntervalIndex]) + } finally { + in.close() + } + } + + test("toRangeEvents: point, interval, reversed, and null") { + val getters = List(RangeIndex.getValue(IntegerType, 0), RangeIndex.getValue(IntegerType, 1)) + val identity = new Projection { override def apply(input: InternalRow): InternalRow = input } + val eventifier = RangeIndex.toRangeEvents(getters, identity, intOrdering) + + assert(eventifier(InternalRow(5, 5), 0) == + Seq(RangeIndex.RangeEvent(5, RangeIndex.RangeEvent.Point, InternalRow(5, 5), 0))) + assert(eventifier(InternalRow(1, 3), 1) == + Seq( + RangeIndex.RangeEvent(1, RangeIndex.RangeEvent.Start, InternalRow(1, 3), 1), + RangeIndex.RangeEvent(3, RangeIndex.RangeEvent.End, InternalRow(1, 3), 1))) + // Normalized, not dropped: the join condition still accepts a pair whose sweep + // events would otherwise never exist. + assert(eventifier(InternalRow(3, 1), 2) == + Seq( + RangeIndex.RangeEvent(1, RangeIndex.RangeEvent.Start, InternalRow(3, 1), 2), + RangeIndex.RangeEvent(3, RangeIndex.RangeEvent.End, InternalRow(3, 1), 2))) + assert(eventifier(InternalRow(null, 1), 3).isEmpty) + } + + test("an inverted build interval reaches the windows the condition accepts") { + // (3, 1) is normalized to [1, 3]. A window with low < 1 and high > 3 accepts it, + // and dropping the row would lose the pair the join condition then rejects. Review Comment: fix -- 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]
