Skip to content

Commit 5bf22f0

Browse files
committed
NH-2290 Invalid hql parenthesis expansion in generated sql
1 parent e3c0f79 commit 5bf22f0

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

releasenotes.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
Build 3.4.0.CR1
22
=============================
3+
** Known BREAKING CHANGES from NH3.3.0.GA to NH3.4.0.GA
4+
5+
##### Possible Breaking Changes #####
6+
* [NH-2290] Invalid hql parenthesis expansion in generated sql
7+
Unary minus before parentheses in HQL lost the parentheses when translated
8+
to SQL and therefore the wrong value was returned. This use of unary minus is now
9+
implemented in the mathematically correct way.
10+
311

412
** Sub-task
513
* [NH-3434] - Need to be sure NH-3428 is resolved in 3.Next.

src/NHibernate.Test/Hql/Ast/HqlFixture.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Collections;
34
using NHibernate.Criterion;
45
using NHibernate.Engine.Query;
@@ -310,5 +311,29 @@ public void InsertIntoFromSelect_WithSelectClauseParameters()
310311
}
311312
}
312313
}
314+
315+
316+
[Test]
317+
public void UnaryMinusBeforeParenthesesHandledCorrectly()
318+
{
319+
using (ISession s = OpenSession())
320+
using (ITransaction txn = s.BeginTransaction())
321+
{
322+
s.Save(new Animal {Description = "cat1", BodyWeight = 1});
323+
324+
// NH-2290: Unary minus before parentheses wasn't handled correctly (this query returned 0).
325+
int actual = s.CreateQuery("select -(1+1) from Animal as h")
326+
.List<int>().Single();
327+
Assert.That(actual, Is.EqualTo(-2));
328+
329+
// This was the workaround, which of course should still work.
330+
int actualWorkaround = s.CreateQuery("select -1*(1+1) from Animal as h")
331+
.List<int>().Single();
332+
Assert.That(actualWorkaround, Is.EqualTo(-2));
333+
334+
s.CreateQuery("delete from Animal").ExecuteUpdate();
335+
txn.Commit();
336+
}
337+
}
313338
}
314339
}

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlLexer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// </auto-generated>
99
//------------------------------------------------------------------------------
1010

11-
// $ANTLR 3.5.0.2 Hql.g 2014-08-03 13:32:00
11+
// $ANTLR 3.5.0.2 Hql.g 2014-08-03 16:52:01
1212

1313
// The variable 'variable' is assigned but its value is never used.
1414
#pragma warning disable 219

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// </auto-generated>
99
//------------------------------------------------------------------------------
1010

11-
// $ANTLR 3.5.0.2 Hql.g 2014-08-03 13:32:00
11+
// $ANTLR 3.5.0.2 Hql.g 2014-08-03 16:52:01
1212

1313
// The variable 'variable' is assigned but its value is never used.
1414
#pragma warning disable 219

src/NHibernate/Hql/Ast/ANTLR/Generated/HqlSqlWalker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// </auto-generated>
99
//------------------------------------------------------------------------------
1010

11-
// $ANTLR 3.5.0.2 HqlSqlWalker.g 2014-08-03 13:32:01
11+
// $ANTLR 3.5.0.2 HqlSqlWalker.g 2014-08-03 16:52:02
1212

1313
// The variable 'variable' is assigned but its value is never used.
1414
#pragma warning disable 219

src/NHibernate/Hql/Ast/ANTLR/Generated/SqlGenerator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// </auto-generated>
99
//------------------------------------------------------------------------------
1010

11-
// $ANTLR 3.5.0.2 SqlGenerator.g 2014-08-03 13:32:03
11+
// $ANTLR 3.5.0.2 SqlGenerator.g 2014-08-03 16:52:04
1212

