diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java b/common/src/main/java/org/apache/sedona/common/Functions.java index bcc46c8e4cb..a45ad8c2ee0 100644 --- a/common/src/main/java/org/apache/sedona/common/Functions.java +++ b/common/src/main/java/org/apache/sedona/common/Functions.java @@ -2088,6 +2088,18 @@ public static Long[] h3ToChildren(long cell, int resolution) { return H3Utils.h3.cellToChildren(cell, resolution).toArray(new Long[0]); } + /** + * Returns the center child of an H3 cell at the requested resolution. + * + * @param cell the H3 cell + * @param resolution the resolution of the center child, between the input cell's resolution and + * 15 + * @return the center child H3 cell, or the input cell when the resolutions are equal + */ + public static long h3ToCenterChild(long cell, int resolution) { + return H3Utils.h3.cellToCenterChild(cell, resolution); + } + /** * Compacts H3 cells by recursively replacing complete child sets with their parents. * diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java index 7917cb2c844..1b0c6c97fac 100644 --- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java +++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java @@ -1650,6 +1650,58 @@ public void h3ToChildrenInvalidResolution() { assertThrows(H3Exception.class, () -> Functions.h3ToChildren(cell, 4)); } + @Test + public void h3ToCenterChild() { + long parent = 601041811137363967L; + long immediate = Functions.h3ToCenterChild(parent, 6); + assertEquals(605545409825210367L, immediate); + assertTrue(H3Utils.h3.cellToChildren(parent, 6).contains(immediate)); + for (int resolution : new int[] {6, 8, 15}) { + long child = Functions.h3ToCenterChild(parent, resolution); + assertEquals(resolution, H3Utils.h3.getResolution(child)); + assertEquals(parent, Functions.h3ToParent(child, 5)); + assertEquals(child, Functions.h3ToCenterChild(immediate, resolution)); + } + // H3's documented center-child example, including a skip of two resolutions. + assertEquals(0x872834700ffffffL, Functions.h3ToCenterChild(0x85283473fffffffL, 7)); + } + + @Test + public void h3ToCenterChildPentagon() { + for (long pentagon : H3Utils.h3.getPentagons(5)) { + for (int resolution : new int[] {6, 8, 15}) { + long child = Functions.h3ToCenterChild(pentagon, resolution); + assertEquals(resolution, H3Utils.h3.getResolution(child)); + assertTrue(H3Utils.h3.isPentagon(child)); + assertEquals(pentagon, Functions.h3ToParent(child, 5)); + } + } + } + + @Test + public void h3ToCenterChildEqualResolutionAndBoundaries() { + for (int resolution : new int[] {0, 5, 15}) { + long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), resolution); + assertEquals(cell, Functions.h3ToCenterChild(cell, resolution)); + } + for (int resolution : new int[] {0, 14}) { + long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), resolution); + long child = Functions.h3ToCenterChild(cell, resolution + 1); + assertEquals(resolution + 1, H3Utils.h3.getResolution(child)); + assertEquals(cell, Functions.h3ToParent(child, resolution)); + } + } + + @Test + public void h3ToCenterChildInvalidResolution() { + long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), 5); + for (int resolution : new int[] {-1, 16}) { + assertThrows( + IllegalArgumentException.class, () -> Functions.h3ToCenterChild(cell, resolution)); + } + assertThrows(H3Exception.class, () -> Functions.h3ToCenterChild(cell, 4)); + } + @Test public void h3CompactRecursive() { long parent = H3Utils.coordinateToCell(new Coordinate(1, 2), 5); diff --git a/docs/api/flink/Geometry-Functions.md b/docs/api/flink/Geometry-Functions.md index 23b9c9f798e..164711a2433 100644 --- a/docs/api/flink/Geometry-Functions.md +++ b/docs/api/flink/Geometry-Functions.md @@ -351,6 +351,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil | [ST_H3CellIDs](Spatial-Indexing/ST_H3CellIDs.md) | `Array` | Cover the geometry by H3 cell IDs with the given resolution(level). To understand the cell statistics please refer to [H3 Doc](https://h3geo.org/docs/core-library/restable) H3 native fill functions... | v1.5.0 | | [ST_H3Compact](Spatial-Indexing/ST_H3Compact.md) | `Array` | Compacts H3 cells by recursively replacing complete child sets with their parents. | v2.0.0 | | [ST_H3KRing](Spatial-Indexing/ST_H3KRing.md) | `Array` | return the result of H3 function [gridDisk(cell, k)](https://h3geo.org/docs/api/traversal#griddisk). | v1.5.0 | +| [ST_H3ToCenterChild](Spatial-Indexing/ST_H3ToCenterChild.md) | Long | Returns the center child of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array` | Return the result of H3 function [cellsToMultiPolygon(cells)](https://h3geo.org/docs/api/regions#cellstolinkedmultipolygon--cellstomultipolygon). | v1.6.0 | | [ST_H3ToParent](Spatial-Indexing/ST_H3ToParent.md) | Long | Returns the parent of an H3 cell at the requested resolution. | v2.0.0 | diff --git a/docs/api/flink/Spatial-Indexing/ST_H3ToCenterChild.md b/docs/api/flink/Spatial-Indexing/ST_H3ToCenterChild.md new file mode 100644 index 00000000000..9e1d0f9e594 --- /dev/null +++ b/docs/api/flink/Spatial-Indexing/ST_H3ToCenterChild.md @@ -0,0 +1,58 @@ + + +# ST_H3ToCenterChild + +Introduction: Returns the center child of an H3 cell at the requested resolution using H3's [cellToCenterChild(cell, resolution)](https://h3geo.org/docs/api/hierarchy#celltocenterchild). + +The requested resolution must be between the input cell's resolution and 15, inclusive. An equal resolution returns the input cell. A lower resolution or a resolution outside this range raises an error. A null cell or resolution returns null. + +The function returns a single center descendant at the target resolution, including when skipping multiple resolutions. For a pentagon input, the center child is also a pentagon. + +Format: `ST_H3ToCenterChild(cell: Long, resolution: Int)` + +Return type: `Long` + +Since: `v2.0.0` + +SQL Example + +Find the resolution 6 center child of a resolution 5 hexagon: + +```sql +SELECT ST_H3ToCenterChild(601041811137363967, 6) +``` + +Output: + +```text +605545409825210367 +``` + +An equal resolution retains the input cell: + +```sql +SELECT ST_H3ToCenterChild(601041811137363967, 5) +``` + +Output: + +```text +601041811137363967 +``` diff --git a/docs/api/sql/Geometry-Functions.md b/docs/api/sql/Geometry-Functions.md index 70e9ae050ab..dcada7cbe4c 100644 --- a/docs/api/sql/Geometry-Functions.md +++ b/docs/api/sql/Geometry-Functions.md @@ -359,6 +359,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil | [ST_H3CellIDs](Spatial-Indexing/ST_H3CellIDs.md) | `Array` | Cover the geometry by H3 cell IDs with the given resolution(level). To understand the cell statistics please refer to [H3 Doc](https://h3geo.org/docs/core-library/restable) H3 native fill functions... | v1.5.0 | | [ST_H3Compact](Spatial-Indexing/ST_H3Compact.md) | `Array` | Compacts H3 cells by recursively replacing complete child sets with their parents. | v2.0.0 | | [ST_H3KRing](Spatial-Indexing/ST_H3KRing.md) | `Array` | return the result of H3 function [gridDisk(cell, k)](https://h3geo.org/docs/api/traversal#griddisk). | v1.5.0 | +| [ST_H3ToCenterChild](Spatial-Indexing/ST_H3ToCenterChild.md) | Long | Returns the center child of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array` | Return the result of H3 function [cellsToMultiPolygon(cells)](https://h3geo.org/docs/api/regions#cellstolinkedmultipolygon--cellstomultipolygon). | v1.6.0 | | [ST_H3ToParent](Spatial-Indexing/ST_H3ToParent.md) | Long | Returns the parent of an H3 cell at the requested resolution. | v2.0.0 | diff --git a/docs/api/sql/Spatial-Indexing/ST_H3ToCenterChild.md b/docs/api/sql/Spatial-Indexing/ST_H3ToCenterChild.md new file mode 100644 index 00000000000..9e1d0f9e594 --- /dev/null +++ b/docs/api/sql/Spatial-Indexing/ST_H3ToCenterChild.md @@ -0,0 +1,58 @@ + + +# ST_H3ToCenterChild + +Introduction: Returns the center child of an H3 cell at the requested resolution using H3's [cellToCenterChild(cell, resolution)](https://h3geo.org/docs/api/hierarchy#celltocenterchild). + +The requested resolution must be between the input cell's resolution and 15, inclusive. An equal resolution returns the input cell. A lower resolution or a resolution outside this range raises an error. A null cell or resolution returns null. + +The function returns a single center descendant at the target resolution, including when skipping multiple resolutions. For a pentagon input, the center child is also a pentagon. + +Format: `ST_H3ToCenterChild(cell: Long, resolution: Int)` + +Return type: `Long` + +Since: `v2.0.0` + +SQL Example + +Find the resolution 6 center child of a resolution 5 hexagon: + +```sql +SELECT ST_H3ToCenterChild(601041811137363967, 6) +``` + +Output: + +```text +605545409825210367 +``` + +An equal resolution retains the input cell: + +```sql +SELECT ST_H3ToCenterChild(601041811137363967, 5) +``` + +Output: + +```text +601041811137363967 +``` diff --git a/flink/src/main/java/org/apache/sedona/flink/Catalog.java b/flink/src/main/java/org/apache/sedona/flink/Catalog.java index 36cc63284f1..9b32ca826d1 100644 --- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java +++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java @@ -112,6 +112,7 @@ public static UserDefinedFunction[] getFuncs() { new Functions.ST_H3ToGeom(), new Functions.ST_H3ToParent(), new Functions.ST_H3ToChildren(), + new Functions.ST_H3ToCenterChild(), new Functions.ST_H3Uncompact(), new Functions.ST_H3Compact(), new Functions.ST_HilbertDistance(), diff --git a/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java b/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java index 5b4dae2ba99..55cbfdccb40 100644 --- a/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java +++ b/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java @@ -2986,6 +2986,17 @@ public Long[] eval( } } + public static class ST_H3ToCenterChild extends ScalarFunction { + @DataTypeHint(value = "BIGINT") + public Long eval( + @DataTypeHint("BIGINT") Long cell, @DataTypeHint("INTEGER") Integer resolution) { + if (cell == null || resolution == null) { + return null; + } + return org.apache.sedona.common.Functions.h3ToCenterChild(cell, resolution); + } + } + public static class ST_H3Compact extends ScalarFunction { @DataTypeHint(value = "ARRAY") public Long[] eval(@DataTypeHint("ARRAY") Long[] cells) { diff --git a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java index e040422d03e..2c3f1fadacd 100644 --- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java +++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java @@ -2424,6 +2424,52 @@ public void testH3ToChildren() { } } + @Test + public void testH3ToCenterChild() { + long parent = 601041811137363967L; + Row result = + first( + tableEnv.sqlQuery( + "SELECT ST_H3ToCenterChild(cell, resolution), ST_H3ToCenterChild(cell, 5), " + + "ST_H3ToParent(ST_H3ToCenterChild(cell, 15), 5) " + + "FROM (VALUES (" + + parent + + ", 6)) AS input(cell, resolution)")); + assertEquals(605545409825210367L, result.getField(0)); + assertEquals(parent, result.getField(1)); + assertEquals(parent, result.getField(2)); + + long pentagon = H3Utils.h3.getPentagons(5).iterator().next(); + long child = + (Long) + Objects.requireNonNull( + first(tableEnv.sqlQuery("SELECT ST_H3ToCenterChild(" + pentagon + ", 8)")) + .getField(0)); + assertTrue(H3Utils.h3.isPentagon(child)); + assertEquals(8, H3Utils.h3.getResolution(child)); + assertEquals(pentagon, H3Utils.h3.cellToParent(child, 5)); + + Row nulls = + first( + tableEnv.sqlQuery( + "SELECT ST_H3ToCenterChild(CAST(NULL AS BIGINT), 6), " + + "ST_H3ToCenterChild(" + + parent + + ", CAST(NULL AS INT)), " + + "ST_H3ToCenterChild(CAST(NULL AS BIGINT), CAST(NULL AS INT))")); + for (int i = 0; i < 3; i++) { + assertNull(nulls.getField(i)); + } + Table nullable = + tableEnv.sqlQuery( + "SELECT ST_H3ToCenterChild(cell, resolution) " + + "FROM (VALUES (CAST(NULL AS BIGINT), 6), (" + + parent + + ", CAST(NULL AS INT))) AS input(cell, resolution)"); + assertNull(first(nullable).getField(0)); + assertNull(last(nullable).getField(0)); + } + @Test public void testH3Compact() { long parent = H3Utils.coordinateToCell(new Coordinate(1, 2), 5); diff --git a/python/sedona/spark/sql/st_functions.py b/python/sedona/spark/sql/st_functions.py index cc5afa830ec..87d11fd048b 100644 --- a/python/sedona/spark/sql/st_functions.py +++ b/python/sedona/spark/sql/st_functions.py @@ -1030,6 +1030,20 @@ def ST_H3ToChildren( return _call_st_function("ST_H3ToChildren", args) +@validate_argument_types +def ST_H3ToCenterChild( + cell: Union[ColumnOrName, int], resolution: Union[ColumnOrName, int] +) -> Column: + """Return the center child of an H3 cell at the requested resolution. + :param cell: H3 cell + :param resolution: resolution of the center child, from the input resolution to 15 + :return: center child H3 cell, or the input cell when the resolutions are equal + :rtype: Long + """ + args = (cell, resolution) + return _call_st_function("ST_H3ToCenterChild", args) + + @validate_argument_types def ST_H3Uncompact( cells: Union[ColumnOrName, list], resolution: Union[ColumnOrName, int] diff --git a/python/tests/sql/test_dataframe_api.py b/python/tests/sql/test_dataframe_api.py index 3ba3ea475cb..1772e0dcc32 100644 --- a/python/tests/sql/test_dataframe_api.py +++ b/python/tests/sql/test_dataframe_api.py @@ -2021,6 +2021,26 @@ def test_st_h3_to_children(self): assert by_name == by_column == by_literal assert df.select(stf.ST_H3ToChildren("cell", 5)).first()[0] == [parent] + def test_st_h3_to_center_child(self): + parent = 601041811137363967 + df = self.spark.createDataFrame([(parent, 6)], ["cell", "resolution"]) + by_name, by_column, by_literal = df.select( + stf.ST_H3ToCenterChild("cell", 6), + stf.ST_H3ToCenterChild(f.col("cell"), f.col("resolution")), + stf.ST_H3ToCenterChild(parent, 6), + ).first() + assert by_name == by_column == by_literal == 605545409825210367 + assert df.select(stf.ST_H3ToCenterChild("cell", 5)).first()[0] == parent + nullable = self.spark.createDataFrame( + [(None, 6), (parent, None), (None, None)], "cell long, resolution int" + ) + assert all( + row[0] is None + for row in nullable.select( + stf.ST_H3ToCenterChild("cell", "resolution") + ).collect() + ) + def test_st_h3_compact(self): parent = 601041811137363967 children = self.spark.sql(f"SELECT ST_H3ToChildren({parent}, 6)").first()[0] diff --git a/python/tests/sql/test_function.py b/python/tests/sql/test_function.py index 27ee5b1e6d9..99f8906d66f 100644 --- a/python/tests/sql/test_function.py +++ b/python/tests/sql/test_function.py @@ -2461,6 +2461,22 @@ def test_st_h3_to_children(self): """).first() assert all(value is None for value in nulls) + def test_st_h3_to_center_child(self): + parent = 601041811137363967 + child, same, ancestor = self.spark.sql(f""" + SELECT ST_H3ToCenterChild({parent}, 6), + ST_H3ToCenterChild({parent}, 5), + ST_H3ToParent(ST_H3ToCenterChild({parent}, 15), 5) + """).first() + assert child == 605545409825210367 + assert same == ancestor == parent + nulls = self.spark.sql(f""" + SELECT ST_H3ToCenterChild(CAST(NULL AS BIGINT), 6), + ST_H3ToCenterChild({parent}, CAST(NULL AS INT)), + ST_H3ToCenterChild(CAST(NULL AS BIGINT), CAST(NULL AS INT)) + """).first() + assert all(value is None for value in nulls) + def test_st_h3_compact(self): parent = 601041811137363967 original, compacted, restored = self.spark.sql(f""" diff --git a/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala b/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala index d8f222c958b..1fd9f52daf0 100644 --- a/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala +++ b/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala @@ -319,6 +319,7 @@ object Catalog extends AbstractCatalog with Logging { function[ST_H3ToGeom](), function[ST_H3ToParent](), function[ST_H3ToChildren](), + function[ST_H3ToCenterChild](), function[ST_H3Uncompact](), function[ST_H3Compact](), function[ST_HilbertDistance](), diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala index 4e42814b440..092c6b99d15 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala @@ -1743,6 +1743,14 @@ private[apache] case class ST_H3ToChildren(inputExpressions: Seq[Expression]) } } +private[apache] case class ST_H3ToCenterChild(inputExpressions: Seq[Expression]) + extends InferredExpression(Functions.h3ToCenterChild _) { + + protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = { + copy(inputExpressions = newChildren) + } +} + private[apache] case class ST_H3Compact(inputExpressions: Seq[Expression]) extends InferredExpression(Functions.h3Compact _) { diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala index 65e008acc78..06b0201e661 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala @@ -320,6 +320,15 @@ object st_functions { def ST_H3ToChildren(cell: Long, resolution: Integer): Column = wrapExpression[ST_H3ToChildren](cell, resolution) + def ST_H3ToCenterChild(cell: Column, resolution: Column): Column = + wrapExpression[ST_H3ToCenterChild](cell, resolution) + + def ST_H3ToCenterChild(cell: Column, resolution: Integer): Column = + wrapExpression[ST_H3ToCenterChild](cell, resolution) + + def ST_H3ToCenterChild(cell: Long, resolution: Integer): Column = + wrapExpression[ST_H3ToCenterChild](cell, resolution) + def ST_H3Uncompact(cells: Column, resolution: Column): Column = wrapExpression[ST_H3Uncompact](cells, resolution) diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala b/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala index 25739b70736..08fd08e2ef2 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala @@ -2116,6 +2116,18 @@ class dataFrameAPITestScala extends TestBaseScala { } } + it("Passed ST_H3ToCenterChild") { + val cell = 601041811137363967L + val result = sparkSession + .sql(s"SELECT $cell AS cell, 6 AS resolution") + .select( + ST_H3ToCenterChild(col("cell"), col("resolution")), + ST_H3ToCenterChild(col("cell"), 6), + ST_H3ToCenterChild(cell, 6)) + .first() + (0 until 3).foreach(i => assert(result.getLong(i) == 605545409825210367L)) + } + it("Passed ST_H3Compact") { val cell = 601041811137363967L val children = org.apache.sedona.common.Functions.h3ToChildren(cell, 6).map(_.longValue()) diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala b/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala index 45796789e8b..c644b52e37c 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala @@ -185,6 +185,70 @@ class STH3Functions extends TestBaseScala with Matchers with GeometrySample with (0 until 3).foreach(i => result.isNullAt(i) should be(true)) } + it("should return the center child at the requested resolution") { + val inputs = Seq( + (601041811137363967L, 6, 605545409825210367L), + (0x85283473fffffffL, 7, 0x872834700ffffffL)) + inputs + .toDF("cell", "resolution", "expected") + .select( + col("cell"), + col("resolution"), + col("expected"), + expr("ST_H3ToCenterChild(cell, resolution)")) + .collect() + .foreach { row => + row.getLong(3) should equal(row.getLong(2)) + H3Utils.h3.getResolution(row.getLong(3)) should equal(row.getInt(1)) + Functions.h3ToParent(row.getLong(3), 5) should equal(row.getLong(0)) + } + val result = sparkSession + .sql("""SELECT + |ST_H3ToCenterChild(601041811137363967, 5), + |ST_H3ToParent(ST_H3ToCenterChild(601041811137363967, 15), 5) + |""".stripMargin) + .first() + result.getLong(0) should equal(601041811137363967L) + result.getLong(1) should equal(601041811137363967L) + } + + it("should return a pentagon center child of a pentagon") { + val pentagon = H3Utils.h3.getPentagons(5).iterator().next().longValue() + val child = sparkSession + .sql(s"SELECT ST_H3ToCenterChild($pentagon, 8)") + .first() + .getLong(0) + H3Utils.h3.isPentagon(child) should be(true) + H3Utils.h3.getResolution(child) should equal(8) + Functions.h3ToParent(child, 5) should equal(pentagon) + } + + it("should propagate null inputs to ST_H3ToCenterChild") { + val result = sparkSession + .sql("""SELECT + |ST_H3ToCenterChild(CAST(NULL AS BIGINT), 6), + |ST_H3ToCenterChild(601041811137363967, CAST(NULL AS INT)), + |ST_H3ToCenterChild(CAST(NULL AS BIGINT), CAST(NULL AS INT)) + |""".stripMargin) + .first() + (0 until 3).foreach(i => result.isNullAt(i) should be(true)) + Seq((None, Some(6)), (Some(601041811137363967L), None), (None, None)) + .toDF("cell", "resolution") + .select(expr("ST_H3ToCenterChild(cell, resolution)")) + .collect() + .foreach(row => row.isNullAt(0) should be(true)) + } + + it("should reject a center-child resolution coarser than the input cell") { + val error = intercept[Exception] { + sparkSession.sql("SELECT ST_H3ToCenterChild(601041811137363967, 4)").collect() + } + Iterator + .iterate[Throwable](error)(_.getCause) + .takeWhile(_ != null) + .exists(_.isInstanceOf[com.uber.h3core.exceptions.H3Exception]) should be(true) + } + it("should recursively compact complete child sets and retain partial sets") { val parent = H3Utils.coordinateToCell(new Coordinate(1, 2), 5) val other = H3Utils.coordinateToCell(new Coordinate(10, 20), 7)