|
| 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 | +class Box3DPredicateSuite extends TestBaseScala { |
| 22 | + |
| 23 | + describe("Box3D predicates") { |
| 24 | + |
| 25 | + it("ST_3DBoxIntersects covers overlap, face-, edge- and corner-touching") { |
| 26 | + val row = sparkSession |
| 27 | + .sql(""" |
| 28 | + WITH t AS ( |
| 29 | + SELECT |
| 30 | + ST_3DMakeBox(ST_PointZ(0,0,0), ST_PointZ(5,5,5)) AS a, |
| 31 | + ST_3DMakeBox(ST_PointZ(1,1,1), ST_PointZ(2,2,2)) AS inside, |
| 32 | + ST_3DMakeBox(ST_PointZ(3,3,3), ST_PointZ(7,7,7)) AS overlap, |
| 33 | + ST_3DMakeBox(ST_PointZ(5,0,0), ST_PointZ(10,5,5)) AS face, |
| 34 | + ST_3DMakeBox(ST_PointZ(5,5,5), ST_PointZ(10,10,10)) AS corner, |
| 35 | + ST_3DMakeBox(ST_PointZ(6,6,6), ST_PointZ(7,7,7)) AS disjoint |
| 36 | + ) |
| 37 | + SELECT |
| 38 | + ST_3DBoxIntersects(a, inside), |
| 39 | + ST_3DBoxIntersects(a, overlap), |
| 40 | + ST_3DBoxIntersects(a, face), |
| 41 | + ST_3DBoxIntersects(a, corner), |
| 42 | + ST_3DBoxIntersects(a, disjoint) |
| 43 | + FROM t |
| 44 | + """) |
| 45 | + .collect()(0) |
| 46 | + assert(row.getBoolean(0)) |
| 47 | + assert(row.getBoolean(1)) |
| 48 | + assert(row.getBoolean(2)) |
| 49 | + assert(row.getBoolean(3)) |
| 50 | + assert(!row.getBoolean(4)) |
| 51 | + } |
| 52 | + |
| 53 | + it("ST_3DBoxContains is closed-interval (equal boxes contain each other)") { |
| 54 | + val row = sparkSession |
| 55 | + .sql(""" |
| 56 | + WITH t AS ( |
| 57 | + SELECT |
| 58 | + ST_3DMakeBox(ST_PointZ(0,0,0), ST_PointZ(5,5,5)) AS a, |
| 59 | + ST_3DMakeBox(ST_PointZ(1,1,1), ST_PointZ(2,2,2)) AS inside, |
| 60 | + ST_3DMakeBox(ST_PointZ(3,3,3), ST_PointZ(7,7,7)) AS overlap, |
| 61 | + ST_3DMakeBox(ST_PointZ(0,0,0), ST_PointZ(5,5,5)) AS equal |
| 62 | + ) |
| 63 | + SELECT |
| 64 | + ST_3DBoxContains(a, inside), |
| 65 | + ST_3DBoxContains(a, overlap), |
| 66 | + ST_3DBoxContains(a, equal) |
| 67 | + FROM t |
| 68 | + """) |
| 69 | + .collect()(0) |
| 70 | + assert(row.getBoolean(0)) |
| 71 | + assert(!row.getBoolean(1)) |
| 72 | + assert(row.getBoolean(2)) |
| 73 | + } |
| 74 | + |
| 75 | + it("ST_3DBoxIntersects rejects inverted bounds") { |
| 76 | + val ex = intercept[Exception] { |
| 77 | + sparkSession |
| 78 | + .sql( |
| 79 | + "SELECT ST_3DBoxIntersects(" + |
| 80 | + "ST_3DMakeBox(ST_PointZ(5,0,0), ST_PointZ(0,5,5)), " + |
| 81 | + "ST_3DMakeBox(ST_PointZ(0,0,0), ST_PointZ(1,1,1)))") |
| 82 | + .collect() |
| 83 | + } |
| 84 | + assert( |
| 85 | + Iterator |
| 86 | + .iterate(ex: Throwable)(_.getCause) |
| 87 | + .takeWhile(_ != null) |
| 88 | + .exists(_.isInstanceOf[IllegalArgumentException])) |
| 89 | + } |
| 90 | + |
| 91 | + it("Predicates propagate NULL when either argument is NULL") { |
| 92 | + val row = sparkSession |
| 93 | + .sql(""" |
| 94 | + WITH t AS ( |
| 95 | + SELECT |
| 96 | + ST_3DMakeBox(ST_PointZ(0,0,0), ST_PointZ(5,5,5)) AS a, |
| 97 | + ST_3DMakeBox(ST_GeomFromText(NULL), ST_PointZ(1,1,1)) AS n |
| 98 | + ) |
| 99 | + SELECT ST_3DBoxIntersects(a, n), ST_3DBoxContains(a, n) FROM t |
| 100 | + """) |
| 101 | + .collect()(0) |
| 102 | + assert(row.isNullAt(0)) |
| 103 | + assert(row.isNullAt(1)) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments