nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NH-2431 - Modulo Operator [%] Not Supported by HQL Grammar

Open nhibernate-bot opened this issue 8 years ago • 0 comments

Dana Naideth created an issue — :

Prior to Antlr being used to parse HQL, (big improvement btw), it was possible to use the modulo in HQL as follows:

from Shape where Width % 5 = 0

I recently upgraded one of my projects to 2.1.2.GA, and am getting a parse exception when I use the same syntax. I realize that there is a mod() function, but that is also not working for my case due to a related issue (https://nhibernate.jira.com/browse/NH-2031).

When looking at the HQL.g file, it seems like the intent was to support this operator:

// Operator precedence in HQL // lowest --> ( 8) OR // ( 7) AND, NOT // LIKE, NOT LIKE, BETWEEN, NOT BETWEEN, IN, NOT IN // ( 4) bitwise: |, &
// ( 3) addition and subtraction: (binary) -(binary) // ( 2) multiplication: * / %, concatenate: | | // highest --> ( 1) (unary) -(unary) // [] () (method call) . (dot -- identifier qualification) // aggregate function // () (explicit parenthesis)

However, the implementation does not:

// level 2 - binary multiply and divide multiplyExpression : unaryExpression ( ( STAR | DIV ) unaryExpression )* ;

It also seems like it would be possible to add a MOD keyword as follows:

MOD: '%';

And then update the multiplyExpression as follows:

// level 2 - binary multiply and divide multiplyExpression : unaryExpression ( ( STAR | DIV | MOD^ ) unaryExpression )* ;

Which would implement the functionality as it is denoted in the comments.

Is this something that would be considered? With regards to making a patch, what would be involved besides modifications to the HQL.g file?

Thanks, Dana


Julian Maughan added a comment — :

I agree with your proposed change, however, the related issue you mentioned (NH-2031) is Resolved in 3.0.0.Alpha1...so couldn't you use that instead?


Jeff Hagen added a comment — :

The NH-2031 fix only addresses the order of precedence from what I can see. The source code at the head revision still has no support for the character literal '%' (the modulo operator). Antlr just barfs with a no viable alternative exception when it finds one in the input stream.

I'd use the built in mod() function but it appears to have a bug when using it with functions as arguments:

mod(datediff(day, SomeDate, getdate()), 14) = 0

gets translated to

day % SomeDate


Fabio Maulo added a comment — :

'day' is the name of another HQL function


Jeff Hagen added a comment — :

Yes, 'day' sometimes causes issues with HQL. I tried 'dd' as well - same issue.


Fabio Maulo added a comment — :

Jeff, your is another issue. Please create a specific ticket. Thanks.


Fabio Maulo added a comment — :

changed to improv. since the HQL function 'mod' do the right work.

nhibernate-bot avatar Oct 12 '17 12:10 nhibernate-bot