Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/docs/source/reference/pyspark.sql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,16 @@ Misc Functions
version


Geospatial ST Functions
-----------------------
.. autosummary::
:toctree: api/

st_asbinary
st_geogfromwkb
st_geomfromwkb


UDF, UDTF and UDT
-----------------
.. autosummary::
Expand Down
24 changes: 24 additions & 0 deletions python/pyspark/sql/connect/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,30 @@ def bitmap_and_agg(col: "ColumnOrName") -> Column:
bitmap_and_agg.__doc__ = pysparkfuncs.bitmap_and_agg.__doc__


# Geospatial ST Functions


def st_asbinary(geo: "ColumnOrName") -> Column:
return _invoke_function_over_columns("st_asbinary", geo)


st_asbinary.__doc__ = pysparkfuncs.st_asbinary.__doc__


def st_geogfromwkb(wkb: "ColumnOrName") -> Column:
return _invoke_function_over_columns("st_geogfromwkb", wkb)


st_geogfromwkb.__doc__ = pysparkfuncs.st_geogfromwkb.__doc__


def st_geomfromwkb(wkb: "ColumnOrName") -> Column:
return _invoke_function_over_columns("st_geomfromwkb", wkb)


st_geomfromwkb.__doc__ = pysparkfuncs.st_geomfromwkb.__doc__


# Call Functions


Expand Down
5 changes: 5 additions & 0 deletions python/pyspark/sql/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@
"UserDefinedFunction",
"UserDefinedTableFunction",
"arrow_udf",
# Geospatial ST Functions
"st_asbinary",
"st_geogfromwkb",
"st_geomfromwkb",
# Call Functions
"call_udf",
"pandas_udf",
"udf",
Expand Down
73 changes: 73 additions & 0 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25901,6 +25901,79 @@ def bucket(numBuckets: Union[Column, int], col: "ColumnOrName") -> Column:
return partitioning.bucket(numBuckets, col)


# Geospatial ST Functions


@_try_remote_functions
def st_asbinary(geo: "ColumnOrName") -> Column:
"""Returns the input GEOGRAPHY or GEOMETRY value in WKB format.

.. versionadded:: 4.1.0

Parameters
----------
geo : :class:`~pyspark.sql.Column` or str
A geospatial value, either a GEOGRAPHY or a GEOMETRY.

Examples
--------
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(bytes.fromhex('0101000000000000000000F03F0000000000000040'),)], ['wkb']) # noqa
>>> df.select(sf.hex(sf.st_asbinary(sf.st_geogfromwkb('wkb'))).alias('result')).collect()
[Row(result='0101000000000000000000F03F0000000000000040')]
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(bytes.fromhex('0101000000000000000000F03F0000000000000040'),)], ['wkb']) # noqa
>>> df.select(sf.hex(sf.st_asbinary(sf.st_geomfromwkb('wkb'))).alias('result')).collect()
[Row(result='0101000000000000000000F03F0000000000000040')]
"""
return _invoke_function_over_columns("st_asbinary", geo)


@_try_remote_functions
def st_geogfromwkb(wkb: "ColumnOrName") -> Column:
"""Parses the input WKB description and returns the corresponding GEOGRAPHY value.

.. versionadded:: 4.1.0

Parameters
----------
wkb : :class:`~pyspark.sql.Column` or str
A BINARY value in WKB format, representing a GEOGRAPHY value.

Examples
--------
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(bytes.fromhex('0101000000000000000000F03F0000000000000040'),)], ['wkb']) # noqa
>>> df.select(sf.hex(sf.st_asbinary(sf.st_geogfromwkb('wkb'))).alias('result')).collect()
[Row(result='0101000000000000000000F03F0000000000000040')]
"""
return _invoke_function_over_columns("st_geogfromwkb", wkb)


@_try_remote_functions
def st_geomfromwkb(wkb: "ColumnOrName") -> Column:
"""Parses the input WKB description and returns the corresponding GEOMETRY value.

.. versionadded:: 4.1.0

Parameters
----------
wkb : :class:`~pyspark.sql.Column` or str
A BINARY value in WKB format, representing a GEOMETRY value.

Examples
--------
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(bytes.fromhex('0101000000000000000000F03F0000000000000040'),)], ['wkb']) # noqa
>>> df.select(sf.hex(sf.st_asbinary(sf.st_geomfromwkb('wkb'))).alias('result')).collect()
[Row(result='0101000000000000000000F03F0000000000000040')]
"""
return _invoke_function_over_columns("st_geomfromwkb", wkb)


# Call Functions


@_try_remote_functions
def call_udf(udfName: str, *cols: "ColumnOrName") -> Column:
"""
Expand Down
17 changes: 17 additions & 0 deletions python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,23 @@ def test_string_validation(self):
result_try_validate_utf8 = df.select(F.try_validate_utf8(df.a).alias("r"))
assertDataFrameEqual([Row(r="abc")], result_try_validate_utf8)

# Geospatial ST Functions

def test_st_asbinary(self):
df = self.spark.createDataFrame(
[(bytes.fromhex("0101000000000000000000F03F0000000000000040"),)],
["wkb"],
)
results = df.select(
F.hex(F.st_asbinary(F.st_geogfromwkb("wkb"))),
F.hex(F.st_asbinary(F.st_geomfromwkb("wkb"))),
).collect()
expected = Row(
"0101000000000000000000F03F0000000000000040",
"0101000000000000000000F03F0000000000000040",
)
self.assertEqual(results, [expected])


class FunctionsTests(ReusedSQLTestCase, FunctionsTestsMixin):
pass
Expand Down