staabm / phpstan-dba

PHPStan based SQL static analysis and type inference for the database access layer
https://staabm.github.io/archive.html#phpstan-dba
MIT License
250 stars 17 forks source link

SQL AST: ifnull/nullif improvements #592

Closed hemberger closed 1 year ago

hemberger commented 1 year ago

Previously the IfNullReturnTypeExtension was used for both IFNULL and NULLIF. However, these SQL functions should have different behavior.

NULLIF(expr1,expr2)

Returns NULL if expr1 = expr2 is true, otherwise returns expr1.

IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

Note that IFNULL has additional type-mutating logic because it has to compare the two values in a temporary table:

The default return type of IFNULL(expr1,expr2) is the more "general" of the two expressions, in the order STRING, REAL, or INTEGER.

We now also require that the functions take exactly two arguments.

The typemix table is used for these tests since its column names clearly identify the column type (e.g. c_tinyint vs. gesperrt).

staabm commented 1 year ago

thanks for the in detail discussion and your analysis. I really appreciate it.