1313
// The variable 'variable' is assigned but its value is never used.
1414
#pragma warning disable 219
@@ -5717,7 +5717,7 @@ private TreeRuleReturnScope<IASTNode> constant()
57175717
partial void EnterRule_arithmeticExpr();
57185718
partial void LeaveRule_arithmeticExpr();
57195719
// $ANTLR start "arithmeticExpr"
5720-
// SqlGenerator.g:293:1: arithmeticExpr : ( additiveExpr | bitwiseExpr | multiplicativeExpr | ^( UNARY_MINUS expr ) | caseExpr );
5720+
// SqlGenerator.g:293:1: arithmeticExpr : ( additiveExpr | bitwiseExpr | multiplicativeExpr | ^( UNARY_MINUS nestedExprAfterMinusDiv ) | caseExpr );
57215721
[GrammarRule("arithmeticExpr")]
57225722
private void arithmeticExpr()
57235723
{
@@ -5728,7 +5728,7 @@ private void arithmeticExpr()
57285728
DebugLocation(293, 1);
57295729
try
57305730
{
5731-
// SqlGenerator.g:294:2: ( additiveExpr | bitwiseExpr | multiplicativeExpr | ^( UNARY_MINUS expr ) | caseExpr )
5731+
// SqlGenerator.g:294:2: ( additiveExpr | bitwiseExpr | multiplicativeExpr | ^( UNARY_MINUS nestedExprAfterMinusDiv ) | caseExpr )
57325732
int alt55=5;
57335733
try { DebugEnterDecision(55, false);
57345734
switch (input.LA(1))
@@ -5814,7 +5814,7 @@ private void arithmeticExpr()
58145814
break;
58155815
case 4:
58165816
DebugEnterAlt(4);
5817-
// SqlGenerator.g:298:4: ^( UNARY_MINUS expr )
5817+
// SqlGenerator.g:298:4: ^( UNARY_MINUS nestedExprAfterMinusDiv )
58185818
{
58195819
DebugLocation(298, 4);
58205820
DebugLocation(298, 6);
@@ -5828,8 +5828,8 @@ private void arithmeticExpr()
58285828

58295829
Match(input, TokenTypes.Down, null); if (state.failed) return;
58305830
DebugLocation(298, 32);
5831-
PushFollow(Follow._expr_in_arithmeticExpr1659);
5832-
expr();
5831+
PushFollow(Follow._nestedExprAfterMinusDiv_in_arithmeticExpr1659);
5832+
nestedExprAfterMinusDiv();
58335833
PopFollow();
58345834
if (state.failed) return;
58355835

@@ -8407,7 +8407,7 @@ private static class Follow
84078407
public static readonly BitSet _bitwiseExpr_in_arithmeticExpr1643 = new BitSet(new ulong[]{0x2UL});
84088408
public static readonly BitSet _multiplicativeExpr_in_arithmeticExpr1648 = new BitSet(new ulong[]{0x2UL});
84098409
public static readonly BitSet _UNARY_MINUS_in_arithmeticExpr1655 = new BitSet(new ulong[]{0x4UL});
8410-
public static readonly BitSet _expr_in_arithmeticExpr1659 = new BitSet(new ulong[]{0x8UL});
8410+
public static readonly BitSet _nestedExprAfterMinusDiv_in_arithmeticExpr1659 = new BitSet(new ulong[]{0x8UL});
84118411
public static readonly BitSet _caseExpr_in_arithmeticExpr1665 = new BitSet(new ulong[]{0x2UL});
84128412
public static readonly BitSet _PLUS_in_additiveExpr1677 = new BitSet(new ulong[]{0x4UL});
84138413
public static readonly BitSet _expr_in_additiveExpr1679 = new BitSet(new ulong[]{0x8801003140ED150UL,0x561444C01F814010UL,0x208080UL});

src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ arithmeticExpr
295295
| bitwiseExpr
296296
| multiplicativeExpr
297297
// | ^(CONCAT { Out("("); } expr ( { Out("||"); } expr )+ { Out(")"); } )
298-
| ^(UNARY_MINUS { Out("-"); } expr)
298+
| ^(UNARY_MINUS { Out("-"); } nestedExprAfterMinusDiv)
299299
| caseExpr
300300
;
301301

0 commit comments

Comments
 (0)