Skip to content

Commit da315e9

Browse files
Fix issue with non-nan values for null float columns (#72)
1 parent 4f45480 commit da315e9

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/npcontainer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fill_NAvalue(void *value, PyArray_Descr *dtype)
329329
{
330330
switch (dtype->type_num) {
331331
case NPY_BOOL:
332-
((npy_bool *)value)[0] = 0; // XXX False is a good default?
332+
((npy_bool *)value)[0] = 0; // Make this False to match `bool(None)` in Python
333333
break;
334334
case NPY_BYTE:
335335
((npy_byte *)value)[0] = NPY_MAX_BYTE;
@@ -368,10 +368,10 @@ fill_NAvalue(void *value, PyArray_Descr *dtype)
368368
((npy_float *)value)[0] = NPY_NANF;
369369
break;
370370
case NPY_DOUBLE:
371-
((npy_float *)value)[0] = NPY_NAN;
371+
((npy_double *)value)[0] = NPY_NAN;
372372
break;
373373
case NPY_LONGDOUBLE:
374-
((npy_float *)value)[0] = NPY_NANL;
374+
((npy_longdouble *)value)[0] = NPY_NANL;
375375
break;
376376
case NPY_CFLOAT:
377377
((npy_cfloat *)value)[0] = {NPY_NANF, NPY_NANF};

tests/test_numpy.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from typing import Optional
44

55
import hypothesis.strategies as st
6-
import npyodbc
76
import numpy as np
87
import pytest
98
from hypothesis import given, reject, settings
109
from numpy.testing import assert_allclose, assert_array_equal
1110

11+
import npyodbc
12+
1213

1314
@pytest.fixture(scope="module")
1415
def connection() -> npyodbc.Connection:
@@ -349,3 +350,27 @@ def test_fetchdictarray_coercion(cursor, int_dtype, float_dtype, values):
349350
)
350351

351352
cleanup(cursor)
353+
354+
355+
def test_null_floats(cursor):
356+
"""Test that writing null values into a float column are NaN when retrieved."""
357+
cursor.execute("CREATE TABLE t (f FLOAT NULL);")
358+
cursor.execute("INSERT INTO t VALUES (123.12345678), (NULL);")
359+
res = cursor.execute("SELECT * FROM t;").fetchdictarray(null_suffix="_isnull")
360+
361+
assert_array_equal(res['f'], [123.12345678, np.nan])
362+
assert_array_equal(res['f_isnull'], [False, True])
363+
364+
cleanup(cursor)
365+
366+
367+
def test_null_strings(cursor):
368+
"""Test that null values in a text column are empty strings when retrieved."""
369+
cursor.execute("CREATE TABLE t (f TEXT NULL);")
370+
cursor.execute("INSERT INTO t VALUES ('foo'), (NULL);")
371+
res = cursor.execute("SELECT * FROM t;").fetchdictarray(null_suffix="_isnull")
372+
373+
assert_array_equal(res['f'], ['foo', ''])
374+
assert_array_equal(res['f_isnull'], [False, True])
375+
376+
cleanup(cursor)

0 commit comments

Comments
 (0)