-
- Notifications
You must be signed in to change notification settings - Fork 19.3k
Fix BUG: read_sql tries to convert blob/varbinary to string with pyarrow backend #60105
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
Changes from all commits
2244869 bd00fc5 6a23f05 2f261c8 8f900b8 536b1ed 8965a1d a32b4a6 f412c72 a0200d0 10ca030 ba2e82d 8dd45ca 602194a f8ae285 02514e0 ef5c2ed da7817a de5c604 e025d84 3d83bab 6ea5785 88accb3 c066ebf 2afeed5 de286a4 a99365a a8f69e5 2f8ac63 1a0b2cf 1da6ee4 4c1c282 fe4bdba 71d94e5 8813f77 30ce3b3 9721fc3 bcb7e3d fe52a7a 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 |
|---|---|---|
| | @@ -4358,3 +4358,45 @@ def test_xsqlite_if_exists(sqlite_buildin): | |
| (5, "E"), | ||
| ] | ||
| drop_table(table_name, sqlite_buildin) | ||
| | ||
| | ||
| @pytest.mark.parametrize("con", all_connectable) | ||
| def test_bytes_column(con, dtype_backend, request): | ||
| # GitHub Issue #59242 | ||
| conn = request.getfixturevalue(con) | ||
| pa = pytest.importorskip("pyarrow") | ||
| | ||
| hex_str = "0123456789abcdef0123456789abcdef" | ||
| val = bytes.fromhex(hex_str) | ||
| if "postgres" in con: | ||
| if "adbc" in con: | ||
| val = b"\x00\x00\x00\x80\x01#Eg\x89\xab\xcd\xef\x01#Eg\x89\xab\xcd\xef" | ||
| Member 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. Why does postgres have different content than the other databases? Author 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 Member 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. Instead of selecting a literal value can you write and read back the dataframe? I think this PR is missing something about how the different drivers are going to return binary data. The ADBC driver has maps binary data to the BYTEA type (see also https://github.com/apache/arrow-adbc/blob/c6e529980f1369202adb101f2e1ea03c4e12dcdc/c/driver/postgresql/postgres_type.h#L599) so we should be round tripping that, not bits | ||
| else: | ||
| val = ( | ||
| Member 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. Is this supposed to be the binary representation of the bytes? I'm a bit confused why this test has so many different expected values Author 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. yeah exactly and this is due to postgres returning the | ||
| "0000000100100011010001010110011110001001101010" | ||
| "11110011011110111100000001001000110100010101100" | ||
| "11110001001101010111100110111101111" | ||
| ) | ||
| | ||
| if dtype_backend == "pyarrow": | ||
| dtype = pd.ArrowDtype(pa.binary()) | ||
| if "postgres" in con: | ||
| if "adbc" in con: | ||
| dtype = pd.ArrowDtype(pa.opaque(pa.binary(), "bit", "PostgreSQL")) | ||
| else: | ||
| dtype = pd.ArrowDtype(pa.string()) | ||
| else: | ||
| dtype = "O" | ||
| if "postgres" in con and "psycopg2" in con: | ||
| if dtype_backend == "numpy_nullable": | ||
| dtype = pd.StringDtype() | ||
| elif dtype_backend == lib.no_default and pd.options.future.infer_string: | ||
| dtype = pd.StringDtype(storage="pyarrow", na_value=np.nan) | ||
| | ||
| expected = DataFrame([{"a": val}], dtype=dtype) | ||
| df = pd.read_sql( | ||
| f"select x'{hex_str}' a", | ||
| conn, | ||
| dtype_backend=dtype_backend, | ||
| ) | ||
| tm.assert_frame_equal(df, expected) | ||
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.
Hmm AFAIK the OpaqueType is returned when there is no suitable database type, but isn't binary already returned directly? It is a bit unclear what this additional
isinstancecheck is forThere 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.
I had to add this because
adbc-driver-postgresqlis returning anOpaqueType- without this included an exception is thrown