Thread: $1 IS NULL with iso-8859-1 type

$1 IS NULL with iso-8859-1 type

From
Daniele Varrazzo
Date:
Hello,

The operator `IS NULL` doesn't work if the argument has unknown type.
In psycopg 3:

    >>> conn.execute("select %s is null", ['foo']).fetchone()
    IndeterminateDatatype: could not determine data type of parameter $1

This can get in the way of using the unknown type for strings (but
specifying the text oid for strings is worse, because there is no
implicit cast from string to most types).

It doesn't seem necessary to specify a type for an argument if it only
has to be compared with null: nullness is independent from the type
and is even specified, in the query parameters, in a separate array
from the parameter values.

Maybe this behaviour can be relaxed?

Cheers

-- Daniele



Re: $1 IS NULL with iso-8859-1 type

From
Tom Lane
Date:
Daniele Varrazzo <daniele.varrazzo@gmail.com> writes:
> The operator `IS NULL` doesn't work if the argument has unknown type.
> conn.execute("select %s is null", ['foo']).fetchone()
>     IndeterminateDatatype: could not determine data type of parameter $1

Yeah.

> It doesn't seem necessary to specify a type for an argument if it only
> has to be compared with null: nullness is independent from the type
> and is even specified, in the query parameters, in a separate array
> from the parameter values.

True, the IS NULL operator itself doesn't care about the data type,
but that doesn't mean that noplace else in the system does.

As an example, if we silently resolved the type as "text" as you seem
to wish, and then the client sends a non-null integer in binary format,
we'll likely end up throwing a bad-encoding error.

Maybe that's not a big problem, I dunno.  But we can't just not
resolve the type, we have to pick something.

            regards, tom lane