Repository navigation
Keep Databricks integer columns exact when they contain NULLs - #12
Open
maharanay22 wants to merge 1 commit into
Open
maharanay22 wants to merge 1 commit into
maharanay22 wants to merge 1 commit into
Conversation
native_query built the result DataFrame straight from the cursor rows, so a NULL in a BIGINT or INT column made pandas store the column as float64. Values above 2**53 lost precision (9007199254740993 came back as 9007199254740992.0) and small integers turned into floats (3 -> 3.0). Rebuild integer and boolean columns that contain NULLs with the pandas nullable Int64 and boolean dtypes. Columns without NULLs keep their current dtypes.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
DatabricksHandler.native_query()builds the result withpd.DataFrame(result, columns=...)straight from the cursor rows. When aBIGINT,INT,SMALLINTorTINYINTcolumn contains a NULL, pandas stores the whole column asfloat64. That changes the data MindsDB returns:BIGINTID of9007199254740993comes back as9007199254740992.0, which is a different ID3comes back as3.0BOOLEANcolumns with a NULL have a similar problem and come back asobjectThis change builds the DataFrame in a small helper,
_make_data_frame(). After the normal construction, any column that pandas couldn't keep as int or bool is rebuilt from the raw values with the pandas nullableInt64orbooleandtype, but only when every non-NULL value in it is anint(or every one is abool). Columns without NULLs keep their current dtypes, and float, decimal, string and date columns are left alone. The PostgreSQL handler already returns nullableInt64/booleancolumns, so this matches what MindsDB gets from it. Columns are handled by position, so queries that return two columns with the same name still workFixes: no linked issue, because issues are turned off for this repository. The bug is described in full above
Type of change
Verification Process
To ensure the changes are working as expected:
tests/unit/handlers/community_handlers/test_databricks.py, new testTestTableOperations::test_native_query_keeps_integers_with_nulls_exactnative_query()with a mocked connection whose cursor returnsdatabricks.sql.types.Rowrows forid BIGINT,amount DECIMAL(10,2)andqty INT, where the second row is all NULLsBefore (current main):
After:
Unit tests. The new test fails on main and passes with the fix, and the existing Databricks tests still pass:
I didn't have a Databricks workspace to test against, so the check uses the connector's own
Rowtype with a mocked connection.ruff checkandruff format --check(0.11.11) pass on the changed files. No dependencies changed.databricks_handler.pyis stored with CRLF line endings in the repository, so the new lines use CRLF too and the diff only touches the lines that changedAdditional Media:
I didn't record a video or take screenshots. The before and after terminal output above shows the change instead
Checklist:
No documentation change is needed
I used an AI assistant to help investigate this and draft the change and tests, and I reviewed and tested it myself