|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.sedona.sql |
| 20 | + |
| 21 | +import org.apache.spark.sql.DataFrame |
| 22 | +import org.apache.spark.sql.functions.{broadcast, expr} |
| 23 | +import org.apache.spark.sql.sedona_sql.strategy.join.{BroadcastIndexJoinExec, RangeJoinExec} |
| 24 | + |
| 25 | +class Box3DJoinSuite extends TestBaseScala { |
| 26 | + |
| 27 | + import Box3DJoinSuite.TestBox3D |
| 28 | + |
| 29 | + /** |
| 30 | + * Left and right boxes wired so result counts are predictable across the XY filter and the |
| 31 | + * Z-axis refine step. L4 is the discriminating row: same XY footprint as L1/L2 so the 2D R-tree |
| 32 | + * pairs it with every right in the (0..10) XY cluster, but its Z range is disjoint from every |
| 33 | + * right's Z (above R1/R2's Z, below R4's Z) so the refine must reject all those candidates. |
| 34 | + * |
| 35 | + * - L1=(0..10, 0..10, 0..10), L2=same — dense cluster on Z=(0..10). |
| 36 | + * - L3=(20..30, 20..30, 0..10) — XY-disjoint outlier. |
| 37 | + * - L4=(0..10, 0..10, 50..60) — XY in the cluster, Z disjoint from every right side. Every |
| 38 | + * L4-R* candidate the R-tree emits has to be killed by the Z refine. |
| 39 | + * |
| 40 | + * - R1=(5..15, 5..15, 5..15), R2=(2..8, 2..8, 2..8) — overlap L1/L2 in XY *and* Z. |
| 41 | + * - R3=(40..50, 40..50, 0..10) — XY-disjoint. |
| 42 | + * - R4=(2..8, 2..8, 100..200) — XY in the cluster, Z separated from every left. |
| 43 | + * |
| 44 | + * True intersection pairs: 4 — (L1,R1), (L1,R2), (L2,R1), (L2,R2). Containment pairs |
| 45 | + * (closed-interval, all three axes): 2 — (L1,R2), (L2,R2). R1 extends past L1/L2 on all three |
| 46 | + * axes; R4 and L4 are Z-disjoint from anything they could contain or be contained by. |
| 47 | + */ |
| 48 | + private def leftBoxes: DataFrame = { |
| 49 | + import sparkSession.implicits._ |
| 50 | + Seq( |
| 51 | + TestBox3D(1, 0, 0, 0, 10, 10, 10), |
| 52 | + TestBox3D(2, 0, 0, 0, 10, 10, 10), |
| 53 | + TestBox3D(3, 20, 20, 0, 30, 30, 10), |
| 54 | + TestBox3D(4, 0, 0, 50, 10, 10, 60)) |
| 55 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 56 | + .selectExpr( |
| 57 | + "id", |
| 58 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 59 | + } |
| 60 | + |
| 61 | + private def rightBoxes: DataFrame = { |
| 62 | + import sparkSession.implicits._ |
| 63 | + Seq( |
| 64 | + TestBox3D(11, 5, 5, 5, 15, 15, 15), |
| 65 | + TestBox3D(12, 2, 2, 2, 8, 8, 8), |
| 66 | + TestBox3D(13, 40, 40, 0, 50, 50, 10), |
| 67 | + TestBox3D(14, 2, 2, 100, 8, 8, 200)) |
| 68 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 69 | + .selectExpr( |
| 70 | + "id", |
| 71 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 72 | + } |
| 73 | + |
| 74 | + describe("Box3D spatial join") { |
| 75 | + |
| 76 | + it("ST_Intersects: broadcast index join filters Z-disjoint candidates") { |
| 77 | + val joined = leftBoxes |
| 78 | + .alias("L") |
| 79 | + .join(broadcast(rightBoxes.alias("R")), expr("ST_Intersects(L.box, R.box)")) |
| 80 | + |
| 81 | + assert(joined.queryExecution.executedPlan.collectFirst { case _: BroadcastIndexJoinExec => |
| 82 | + true |
| 83 | + }.isDefined) |
| 84 | + // 4 true intersections. The L4 row produces three XY-overlapping candidates (L4-R1, |
| 85 | + // L4-R2, L4-R4) that the 2D R-tree emits and the Z refine rejects. |
| 86 | + assert(joined.count() == 4) |
| 87 | + } |
| 88 | + |
| 89 | + it("ST_Intersects: argument order is symmetric") { |
| 90 | + val joined = leftBoxes |
| 91 | + .alias("L") |
| 92 | + .join(broadcast(rightBoxes.alias("R")), expr("ST_Intersects(R.box, L.box)")) |
| 93 | + assert(joined.count() == 4) |
| 94 | + } |
| 95 | + |
| 96 | + it("ST_Contains: broadcast index join uses Box3D containment semantics") { |
| 97 | + val joined = leftBoxes |
| 98 | + .alias("L") |
| 99 | + .join(broadcast(rightBoxes.alias("R")), expr("ST_Contains(L.box, R.box)")) |
| 100 | + |
| 101 | + assert(joined.queryExecution.executedPlan.collectFirst { case _: BroadcastIndexJoinExec => |
| 102 | + true |
| 103 | + }.isDefined) |
| 104 | + // Only R2 is fully inside L1 / L2 on all three axes. R1 sticks out in X/Y/Z; R4 is |
| 105 | + // disjoint in Z. |
| 106 | + assert(joined.count() == 2) |
| 107 | + } |
| 108 | + |
| 109 | + it("ST_Contains: edge-touching boxes count (closed-interval semantics)") { |
| 110 | + import sparkSession.implicits._ |
| 111 | + val outer = Seq(TestBox3D(1, 0, 0, 0, 10, 10, 10)) |
| 112 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 113 | + .selectExpr( |
| 114 | + "id", |
| 115 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 116 | + // Inner shares all three faces with the outer on the high side. |
| 117 | + val inner = Seq(TestBox3D(2, 5, 5, 5, 10, 10, 10)) |
| 118 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 119 | + .selectExpr( |
| 120 | + "id", |
| 121 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 122 | + val joined = outer |
| 123 | + .alias("O") |
| 124 | + .join(broadcast(inner.alias("I")), expr("ST_Contains(O.box, I.box)")) |
| 125 | + assert(joined.count() == 1) |
| 126 | + } |
| 127 | + |
| 128 | + it("ST_Intersects: non-broadcast range join produces the same count") { |
| 129 | + val joined = leftBoxes |
| 130 | + .alias("L") |
| 131 | + .join(rightBoxes.alias("R"), expr("ST_Intersects(L.box, R.box)")) |
| 132 | + |
| 133 | + assert(joined.queryExecution.executedPlan.collectFirst { case _: RangeJoinExec => |
| 134 | + true |
| 135 | + }.isDefined) |
| 136 | + assert(joined.count() == 4) |
| 137 | + } |
| 138 | + |
| 139 | + it("Inverted Box3D bounds in a join throw IllegalArgumentException") { |
| 140 | + import sparkSession.implicits._ |
| 141 | + val ordered = Seq(TestBox3D(1, 0, 0, 0, 10, 10, 10)) |
| 142 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 143 | + .selectExpr( |
| 144 | + "id", |
| 145 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 146 | + // Z inverted on the right side. |
| 147 | + val invertedZ = Seq(TestBox3D(2, 0, 0, 10, 10, 10, 0)) |
| 148 | + .toDF("id", "xmin", "ymin", "zmin", "xmax", "ymax", "zmax") |
| 149 | + .selectExpr( |
| 150 | + "id", |
| 151 | + "ST_3DMakeBox(ST_PointZ(xmin, ymin, zmin), ST_PointZ(xmax, ymax, zmax)) AS box") |
| 152 | + val ex = intercept[Exception] { |
| 153 | + ordered |
| 154 | + .alias("L") |
| 155 | + .join(broadcast(invertedZ.alias("R")), expr("ST_Intersects(L.box, R.box)")) |
| 156 | + .collect() |
| 157 | + } |
| 158 | + assert( |
| 159 | + Iterator |
| 160 | + .iterate(ex: Throwable)(_.getCause) |
| 161 | + .takeWhile(_ != null) |
| 162 | + .exists(_.isInstanceOf[IllegalArgumentException])) |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +object Box3DJoinSuite { |
| 168 | + case class TestBox3D( |
| 169 | + id: Int, |
| 170 | + xmin: Double, |
| 171 | + ymin: Double, |
| 172 | + zmin: Double, |
| 173 | + xmax: Double, |
| 174 | + ymax: Double, |
| 175 | + zmax: Double) |
| 176 | +} |
0 commit comments