-
Notifications
You must be signed in to change notification settings - Fork 706
[SEDONA-721] Add Sedona vectorized udf for Python #1859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
286a610
SEDONA-721 Add Sedona vectorized udf.
Imbruced f281ede
SEDONA-721 Add documentation
Imbruced 932c8ad
SEDONA-721 Add documentation
Imbruced 2749a74
SEDONA-721 Add documentation
Imbruced 264305a
Update .github/workflows/java.yml
Imbruced d5b9f5f
SEDONA-721 Apply requested changes.
Imbruced 6277cbd
SEDONA-721 Apply requested changes.
Imbruced 95e129a
SEDONA-721 Apply requested changes.
Imbruced bb0a8fb
SEDONA-721 Apply requested changes.
Imbruced f1d468d
SEDONA-721 Apply requested changes.
Imbruced 7219c81
SEDONA-721 Apply requested changes.
Imbruced e1a32d7
SEDONA-721 Apply requested changes.
Imbruced 9b34875
SEDONA-721 Apply requested changes.
Imbruced File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# 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. | ||
|
||
|
||
import inspect | ||
from enum import Enum | ||
|
||
import pandas as pd | ||
|
||
from sedona.sql.types import GeometryType | ||
from sedona.utils import geometry_serde | ||
from pyspark.sql.udf import UserDefinedFunction | ||
from pyspark.sql.types import DataType | ||
from shapely.geometry.base import BaseGeometry | ||
|
||
|
||
SEDONA_SCALAR_EVAL_TYPE = 5200 | ||
SEDONA_PANDAS_ARROW_NAME = "SedonaPandasArrowUDF" | ||
|
||
|
||
class SedonaUDFType(Enum): | ||
SHAPELY_SCALAR = "ShapelyScalar" | ||
GEO_SERIES = "GeoSeries" | ||
|
||
|
||
class InvalidSedonaUDFType(Exception): | ||
pass | ||
|
||
|
||
sedona_udf_to_eval_type = { | ||
SedonaUDFType.SHAPELY_SCALAR: SEDONA_SCALAR_EVAL_TYPE, | ||
SedonaUDFType.GEO_SERIES: SEDONA_SCALAR_EVAL_TYPE, | ||
} | ||
|
||
|
||
def sedona_vectorized_udf( | ||
return_type: DataType, udf_type: SedonaUDFType = SedonaUDFType.SHAPELY_SCALAR | ||
): | ||
import geopandas as gpd | ||
|
||
def apply_fn(fn): | ||
function_signature = inspect.signature(fn) | ||
serialize_geom = False | ||
deserialize_geom = False | ||
|
||
if isinstance(return_type, GeometryType): | ||
serialize_geom = True | ||
|
||
if issubclass(function_signature.return_annotation, BaseGeometry): | ||
serialize_geom = True | ||
|
||
if issubclass(function_signature.return_annotation, gpd.GeoSeries): | ||
serialize_geom = True | ||
|
||
for param in function_signature.parameters.values(): | ||
if issubclass(param.annotation, BaseGeometry): | ||
deserialize_geom = True | ||
|
||
if issubclass(param.annotation, gpd.GeoSeries): | ||
deserialize_geom = True | ||
|
||
if udf_type == SedonaUDFType.SHAPELY_SCALAR: | ||
return _apply_shapely_series_udf( | ||
fn, return_type, serialize_geom, deserialize_geom | ||
) | ||
|
||
if udf_type == SedonaUDFType.GEO_SERIES: | ||
return _apply_geo_series_udf( | ||
fn, return_type, serialize_geom, deserialize_geom | ||
) | ||
|
||
raise InvalidSedonaUDFType(f"Invalid UDF type: {udf_type}") | ||
|
||
return apply_fn | ||
|
||
|
||
def _apply_shapely_series_udf( | ||
fn, return_type: DataType, serialize_geom: bool, deserialize_geom: bool | ||
): | ||
def apply(series: pd.Series) -> pd.Series: | ||
applied = series.apply( | ||
lambda x: ( | ||
fn(geometry_serde.deserialize(x)[0]) if deserialize_geom else fn(x) | ||
) | ||
) | ||
|
||
return applied.apply( | ||
lambda x: geometry_serde.serialize(x) if serialize_geom else x | ||
) | ||
|
||
udf = UserDefinedFunction( | ||
apply, return_type, "SedonaPandasArrowUDF", evalType=SEDONA_SCALAR_EVAL_TYPE | ||
) | ||
|
||
return udf | ||
|
||
|
||
def _apply_geo_series_udf( | ||
fn, return_type: DataType, serialize_geom: bool, deserialize_geom: bool | ||
): | ||
import geopandas as gpd | ||
|
||
def apply(series: pd.Series) -> pd.Series: | ||
series_data = series | ||
if deserialize_geom: | ||
series_data = gpd.GeoSeries( | ||
series.apply(lambda x: geometry_serde.deserialize(x)[0]) | ||
) | ||
|
||
return fn(series_data).apply( | ||
lambda x: geometry_serde.serialize(x) if serialize_geom else x | ||
) | ||
|
||
return UserDefinedFunction( | ||
apply, return_type, "SedonaPandasArrowUDF", evalType=SEDONA_SCALAR_EVAL_TYPE | ||
) | ||
|
||
|
||
def deserialize_geometry_if_geom(data): | ||
if isinstance(data, BaseGeometry): | ||
return geometry_serde.deserialize(data)[0] | ||
|
||
return data | ||
|
||
|
||
def serialize_to_geometry_if_geom(data, return_type: DataType): | ||
if isinstance(return_type, GeometryType): | ||
return geometry_serde.serialize(data) | ||
|
||
return data |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apache File header please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize that it is not added automatically