-
Notifications
You must be signed in to change notification settings - Fork 765
[GH-2511][Python] Add to_sedonadb() for SedonaSpark to SedonaDB conversion #2515
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
5beaa27
27e1614
221a9cd
b8cf8c1
9cebd3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -71,3 +71,27 @@ | |||||||||||||||||||||||||||||||
| from sedona.spark.utils.adapter import Adapter | ||||||||||||||||||||||||||||||||
| from sedona.spark.utils.spatial_rdd_parser import GeoData | ||||||||||||||||||||||||||||||||
| from sedona.spark.utils.structured_adapter import StructuredAdapter | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| from pyspark.sql import DataFrame | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def to_sedonadb(self, connection=None): | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| Converts a SedonaSpark DataFrame to a SedonaDB DataFrame. | ||||||||||||||||||||||||||||||||
| :param connection: Optional SedonaDB connection object. If None, a new connection will be created. | ||||||||||||||||||||||||||||||||
| :return: SedonaDB DataFrame | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||
| import sedona.db | ||||||||||||||||||||||||||||||||
| except ImportError: | ||||||||||||||||||||||||||||||||
| raise ImportError( | ||||||||||||||||||||||||||||||||
| "SedonaDB is not installed. Please install it using `pip install sedona-db`." | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if connection is None: | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| try: | |
| import sedona.db | |
| except ImportError: | |
| raise ImportError( | |
| "SedonaDB is not installed. Please install it using `pip install sedona-db`." | |
| ) | |
| if connection is None: | |
| if connection is None: | |
| try: | |
| import sedona.db | |
| except ImportError: | |
| raise ImportError( | |
| "SedonaDB is not installed. Please install it using `pip install sedona-db`." | |
| ) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 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 unittest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from unittest.mock import MagicMock, patch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pyspark.sql import SparkSession, DataFrame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from sedona.spark import * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestToSedonaDB(unittest.TestCase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def setUp(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Mock sedona.db to avoid ImportError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.mock_sedona_db = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sys.modules["sedona.db"] = self.mock_sedona_db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sedona | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sedona.db = self.mock_sedona_db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.spark = SparkSession.builder.getOrCreate() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def tearDown(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "sedona.db" in sys.modules: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| del sys.modules["sedona.db"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sedona | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hasattr(sedona, "db"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Mock sedona.db to avoid ImportError | |
| self.mock_sedona_db = MagicMock() | |
| sys.modules["sedona.db"] = self.mock_sedona_db | |
| import sedona | |
| sedona.db = self.mock_sedona_db | |
| self.spark = SparkSession.builder.getOrCreate() | |
| def tearDown(self): | |
| if "sedona.db" in sys.modules: | |
| del sys.modules["sedona.db"] | |
| import sedona | |
| if hasattr(sedona, "db"): | |
| # Preserve any existing sedona.db module and sedona.db attribute | |
| self._original_sedona_db_module = sys.modules.get("sedona.db") | |
| import sedona | |
| self._had_sedona_db_attr = hasattr(sedona, "db") | |
| self._original_sedona_db_attr = getattr(sedona, "db", None) | |
| # Mock sedona.db to avoid ImportError | |
| self.mock_sedona_db = MagicMock() | |
| sys.modules["sedona.db"] = self.mock_sedona_db | |
| sedona.db = self.mock_sedona_db | |
| self.spark = SparkSession.builder.getOrCreate() | |
| def tearDown(self): | |
| # Restore prior sys.modules['sedona.db'] state | |
| if self._original_sedona_db_module is not None: | |
| sys.modules["sedona.db"] = self._original_sedona_db_module | |
| elif "sedona.db" in sys.modules: | |
| del sys.modules["sedona.db"] | |
| import sedona | |
| # Restore prior sedona.db attribute state | |
| if self._had_sedona_db_attr: | |
| sedona.db = self._original_sedona_db_attr | |
| elif hasattr(sedona, "db"): |
Copilot
AI
Mar 26, 2026
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.
The test creates/gets a SparkSession in setUp but never stops it in tearDown. This can leak resources across the test process and cause flakiness/hangs in CI. Add self.spark.stop() (or use the repo’s existing Spark test fixture/session management if one exists) so the session lifecycle is properly bounded per test/module.
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.
The function imports
sedona.dbunconditionally, even when aconnectionis explicitly provided. That makes the method fail withImportErrorin cases where a caller passes a compatible connection object (or in environments doing partial installs) even thoughsedona.dbisn’t required on that code path. Move theimport sedona.db+connect()logic inside theif connection is None:branch so only the auto-connect path requires the SedonaDB package.