Skip to content

A null expression parameter raises TypeError when prepared and renders '' in getSqlString() #207

Open

Description

@simon-mundy

Package Version

0.6.x-dev @ e022af5c

Php Version

8.3, 8.4, 8.5

Database Engine

Any

Basic Information

AbstractSql::processExpression() renders a Sql\Argument\Value in one of two ways (AbstractSql.php#L234-L242):

  • With a parameter container, it calls processExpressionParameterName(), whose $value is int|float|string|bool (AbstractSql.php#L285). Value::getValue() can return null, which raises a TypeError.
  • Without one, it calls quoteValue((string) $argument->getValue()), which turns null into a quoted empty string.

So a = ? with null either fails to prepare or renders a = '', a different query. This affects every predicate holding a null Value, including expression(), equalTo() and the other comparison methods.

Column values already render null as SQL NULL: resolveColumnValue() returns 'NULL' for a null column, so Insert and Update produce VALUES (:id, NULL) in prepared mode too.

The '' expectation in PredicateTest dates from laminas-db (1c5ad3a, 2022), which recorded what quoting null produced at the time. That test never reaches its assertion on 0.6.x: its helper expects Sql92 to refuse the quote and throw.

Steps to Reproduce

  1. (new Select('t'))->where(new Predicate\Expression('COALESCE(?, 7) = 7', [null])), prepared and executed on SQLite.
  2. The same predicate rendered with getSqlString() on any driver platform.

Expected Behavior

NULL, i.e. COALESCE(NULL, 7), in both modes, consistent with resolveColumnValue().

Inlining NULL changes the SQL text depending on whether a value is null. The alternative is to bind null as a parameter, which keeps one statement shape for statement caching (see #112). That means widening processExpressionParameterName() to accept null, which breaks subclasses that override it with the current signature. No driver package overrides it.

Actual behavior?

Prepared:

TypeError: PhpDb\Sql\AbstractSql::processExpressionParameterName(): Argument #1 ($value) must be of type string|int|float|bool, null given

getSqlString():

SELECT "t".* FROM "t" WHERE COALESCE('', 7) = 7

Activity

  1. added
    bug
    on Oct 4, 2026
  2. simon-mundy commented on Nov 30, 2026

    @simon-mundy
    MemberAuthor

    Re-checked on current 0.6.x (7a142543), #259 and the render-time dialect branch in #261. All three behave the same.

    The prepared half is fixed: a null expression parameter now binds as null.

    The getSqlString() half remains: null still renders as '', from the (string) cast in AbstractSql::processExpression().

    It reaches further than the predicates listed above. The same cast turns null into '' for:

    • in('a', [1, null]) and where(['a' => [1, null]])
    • Argument::value(null) in a predicate
    • an Expression parameter in columns() or order()
    • an Expression value in Insert::values() or Update::set()
    • a DDL column default of Argument::value(null), which renders DEFAULT ''

    The same cast applied to false is filed separately as #262.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions