-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Failure to infer correct type of arithmetic expression
PostPosted: Thu Sep 08, 2016 11:34 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
Hello!

I'm having a very specific issue with Hibernate (5.2.1.Final), I'm starting to think it's a bug, but I don't want to rush the report. (Also the tracker site is kinda unfriendly, so I'm not sure it wouldn't be a duplicate.) So, to get to the point, here's the test code that throws an exception, while I don't think it should:

Code:
   
    @Test
    public void paramAdditionTest() throws Exception {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();

        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        // Entity can be anything, doesn't matter here
        CriteriaQuery<Stuff> criteriaQuery = criteriaBuilder.createQuery(Stuff.class);
        Root<Stuff> root = criteriaQuery.from(Stuff.class);

        ParameterExpression<Integer> p1 = criteriaBuilder.parameter(Integer.class);
        ParameterExpression<Integer> p2 = criteriaBuilder.parameter(Integer.class);
        ParameterExpression<Integer> p3 = criteriaBuilder.parameter(Integer.class);

        Predicate predicate = criteriaBuilder.equal(criteriaBuilder.sum(p1, p2), p3);

        criteriaQuery.select(root).where(predicate);

        TypedQuery<Stuff> query = entityManager.createQuery(criteriaQuery);

        query.setParameter(p1, 1);
        query.setParameter(p2, 2);
        query.setParameter(p3, 3);

        query.getResultList();

        entityManager.getTransaction().commit();
        entityManager.close();
    }


This should run a query along these lines:
Code:
SELECT * FROM Stuff WHERE 1+2=3;


Simple, yet, it throws an exception:
Code:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double


As a part of a more complex application, running in WildFly:
Code:
java.lang.IllegalArgumentException: Parameter value [3] did not match expected type [java.lang.Double (n/a)]


Same thing, basically. I did some digging, and I think the issue is that "somewhere" Hibernate fails to "port" the types of the parameters (while it's clearly possible with the getJavaType() method), so BinaryArithmeticOperatorNode fails to infer the result type (somewhere here: https://github.com/hibernate/hibernate-orm/blob/ec4f20a5fba79444dbd704f1305f961c339dd7d1/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/BinaryArithmeticOperatorNode.java#L106).

Also, if any of the parameters comes from a "different source", that is on of the fields of the entity, like the id, the type is inferred just fine and the query executes. So I believe this is an issue with parameter handling?

Is this really is a bug, or am I doing something wrong?


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 1:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
First of all, why would you want to run the 1+2=3 expression?

Usually you want to have at least one table column being involved in that, and the type is going to be guessed from that column.


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 2:16 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
I'm writing a generic REST API that translates (ODATA-like) filter query strings to criteria queries with ANTLR. A programmatic client might end up with such a request for whatever reasons.

This is special case of a rule like "expr: expr + expr ;" where both the expressions are primitives. This is a legal SQL and while does not make much sense, I don't really want to detect all the cases that can be evaluated from Java. Hibernate doesn't do so either, an empty conjunction() is (1=1) which is trivially true, yet it adds it to the query.


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 2:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can open a JIRA issue, but I'm not sure if we'd be able to infer the type. We'll see.


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 2:29 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
Okay, thank you for your quick replies! I'll post the ticket later today.


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 7:45 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
I'll postpone the report for a little as this seems to be part of a more broad issue with type inference, found some other inconsistencies, like:

int_param < long_param --> works
int_field < long_field --> works
int_param < long_field --> exception


Top
 Profile  
 
 Post subject: Re: Failure to infer correct type of arithmetic expression
PostPosted: Fri Sep 09, 2016 10:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Thanks for investigating it. You can add all these use cases in the JIRA issue. A replicating test case is highly appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.