Skip to content

HHH-9812 @Formula with SQL cast function #10377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/sql/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public static String renderWhereStringTemplate(
boolean inFromClause = false;
boolean afterFromTable = false;
boolean inExtractOrTrim = false;
boolean inCast = false;
boolean afterCastAs = false;

boolean hasMore = tokens.hasMoreTokens();
String nextToken = hasMore ? tokens.nextToken() : null;
Expand Down Expand Up @@ -237,6 +239,14 @@ else if ( FUNCTION_WITH_FROM_KEYWORDS.contains(lcToken) && "(".equals( nextToken
result.append(token);
inExtractOrTrim = true;
}
else if ( "cast".equals( lcToken ) ) {
result.append( token );
inCast = true;
}
else if ( inCast && ("as".equals( lcToken ) || afterCastAs) ) {
result.append( token );
afterCastAs = true;
}
else if ( !inFromClause // don't want to append alias to tokens inside the FROM clause
&& isIdentifier( token )
&& !isFunctionOrKeyword( lcToken, nextToken, dialect, typeConfiguration )
Expand All @@ -248,6 +258,8 @@ && isIdentifier( token )
else {
if ( ")".equals( lcToken) ) {
inExtractOrTrim = false;
inCast = false;
afterCastAs = false;
}
else if ( !inExtractOrTrim
&& BEFORE_TABLE_KEYWORDS.contains(lcToken) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public void templateLiterals(SessionFactoryScope scope) {
"where {@}.foo>10 and {@}.bar is not null", factory );
assertWhereStringTemplate("select t.foo, o.bar from table as t left join other as o on t.id = o.id where t.foo>10 and o.bar is not null order by o.bar",
"select t.foo, o.bar from table as t left join other as o on t.id = o.id where t.foo>10 and o.bar is not null order by o.bar", factory );

assertWhereStringTemplate( "CAST(foo AS unsigned)",
"CAST({@}.foo AS unsigned)", factory );
assertWhereStringTemplate( "CAST(foo AS signed)",
"CAST({@}.foo AS signed)", factory );
}

private static void assertWhereStringTemplate(String sql, SessionFactoryImplementor sf) {
Expand Down