-
Notifications
You must be signed in to change notification settings - Fork 571
UN-2882 [FIX] Fix BigQuery float precision issue in PARSE_JSON for metadata serialization #1593
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76e2365
UN-2882 [FIX] Fix BigQuery float precision issue in PARSE_JSON for me…
muhammad-ali-e 2bf8057
Merge branch 'main' into fix/UN-2882-bigquery-float-precision
muhammad-ali-e 4b1dceb
Fix truthiness check for empty values in JSON serialization
muhammad-ali-e 58ce1a6
Merge branch 'fix/UN-2882-bigquery-float-precision' of github.com:Zip…
muhammad-ali-e 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
51 changes: 51 additions & 0 deletions
51
unstract/connectors/src/unstract/connectors/databases/utils.py
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,51 @@ | ||
| """Database Utilities | ||
|
|
||
| Common utilities for database connectors to ensure consistent data handling | ||
| across all database types (BigQuery, PostgreSQL, MySQL, Snowflake, etc.). | ||
| """ | ||
|
|
||
| import math | ||
| from typing import Any | ||
|
|
||
|
|
||
| def sanitize_floats_for_database(data: Any) -> Any: | ||
| """Sanitize special float values (NaN, Inf) for database compatibility. | ||
|
|
||
| This minimal sanitization applies to all databases. It only handles | ||
| special float values that no database can store in JSON: | ||
| - NaN (Not a Number) → None | ||
| - Infinity → None | ||
| - -Infinity → None | ||
|
|
||
| Database-specific precision handling (like BigQuery's round-trip requirements) | ||
| should be implemented in the respective database connector. | ||
|
|
||
| Args: | ||
| data: The data structure to sanitize (dict, list, or primitive) | ||
|
|
||
| Returns: | ||
| Sanitized data with NaN/Inf converted to None | ||
|
|
||
| Example: | ||
| >>> sanitize_floats_for_database({"value": float("nan")}) | ||
| {'value': None} | ||
|
|
||
| >>> sanitize_floats_for_database({"value": float("inf")}) | ||
| {'value': None} | ||
|
|
||
| >>> sanitize_floats_for_database({"price": 1760509016.282637}) | ||
| {'price': 1760509016.282637} # Unchanged - precision preserved | ||
| """ | ||
| if isinstance(data, float): | ||
| # Only handle special values that no database supports | ||
| if math.isnan(data) or math.isinf(data): | ||
| return None | ||
| # Return unchanged - let database connector handle precision if needed | ||
| return data | ||
| elif isinstance(data, dict): | ||
| return {k: sanitize_floats_for_database(v) for k, v in data.items()} | ||
| elif isinstance(data, list): | ||
| return [sanitize_floats_for_database(item) for item in data] | ||
| else: | ||
| # Return other types unchanged (int, str, bool, None, etc.) | ||
| return data |
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
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.