Skip to content

Fix the incorrect data type of float values in pdo_pgsql query results #12476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
#define TIMESTAMPOID 1114
#define VARCHARLABEL "varchar"
#define VARCHAROID 1043
#define FLOAT4LABEL "float4"
#define FLOAT4OID 700
#define FLOAT8LABEL "float8"
#define FLOAT8OID 701



Expand Down Expand Up @@ -513,6 +517,18 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pd
#endif
ZVAL_LONG(result, ZEND_ATOL(ptr));
break;
case FLOAT4OID:
case FLOAT8OID:
if (strncmp(ptr, "Infinity", len) == 0) {
ZVAL_DOUBLE(result, ZEND_INFINITY);
} else if (strncmp(ptr, "-Infinity", len) == 0) {
ZVAL_DOUBLE(result, -ZEND_INFINITY);
} else if (strncmp(ptr, "NaN", len) == 0) {
ZVAL_DOUBLE(result, ZEND_NAN);
} else {
ZVAL_DOUBLE(result, zend_strtod(ptr, NULL));
}
break;

case OIDOID: {
char *end_ptr;
Expand Down Expand Up @@ -632,6 +648,12 @@ static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *r
case INT4OID:
add_assoc_string(return_value, "native_type", INT4LABEL);
break;
case FLOAT4OID:
add_assoc_string(return_value, "native_type", FLOAT4LABEL);
break;
case FLOAT8OID:
add_assoc_string(return_value, "native_type", FLOAT8LABEL);
break;
case TEXTOID:
add_assoc_string(return_value, "native_type", TEXTLABEL);
break;
Expand Down
130 changes: 130 additions & 0 deletions ext/pdo_pgsql/tests/float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
--TEST--
PDO PgSQL float value
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);

$stmt = $db->query(<<<'SQL'
select cast(0.4 as float4) as value4,
cast(0.8 as float8) as value8,
cast('NaN' as float4) as valuenan,
cast('Infinity' as float4) as valueinfinity,
cast('-Infinity' as float4) as valueninfinity
SQL);

var_dump($stmt->fetchAll());
var_dump($stmt->getColumnMeta(0));
var_dump($stmt->getColumnMeta(1));
var_dump($stmt->getColumnMeta(2));
var_dump($stmt->getColumnMeta(3));
var_dump($stmt->getColumnMeta(4));
?>
--EXPECT--
array(1) {
[0]=>
array(5) {
["value4"]=>
float(0.4)
["value8"]=>
float(0.8)
["valuenan"]=>
float(NAN)
["valueinfinity"]=>
float(INF)
["valueninfinity"]=>
float(-INF)
}
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(6) "value4"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(701)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float8"
["pdo_type"]=>
int(2)
["name"]=>
string(6) "value8"
["len"]=>
int(8)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(8) "valuenan"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(13) "valueinfinity"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(14) "valueninfinity"
["len"]=>
int(4)
["precision"]=>
int(-1)
}