JuliaDatabases / ODBC.jl

An ODBC interface for the Julia programming language
https://odbc.juliadatabases.org/stable
Other
106 stars 62 forks source link

MSSQL uniqueidentfier not preserved when read #366

Open deltadecay opened 1 year ago

deltadecay commented 1 year ago

There seems to be an issue reading MSSQL identifiers of type uniqueidentifier. See example below. To test you need to set up an ODBC connection to a MSSQL database. I've tested in Julia 1.8.5 and ODBC.jl 1.1.2.

using ODBC
using DataFrames

conn = ODBC.Connection("<your connection name>")

query = "SELECT CONVERT(uniqueidentifier, '99685768-257e-462e-a29f-e6902550f030') AS anid, '99685768-257e-462e-a29f-e6902550f030' AS strid"
cursor = DBInterface.execute(conn, query)
df = DataFrame(cursor)

DBInterface.close!(conn)

Reading the value from the dataframe we get following evaluated to true:

first(df.anid) == Base.UUID("30f05025-90e6-9fa2-462e-257e99685768")

The expected value should be as below evaluated to true:

first(df.anid) == Base.UUID("99685768-257e-462e-a29f-e6902550f030")
ahjulstad commented 11 months ago

I also experience this. The bug does not appear when testing the same ODBC driver from Python; this seems to be an issue in ODBC.jl, or possibly Base.UUID?

(Julia 1.9.3 and ODBC 1.1.2), ODBC Driver 18 for SQL Server.

ahjulstad commented 11 months ago

Looks very much like something related to byte order:


res = DBInterface.execute(mssql, 
"SELECT CONVERT(uniqueidentifier, 'ABCD0000-0000-0000-1234-000000000000') AS anid, 'ABCD0000-0000-0000-1234-000000000000' AS strid",
debug=true)

for r in Tables.rows(res)
    print(r)
end

# ODBC.Row:
#  :anid   UUID("00000000-0000-3412-0000-0000abcd0000")
#  :strid  "ABCD0000-0000-0000-1234-000000000000"

If someone wants to have a look, this repo reproduces it easily: https://github.com/ahjulstad/debug-julia-odbc