[hibernate-dev] CriteriaBuilder, aggregation and types

Guillaume Smet guillaume.smet at gmail.com
Thu Oct 11 10:01:29 EDT 2018


Hi,

We have an interesting test case here
https://hibernate.atlassian.net/browse/HHH-13001 about the use of
LiteralHandlingMode with the criteria builder. It turns out that the
problem is a bit more general than that.

Let's take this (not so useful) example:
query.multiselect(
                        document.get( "id" ),
                        cb.sum( cb.parameter( Long.class ) ) )
                .groupBy( document.get( "id" ) );

It will lead to a NPE when trying to determine the return type of the sum
in:
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/function/StandardAnsiSqlAggregationFunctions.java#L157

The fact is that we totally lose the type information along the way.

I don't know if it's something addressed in 6 and if we should try to fix
it in 5.4. In any case, I think having a workaround would be nice.

There are a few paths we could follow:
1/ at least throw a more meaningful error and provide a workaround. I
thought about forcing the use of the cast function but it doesn't work as
we have an optimization not adding the cast function is the type is
corresponding (see
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/query/criteria/internal/expression/ExpressionImpl.java#L35).
If we remove this one and provide a helpful error message, that could help.
Note that I don't know if it's just an optimization or something mandated
by the spec itself.

2/ we could try to add a cast automatically for these functions needing a
proper type only when strictly necessary e.g. when you have a parameter (or
a LiteralExpression transformed to a parameter). That would mean adding a
method ExpressionImplementor (and change all our code to use
ExpressionImplementor as it currently uses the JPA version mostly
everywhere). That would be more transparent for the user but clearly a
significant amount of (dumb) work/changes. And probably a nightmare to
merge in 6 if this area has changed.

Thoughts?

-- 
Guillaume


More information about the hibernate-dev mailing list