From bd57af6d9798b304a2fb0ef5d3325cc63765f2e7 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 9 Apr 2023 18:41:58 +0200 Subject: [PATCH] HHH-16449 accept underscores in HQL integer and long literals --- .../src/main/antlr/org/hibernate/grammars/hql/HqlLexer.g4 | 4 ++-- .../hibernate/query/hql/internal/SemanticQueryBuilder.java | 6 +++--- .../java/org/hibernate/orm/test/query/hql/LiteralTests.java | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlLexer.g4 b/hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlLexer.g4 index 0c0203728f..b49c62a29d 100644 --- a/hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlLexer.g4 +++ b/hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlLexer.g4 @@ -58,9 +58,9 @@ FLOATING_POINT_NUMBER | DIGIT+ ; -INTEGER_LITERAL : INTEGER_NUMBER; +INTEGER_LITERAL : INTEGER_NUMBER ('_' INTEGER_NUMBER)*; -LONG_LITERAL : INTEGER_NUMBER LONG_SUFFIX; +LONG_LITERAL : INTEGER_NUMBER ('_' INTEGER_NUMBER)* LONG_SUFFIX; FLOAT_LITERAL : FLOATING_POINT_NUMBER FLOAT_SUFFIX?; diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java index 11608cf6c7..8cf515f18a 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java @@ -3622,7 +3622,7 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implem private SqmLiteral integerOrLongLiteral(String text) { try { - final Integer value = Integer.valueOf( text ); + final Integer value = Integer.valueOf( text.replace("_", "") ); return new SqmLiteral<>( value, resolveExpressibleTypeBasic( Integer.class ), @@ -3651,7 +3651,7 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implem private SqmLiteral integerLiteral(String text) { try { - final Integer value = Integer.valueOf( text ); + final Integer value = Integer.valueOf( text.replace("_", "") ); return new SqmLiteral<>( value, resolveExpressibleTypeBasic( Integer.class ), @@ -3670,7 +3670,7 @@ public class SemanticQueryBuilder extends HqlParserBaseVisitor implem final String originalText = text; try { if ( text.endsWith( "l" ) || text.endsWith( "L" ) ) { - text = text.substring( 0, text.length() - 1 ); + text = text.substring( 0, text.length() - 1 ).replace("_", ""); } final Long value = Long.valueOf( text ); return new SqmLiteral<>( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/LiteralTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/LiteralTests.java index f2e03abb16..6180b94e1c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/LiteralTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/LiteralTests.java @@ -302,6 +302,8 @@ public class LiteralTests { scope.inTransaction( session -> { assertThat( session.createQuery( "select 1" ).getSingleResult(), is( 1 ) ); + assertThat( session.createQuery( "select 1_000_000" ).getSingleResult(), is( 1_000_000 ) ); + assertThat( session.createQuery( "select 1_000_000L" ).getSingleResult(), is( 1_000_000L ) ); } ); }