Skip to content

Commit b0da2d9

Browse files
committed
Fixed array_to_sparsevec on Windows [skip ci]
1 parent 3fb05eb commit b0da2d9

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/sparsevec.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,25 +707,32 @@ array_to_sparsevec(PG_FUNCTION_ARGS)
707707
CheckDim(nelemsp);
708708
CheckExpectedDim(typmod, nelemsp);
709709

710+
#ifdef _MSC_VER
711+
/* /fp:fast may not propagate +/-Infinity or NaN */
712+
#define IS_NOT_ZERO(v) (isnan((float) v) || isinf((float) v) || ((float) v) != 0)
713+
#else
714+
#define IS_NOT_ZERO(v) (((float) (v)) != 0)
715+
#endif
716+
710717
if (ARR_ELEMTYPE(array) == INT4OID)
711718
{
712719
for (int i = 0; i < nelemsp; i++)
713-
nnz += ((float) DatumGetInt32(elemsp[i])) != 0;
720+
nnz += IS_NOT_ZERO(DatumGetInt32(elemsp[i]));
714721
}
715722
else if (ARR_ELEMTYPE(array) == FLOAT8OID)
716723
{
717724
for (int i = 0; i < nelemsp; i++)
718-
nnz += ((float) DatumGetFloat8(elemsp[i])) != 0;
725+
nnz += IS_NOT_ZERO(DatumGetFloat8(elemsp[i]));
719726
}
720727
else if (ARR_ELEMTYPE(array) == FLOAT4OID)
721728
{
722729
for (int i = 0; i < nelemsp; i++)
723-
nnz += (DatumGetFloat4(elemsp[i]) != 0);
730+
nnz += IS_NOT_ZERO(DatumGetFloat4(elemsp[i]));
724731
}
725732
else if (ARR_ELEMTYPE(array) == NUMERICOID)
726733
{
727734
for (int i = 0; i < nelemsp; i++)
728-
nnz += (DatumGetFloat4(DirectFunctionCall1(numeric_float4, elemsp[i])) != 0);
735+
nnz += IS_NOT_ZERO(DirectFunctionCall1(numeric_float4, elemsp[i]));
729736
}
730737
else
731738
{
@@ -740,7 +747,7 @@ array_to_sparsevec(PG_FUNCTION_ARGS)
740747
#define PROCESS_ARRAY_ELEM(elem) \
741748
do { \
742749
float v = (float) (elem); \
743-
if (v != 0) { \
750+
if (IS_NOT_ZERO(v)) { \
744751
/* Safety check */ \
745752
if (j >= result->nnz) \
746753
elog(ERROR, "safety check failed"); \
@@ -778,13 +785,17 @@ array_to_sparsevec(PG_FUNCTION_ARGS)
778785
}
779786

780787
#undef PROCESS_ARRAY_ELEM
788+
#undef IS_NOT_ZERO
781789

782790
/*
783791
* Free allocation from deconstruct_array. Do not free individual elements
784792
* when pass-by-reference since they point to original array.
785793
*/
786794
pfree(elemsp);
787795

796+
if (j != result->nnz)
797+
elog(ERROR, "correctness check failed");
798+
788799
/* Check elements */
789800
for (int i = 0; i < result->nnz; i++)
790801
CheckElement(values[i]);

0 commit comments

Comments
 (0)