Skip to content

dbsql_parameter_from_primitive maps Python float to FloatParameter (32-bit) instead of DoubleParameter (64-bit), causing silent precision loss #741

@Shubhambhusate

Description

@Shubhambhusate

Description:

Python's native float type is a 64-bit IEEE 754 double-precision number. However, the dbsql_parameter_from_primitive() function in src/databricks/sql/parameters/native.py (line 662) maps it to FloatParameter, which casts the value to a Databricks SQL FLOAT (32-bit single-precision). This causes silent precision loss when using parameterized queries with float values.

The correct mapping should be DoubleParameter, which corresponds to Databricks SQL DOUBLE (64-bit) and matches Python's native float precision.

Steps to reproduce:

from databricks import sql
from databricks.sql.parameters import DoubleParameter

test_val = 12345.678901234

with connection.cursor() as cursor:
    cursor.execute("DROP TABLE IF EXISTS test_precision")
    cursor.execute("CREATE TABLE test_precision (val DOUBLE)")

    # BUG: raw float silently loses precision (uses FloatParameter → 32-bit)
    cursor.execute("INSERT INTO test_precision (val) VALUES (?)", [test_val])
    cursor.execute("SELECT val FROM test_precision")
    stored_bug = cursor.fetchone()[0]

    print(f"Input:  {test_val}")       # 12345.678901234
    print(f"Stored: {stored_bug}")     # 12345.6787109375 (precision lost!)
    print(f"Match:  {test_val == stored_bug}")  # False

Expected Behavior

Passing a Python float to a parameterized query should preserve 64-bit precision by default, since Python's float is inherently 64-bit.

Actual Behavior

Precision is silently truncated to 32-bit because dbsql_parameter_from_primitive() returns FloatParameter instead of DoubleParameter.

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