-
Notifications
You must be signed in to change notification settings - Fork 2
feat(duckdb): Bake in spatial and excel extensions
#36
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
m1so
merged 5 commits into
main
from
michalbaumgartner/blu-5180-bake-in-duckdb-extensions-to-not-load-them-from-internet
Nov 20, 2025
+212
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e136775
feat(duckdb): Bake in `spatial` and `excel` extensions
m1so 49419a7
fix: Use `load_extension` directly
m1so 7ce9993
chore: Address code review suggestions
m1so 1b8269a
chore: Test cases where extension loading or importing fails
m1so 4d932e7
fix: NumPy boolean comparison to be flake8 compliant
m1so 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,137 @@ | ||
| from contextlib import contextmanager | ||
| from unittest import mock | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
|
|
||
| from deepnote_toolkit.sql.duckdb_sql import ( | ||
| _get_duckdb_connection, | ||
| _set_sample_size, | ||
| _set_scan_all_frames, | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def fresh_duckdb_connection(): | ||
| import deepnote_toolkit.sql.duckdb_sql as duckdb_sql_module | ||
|
|
||
| duckdb_sql_module._DEEPNOTE_DUCKDB_CONNECTION = None | ||
| conn = _get_duckdb_connection() | ||
|
|
||
| try: | ||
| yield conn | ||
| finally: | ||
| conn.close() | ||
| duckdb_sql_module._DEEPNOTE_DUCKDB_CONNECTION = None | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def duckdb_connection(): | ||
| with fresh_duckdb_connection() as conn: | ||
| yield conn | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("extension_name", ["spatial", "excel"]) | ||
| def test_extension_installed_and_loadable(duckdb_connection, extension_name): | ||
| result = duckdb_connection.execute( | ||
| f"SELECT installed FROM duckdb_extensions() WHERE extension_name = '{extension_name}'" | ||
| ).fetchone() | ||
|
|
||
| assert ( | ||
| result is not None | ||
| ), f"{extension_name} extension should be found in duckdb_extensions()" | ||
| assert result[0] is True, f"{extension_name} extension should be installed" | ||
|
|
||
| loaded_result = duckdb_connection.execute( | ||
| f"SELECT loaded FROM duckdb_extensions() WHERE extension_name = '{extension_name}'" | ||
| ).fetchone() | ||
| assert loaded_result[0] is True, f"{extension_name} extension should be loaded" | ||
m1so marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_connection_singleton_pattern(): | ||
| conn1 = _get_duckdb_connection() | ||
| conn2 = _get_duckdb_connection() | ||
|
|
||
| assert conn1 is conn2, "Connection should be a singleton" | ||
|
|
||
|
|
||
| def test_set_sample_size(duckdb_connection): | ||
| _set_sample_size(duckdb_connection, 50000) | ||
| result = duckdb_connection.execute( | ||
| "SELECT value FROM duckdb_settings() WHERE name = 'pandas_analyze_sample'" | ||
| ).fetchone() | ||
| assert int(result[0]) == 50000 | ||
|
|
||
|
|
||
| def test_set_scan_all_frames(duckdb_connection): | ||
| _set_scan_all_frames(duckdb_connection, False) | ||
| result = duckdb_connection.execute( | ||
| "SELECT value FROM duckdb_settings() WHERE name = 'python_scan_all_frames'" | ||
| ).fetchone() | ||
| assert result[0] == "false" | ||
|
|
||
| _set_scan_all_frames(duckdb_connection, True) | ||
| result = duckdb_connection.execute( | ||
| "SELECT value FROM duckdb_settings() WHERE name = 'python_scan_all_frames'" | ||
| ).fetchone() | ||
| assert result[0] == "true" | ||
m1so marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @mock.patch("deepnote_toolkit.sql.duckdb_sql.import_extension") | ||
| def test_connection_returns_successfully_when_import_extension_fails( | ||
| mock_import_extension, | ||
| ): | ||
| mock_import_extension.side_effect = Exception("Failed to import extension") | ||
|
|
||
| with fresh_duckdb_connection() as conn: | ||
| assert conn is not None | ||
| result = conn.execute( | ||
| "SELECT extension_name, loaded FROM duckdb_extensions()" | ||
| ).df() | ||
| assert result is not None | ||
| # check that spatial and excel extensions are not loaded as import extension failed | ||
| result = result[result["extension_name"].isin(["spatial", "excel"])] | ||
| assert all(result["loaded"]) is False | ||
|
|
||
|
|
||
| @mock.patch("duckdb.DuckDBPyConnection.load_extension") | ||
| def test_connection_returns_successfully_when_load_extension_fails(mock_load_extension): | ||
| mock_load_extension.side_effect = Exception("Failed to load extension") | ||
|
|
||
| with fresh_duckdb_connection() as conn: | ||
| assert conn is not None | ||
| result = conn.execute( | ||
| "SELECT extension_name, loaded FROM duckdb_extensions()" | ||
| ).df() | ||
| assert result is not None | ||
| # check that spatial and excel extensions are not loaded as import extension failed | ||
| result = result[result["extension_name"].isin(["spatial", "excel"])] | ||
| assert all(result["loaded"]) is False | ||
|
|
||
|
|
||
| def test_excel_extension_roundtrip(duckdb_connection, tmp_path): | ||
| test_data = pd.DataFrame( | ||
| { | ||
| "id": [1, 2, 3], | ||
| "name": ["Alice", "Bob", "Charlie"], | ||
| "score": [95.5, 87.3, 91.2], | ||
| } | ||
| ) | ||
| duckdb_connection.register("test_table", test_data) | ||
| excel_path = tmp_path / "test_data.xlsx" | ||
| duckdb_connection.execute( | ||
| f"COPY test_table TO '{excel_path}' WITH (FORMAT xlsx, HEADER true)" | ||
| ) | ||
| duckdb_connection.unregister("test_table") | ||
|
|
||
| assert excel_path.exists(), "Excel file should be created" | ||
|
|
||
| # read with spatial extension | ||
| result = duckdb_connection.execute(f"SELECT * FROM st_read('{excel_path}')").df() | ||
| diff = test_data.compare(result) | ||
| assert diff.empty, "Data should be the same" | ||
|
|
||
| # read with excel extension | ||
| result = duckdb_connection.execute(f"SELECT * FROM read_xlsx('{excel_path}')").df() | ||
| diff = test_data.compare(result) | ||
| assert diff.empty, "Data should be the same" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.