Skip to content

Commit 0c0d1ac

Browse files
committed
Relax stringification of constant floats to numeric-string
1 parent ed7db7d commit 0c0d1ac

File tree

2 files changed

+77
-38
lines changed

2 files changed

+77
-38
lines changed

src/Type/Doctrine/Query/QueryResultTypeWalker.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ public function walkSelectExpression($selectExpression): string
11391139
return $traverse($type);
11401140
}
11411141

1142-
if ($type instanceof IntegerType || $type instanceof FloatType) {
1142+
if ($type instanceof IntegerType) {
11431143
$stringify = $this->shouldStringifyExpressions($type);
11441144

11451145
if ($stringify->yes()) {
@@ -1150,6 +1150,23 @@ public function walkSelectExpression($selectExpression): string
11501150

11511151
return $type;
11521152
}
1153+
1154+
if ($type instanceof FloatType) {
1155+
$stringify = $this->shouldStringifyExpressions($type);
1156+
1157+
// e.g. 1.0 on sqlite results to '1' with pdo_stringify on PHP 8.1, but '1.0' on PHP 8.0 with no setup
1158+
// so we relax constant types and return just numeric-string to avoid those issues
1159+
$stringifiedFloat = $this->createNumericString(false);
1160+
1161+
if ($stringify->yes()) {
1162+
return $stringifiedFloat;
1163+
} elseif ($stringify->maybe()) {
1164+
return TypeCombinator::union($stringifiedFloat, $type);
1165+
}
1166+
1167+
return $type;
1168+
}
1169+
11531170
if ($type instanceof BooleanType) {
11541171
$stringify = $this->shouldStringifyExpressions($type);
11551172

tests/Platform/QueryResultTypeWalkerFetchTypeMatrixTest.php

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ public static function provideCases(): iterable
195195
},
196196
];
197197

198+
yield ' 1.00' => [
199+
'data' => self::dataDefault(),
200+
'select' => 'SELECT 1.00 FROM %s t',
201+
'mysql' => new ConstantStringType('1.00'),
202+
'sqlite' => new ConstantFloatType(1.0),
203+
'pdo_pgsql' => new ConstantStringType('1.00'),
204+
'pgsql' => new ConstantStringType('1.00'),
205+
'mssql' => new MixedType(),
206+
'mysqlResult' => '1.00',
207+
'sqliteResult' => 1.0,
208+
'pdoPgsqlResult' => '1.00',
209+
'pgsqlResult' => '1.00',
210+
'mssqlResult' => '1.00',
211+
'shouldStringify' => static function (string $driver, int $php, string $configName): bool {
212+
return self::defaultStringification($driver, $php, $configName);
213+
},
214+
];
215+
198216
yield ' 0.1' => [
199217
'data' => self::dataDefault(),
200218
'select' => 'SELECT 0.1 FROM %s t',
@@ -213,6 +231,42 @@ public static function provideCases(): iterable
213231
},
214232
];
215233

234+
yield ' 0.10' => [
235+
'data' => self::dataDefault(),
236+
'select' => 'SELECT 0.10 FROM %s t',
237+
'mysql' => new ConstantStringType('0.10'),
238+
'sqlite' => new ConstantFloatType(0.1),
239+
'pdo_pgsql' => new ConstantStringType('0.10'),
240+
'pgsql' => new ConstantStringType('0.10'),
241+
'mssql' => new MixedType(),
242+
'mysqlResult' => '0.10',
243+
'sqliteResult' => 0.1,
244+
'pdoPgsqlResult' => '0.10',
245+
'pgsqlResult' => '0.10',
246+
'mssqlResult' => '.10',
247+
'shouldStringify' => static function (string $driver, int $php, string $configName): bool {
248+
return self::defaultStringification($driver, $php, $configName);
249+
},
250+
];
251+
252+
yield '0.125e0' => [
253+
'data' => self::dataDefault(),
254+
'select' => 'SELECT 0.125e0 FROM %s t',
255+
'mysql' => new ConstantFloatType(0.125),
256+
'sqlite' => new ConstantFloatType(0.125),
257+
'pdo_pgsql' => new ConstantStringType('0.125'),
258+
'pgsql' => new ConstantStringType('0.125'),
259+
'mssql' => new MixedType(),
260+
'mysqlResult' => 0.125,
261+
'sqliteResult' => 0.125,
262+
'pdoPgsqlResult' => '0.125',
263+
'pgsqlResult' => '0.125',
264+
'mssqlResult' => 0.125,
265+
'shouldStringify' => static function (string $driver, int $php, string $configName): bool {
266+
return self::defaultStringification($driver, $php, $configName);
267+
},
268+
];
269+
216270
yield ' 1e0' => [
217271
'data' => self::dataDefault(),
218272
'select' => 'SELECT 1e0 FROM %s t',
@@ -1221,42 +1275,6 @@ public static function provideCases(): iterable
12211275
},
12221276
];
12231277

1224-
yield '0.1' => [
1225-
'data' => self::dataDefault(),
1226-
'select' => 'SELECT 0.1 FROM %s t',
1227-
'mysql' => new ConstantStringType('0.1'),
1228-
'sqlite' => new ConstantFloatType(0.1),
1229-
'pdo_pgsql' => new ConstantStringType('0.1'),
1230-
'pgsql' => new ConstantStringType('0.1'),
1231-
'mssql' => new MixedType(),
1232-
'mysqlResult' => '0.1',
1233-
'sqliteResult' => 0.1,
1234-
'pdoPgsqlResult' => '0.1',
1235-
'pgsqlResult' => '0.1',
1236-
'mssqlResult' => '.1',
1237-
'shouldStringify' => static function (string $driver, int $php, string $configName): bool {
1238-
return self::defaultStringification($driver, $php, $configName);
1239-
},
1240-
];
1241-
1242-
yield '0.125e0' => [
1243-
'data' => self::dataDefault(),
1244-
'select' => 'SELECT 0.125e0 FROM %s t',
1245-
'mysql' => new ConstantFloatType(0.125),
1246-
'sqlite' => new ConstantFloatType(0.125),
1247-
'pdo_pgsql' => new ConstantStringType('0.125'),
1248-
'pgsql' => new ConstantStringType('0.125'),
1249-
'mssql' => new MixedType(),
1250-
'mysqlResult' => 0.125,
1251-
'sqliteResult' => 0.125,
1252-
'pdoPgsqlResult' => '0.125',
1253-
'pgsqlResult' => '0.125',
1254-
'mssqlResult' => 0.125,
1255-
'shouldStringify' => static function (string $driver, int $php, string $configName): bool {
1256-
return self::defaultStringification($driver, $php, $configName);
1257-
},
1258-
];
1259-
12601278
yield "''" => [
12611279
'data' => self::dataDefault(),
12621280
'select' => 'SELECT \'\' FROM %s t',
@@ -3978,10 +3996,14 @@ private static function stringifyType(Type $type): Type
39783996
return $traverse($type);
39793997
}
39803998

3981-
if ($type instanceof IntegerType || $type instanceof FloatType) {
3999+
if ($type instanceof IntegerType) {
39824000
return $type->toString();
39834001
}
39844002

4003+
if ($type instanceof FloatType) {
4004+
return self::numericString();
4005+
}
4006+
39854007
if ($type instanceof BooleanType) {
39864008
return $type->toInteger()->toString();
39874009
}

0 commit comments

Comments
 (0)