Thread: BUG #17427: ERROR: cannot cast type bytea to bigint

BUG #17427: ERROR: cannot cast type bytea to bigint

From
PG Bug reporting form
Date:
The following bug has been logged on the website:

Bug reference:      17427
Logged by:          Ahmed Tareque
Email address:      tareque.bracu@gmail.com
PostgreSQL version: 14.0
Operating system:   Windows
Description:

Error : 
SQL Error: 0, SQLState: 42846.
ERROR: cannot cast type bytea to bigint
could not extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

My Query :
    @Query(value = "SELECT tam.name, tm.* FROM tam_main AS tm " +
            "LEFT JOIN tam_occupation AS tam ON tam.id =
tm.tam_occupation_id " +
            "LEFT JOIN tam_type AS tap ON tap.id = tm.tam_type_id " +
            "WHERE (:occupationName is null or (LOWER(tam.name) LIKE '%' ||
cast(:occupationName AS text) || '%' ) )" +
            "AND (:tamTypeId is null or tap.id = cast(:tamTypeId AS
bigint))", nativeQuery = true)
    Optional<List<TamMain>>
getTamMainByOccupationName(@Param("occupationName") String occupationName,
@Param("tamTypeId") Long tamTypeId);

My JPQL (same as query):
@Query("Select t from TamMain t  WHERE (:occupationName is null or
(lower(t.tamOccupation.name) like ('%' || lower(:occupationName) || '%')))
AND (:tamTypeId is null or t.tamType.id = :tamTypeId) ")

My Opinion:
Hello ! My ass is burning with such spicy error. It's taking 2days from my
precious life to solve this issue. Tried in so many ways to solve but
couldn't do that. Oh ! Experienced some paranormal activity likewise, "Woow
! It's working now". [A few hours later] "Shit, Its not working !" Although
I didn't change a bit of that code. Now, would you please provide me a
solution of this? 
If I provide occupationName in requestParam it gives me error. 
If I give null of both of them it gives me same error.
If I give value for tamTypeId then only I get the data.
Have a wonderful day !


Re: BUG #17427: ERROR: cannot cast type bytea to bigint

From
"David G. Johnston"
Date:
On Sun, Mar 6, 2022 at 4:14 PM PG Bug reporting form <noreply@postgresql.org> wrote:
The following bug has been logged on the website:

Bug reference:      17427
Logged by:          Ahmed Tareque
Email address:      tareque.bracu@gmail.com
PostgreSQL version: 14.0
Operating system:   Windows
Description:       

Error :
SQL Error: 0, SQLState: 42846.
ERROR: cannot cast type bytea to bigint
could not extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Due to the presence of the Java stuff this doesn't qualify as a PostgreSQL bug report and thus this is the wrong place to post.

It is also unlikely to be an actual bug in any vendor software, rather it is likely that you just are not using the API correctly.  For questions of that nature an email to either the PostgreSQL -general list or a Java-oriented community is more useful, the latter being much more likely to find an audience that can assist you.

"AND (:tamTypeId is null or tap.id = cast(:tamTypeId AS
bigint))

The only way you get the aforementioned error is if PostgreSQL, when it executes this query, thinks that the value in the cast is of type bytea; and it will not matter whether the value is null or not.  But the logic as to how "cast(:tamTypeId as bigint)" is transformed into SQL that is sent to the server is completely outside the purview of PostgreSQL and as such, as noted above, outside the purview of this mailing list.

David J.