JuliaDatabases/ODBC.jl

Decimal numbers are not fetched correctly because the receiving buffer is too small

xitology opened this issue · 0 comments

When I use ODBC.jl to execute the query like SELECT CAST(-1.5 AS DECIMAL(2,1)), I get back the value -1.0 instead of the expected -1.5. Depending on the database server, a query as simple as SELECT -1.5 can demonstrate the issue. The problem is that the receiving buffer allocated by ODBC.jl is too small to hold the complete value.

Here's the code to quickly reproduce the issue:

using ODBC
using DataFrames

const dsn = ENV["DSN"]
const sql = "SELECT CAST(-1.5 AS DECIMAL(2,1))"
#const sql = "SELECT -1.5"
const conn = ODBC.Connection(dsn)
const cr = DBInterface.execute(conn, sql)
display(cr.metadata)
display(DataFrame(cr))

The output, for MS SQL Server, is:

2×7 Matrix{Any}:
 "column name"   "column type"          "sql type"     "c type"       "sizes"      "nullable"       "long data"
 Symbol("-1.5")  Union{Missing, Dec64}  "SQL_DECIMAL"  "SQL_C_CHAR"  3         true            false
1×1 DataFrame
 Row │ -1.5  
     │ Dec64 
─────┼───────
   1 │  -1.0

Notably, the column size reported by the ODBC driver is 3-1=2, apparently, the precision of the decimal number and not the size of the receiving buffer. I also ran this code and got the same result against three other drivers: for PostgreSQL, MariaDB, and Databricks.