title: Postgres bytea error when binding null to prepared statements
body: <p>I am working with a Java application which uses JPA and a Postgres database, and I am trying to create a flexible prepared statement which can handle a variable number of input parameters.  An example query would best explain this:</p>
  
  <pre><code>SELECT *
  FROM my_table
  WHERE
      (string_col = :param1 OR :param1 IS NULL) AND
      (double_col = :param2 OR :param2 IS NULL);
  </code></pre>
  
  <p>The idea behind this "trick" is that if a user specifies only one parameter, say <code>:param1</code>, we can just bind <code>null</code> to <code>:param2</code>, and the <code>WHERE</code> clause would then behave as if only the first parameter were even being checked.  This approach lets us handle, in theory, any number of input parameters using a single prepared statement, instead of needing to maintain many different statements.</p>
  
  <p>I have gotten a simple POC working locally using pure JDBC prepared statements.  However, doing so required casting the parameter before comparing it to <code>NULL</code>, e.g.</p>
  
  <pre><code>WHERE (double_col = ? OR ?::numeric IS NULL)
                           ^^ does not work without cast
  </code></pre>
  
  <p>However, my actual application is using JPA, and I keep getting the following persistent error:</p>
  
  <pre><code>Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: double precision = bytea
    Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  </code></pre>
  
  <p>The problem does <em>not</em> occur with string/text columns, but only with columns which are <code>double precision</code> in my Postgres table.  I have tried all combinations of casting, and nothing works:</p>
  
  <pre><code>(double_col = :param2 OR CAST(:param2 AS double precision) IS NULL);
  (CAST(double_col AS double precision) = :param2 OR :param2 IS NULL);
  (CAST(double_col AS double precision) = :param2 OR CAST(:param2 AS double precision) IS NULL);
  </code></pre>
  
  <p>The error seems to be saying that JDBC is sending Postgres a <code>bytea</code> type for the double columns, and then Postgres is rolling over because it can't find a way to cast <code>byte</code> to double precision.</p>
  
  <p>The Java code looks something like:</p>
  
  <pre><code>Query query = entityManager.createNativeQuery(sqlString, MyEntity.class);
  query.setParameter("param1", "some value");
  // bind other parameters here
  List&lt;MyEntity&gt; = query.getResultList();
  </code></pre>
  
  <p>For reference, here are the versions of everything I am using:</p>
  
  <pre><code>Hibernate version         | 4.3.7.Final
  Spring data JPA vesion    | 1.7.1.RELEASE
  Postgres driver version   | 42.2.2
  Postgres database version | 9.6.10
  Java version              | 1.8.0_171
  </code></pre>