-
Notifications
You must be signed in to change notification settings - Fork 50
feat: Add experimental polars execution #1747
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
Open
TrevorBergeron
wants to merge
6
commits into
main
Choose a base branch
from
polars_semi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5449696
feat: Add experimental polars execution
TrevorBergeron 98b300c
Merge remote-tracking branch 'github/main' into polars_semi
TrevorBergeron 5d9e208
Merge remote-tracking branch 'github/main' into polars_semi
TrevorBergeron 6a2ae4c
fix test and import
TrevorBergeron 404f447
error for old polars version
TrevorBergeron 683f349
Merge remote-tracking branch 'github/main' into polars_semi
TrevorBergeron 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 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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import google.auth.credentials | ||
import requests.adapters | ||
|
||
import bigframes._importing | ||
import bigframes.enums | ||
import bigframes.exceptions as bfe | ||
|
||
|
@@ -94,6 +95,7 @@ def __init__( | |
requests_transport_adapters: Sequence[ | ||
Tuple[str, requests.adapters.BaseAdapter] | ||
] = (), | ||
enable_polars_execution: bool = False, | ||
): | ||
self._credentials = credentials | ||
self._project = project | ||
|
@@ -113,6 +115,9 @@ def __init__( | |
client_endpoints_override = {} | ||
|
||
self._client_endpoints_override = client_endpoints_override | ||
if enable_polars_execution: | ||
bigframes._importing.import_polars() | ||
self._enable_polars_execution = enable_polars_execution | ||
|
||
@property | ||
def application_name(self) -> Optional[str]: | ||
|
@@ -424,3 +429,18 @@ def requests_transport_adapters( | |
SESSION_STARTED_MESSAGE.format(attribute="requests_transport_adapters") | ||
) | ||
self._requests_transport_adapters = value | ||
|
||
@property | ||
def enable_polars_execution(self) -> bool: | ||
"""If True, will use polars to execute some simple query plans locally.""" | ||
return self._enable_polars_execution | ||
|
||
@enable_polars_execution.setter | ||
def enable_polars_execution(self, value: bool): | ||
if value is True: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setter might also be an opportune time to check that polars is installed and a compatible version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a util to check version |
||
msg = bfe.format_message( | ||
"Polars execution is an experimental feature, and may not be stable. Must have polars installed." | ||
) | ||
warnings.warn(msg, category=bfe.PreviewWarning) | ||
bigframes._importing.import_polars() | ||
self._enable_polars_execution = value |
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,30 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed 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 importlib | ||
from types import ModuleType | ||
|
||
from packaging import version | ||
|
||
# Keep this in sync with setup.py | ||
POLARS_MIN_VERSION = version.Version("1.7.0") | ||
|
||
|
||
def import_polars() -> ModuleType: | ||
polars_module = importlib.import_module("polars") | ||
imported_version = version.Version(polars_module.build_info()["version"]) | ||
if imported_version < POLARS_MIN_VERSION: | ||
raise ImportError( | ||
f"Imported polars version: {imported_version} is below the minimum version: {POLARS_MIN_VERSION}" | ||
) | ||
return polars_module |
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
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
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
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ matplotlib==3.7.1 | |
psutil==5.9.5 | ||
seaborn==0.13.1 | ||
traitlets==5.7.1 | ||
polars==1.7.0 |
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,76 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed 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 pytest | ||
|
||
import bigframes | ||
from bigframes.testing.utils import assert_pandas_df_equal | ||
|
||
polars = pytest.importorskip("polars", reason="polars is required for this test") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def session_w_polars(): | ||
context = bigframes.BigQueryOptions(location="US", enable_polars_execution=True) | ||
session = bigframes.Session(context=context) | ||
yield session | ||
session.close() # close generated session at cleanup time | ||
|
||
|
||
def test_polar_execution_sorted(session_w_polars, scalars_pandas_df_index): | ||
execution_count_before = session_w_polars._metrics.execution_count | ||
bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) | ||
|
||
pd_result = scalars_pandas_df_index.sort_index(ascending=False)[ | ||
["int64_too", "bool_col"] | ||
] | ||
bf_result = bf_df.sort_index(ascending=False)[["int64_too", "bool_col"]].to_pandas() | ||
|
||
assert session_w_polars._metrics.execution_count == execution_count_before | ||
assert_pandas_df_equal(bf_result, pd_result) | ||
|
||
|
||
def test_polar_execution_sorted_filtered(session_w_polars, scalars_pandas_df_index): | ||
execution_count_before = session_w_polars._metrics.execution_count | ||
bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) | ||
|
||
pd_result = scalars_pandas_df_index.sort_index(ascending=False).dropna( | ||
subset=["int64_col", "string_col"] | ||
) | ||
bf_result = ( | ||
bf_df.sort_index(ascending=False) | ||
.dropna(subset=["int64_col", "string_col"]) | ||
.to_pandas() | ||
) | ||
|
||
# Filter and isnull not supported by polar engine yet, so falls back to bq execution | ||
assert session_w_polars._metrics.execution_count == (execution_count_before + 1) | ||
assert_pandas_df_equal(bf_result, pd_result) | ||
|
||
|
||
def test_polar_execution_unsupported_sql_fallback( | ||
session_w_polars, scalars_pandas_df_index | ||
): | ||
execution_count_before = session_w_polars._metrics.execution_count | ||
bf_df = session_w_polars.read_pandas(scalars_pandas_df_index) | ||
|
||
pd_df = scalars_pandas_df_index.copy() | ||
pd_df["str_len_col"] = pd_df.string_col.str.len() | ||
pd_result = pd_df | ||
|
||
bf_df["str_len_col"] = bf_df.string_col.str.len() | ||
bf_result = bf_df.to_pandas() | ||
|
||
# str len not supported by polar engine yet, so falls back to bq execution | ||
assert session_w_polars._metrics.execution_count == (execution_count_before + 1) | ||
assert_pandas_df_equal(bf_result, pd_result) |
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.
Should this be a compute option so it can be changed at runtime?
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.
Kind of just want to keep it as a constant within-session for now (will need to commit if making this a GA feature though). Turning polars execution on and off mid-session will make things like caching, multi-part execution really tricky