Skip to content

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

@xitology

Description

@xitology

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions