-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-16861 HQL ordinal() function #8991
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
hibernate-core/src/main/java/org/hibernate/dialect/function/OrdinalFunction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.dialect.function; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.hibernate.QueryException; | ||
| import org.hibernate.metamodel.mapping.JdbcMapping; | ||
| import org.hibernate.query.ReturnableType; | ||
| import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; | ||
| import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator; | ||
| import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; | ||
| import org.hibernate.sql.ast.SqlAstTranslator; | ||
| import org.hibernate.sql.ast.spi.SqlAppender; | ||
| import org.hibernate.sql.ast.tree.SqlAstNode; | ||
| import org.hibernate.sql.ast.tree.expression.Expression; | ||
| import org.hibernate.type.SqlTypes; | ||
| import org.hibernate.type.StandardBasicTypes; | ||
| import org.hibernate.type.descriptor.java.EnumJavaType; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
| import org.hibernate.type.spi.TypeConfiguration; | ||
|
|
||
| import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ENUM; | ||
|
|
||
|
|
||
| /** | ||
| * The HQL {@code ordinal()} function returns the ordinal value of an enum | ||
| * <p> | ||
| * For enum fields mapped as ORDINAL it's a synonym for {@code cast(x as Integer)}. Same as {@link CastStrEmulation} but for Integer. | ||
| * For enum fields mapped as STRING or ENUM it's a case statement that returns the ordinal value. | ||
| * | ||
| * @author Luca Molteni | ||
| */ | ||
| public class OrdinalFunction | ||
| extends AbstractSqmSelfRenderingFunctionDescriptor { | ||
|
|
||
| public OrdinalFunction(TypeConfiguration typeConfiguration) { | ||
| super( | ||
| "ordinal", | ||
| new ArgumentTypesValidator( null, ENUM ), | ||
| StandardFunctionReturnTypeResolvers.invariant( | ||
| typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.INTEGER ) | ||
| ), | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void render( | ||
| SqlAppender sqlAppender, | ||
| List<? extends SqlAstNode> arguments, | ||
| ReturnableType<?> returnType, | ||
| SqlAstTranslator<?> walker) { | ||
| Expression singleExpression = (Expression) arguments.get( 0 ); | ||
|
|
||
| JdbcMapping singleJdbcMapping = singleExpression.getExpressionType().getSingleJdbcMapping(); | ||
| JdbcType argumentType = singleJdbcMapping.getJdbcType(); | ||
|
|
||
| if ( argumentType.isInteger() ) { | ||
| singleExpression.accept( walker ); | ||
| } | ||
| else if ( argumentType.isString() || argumentType.getDefaultSqlTypeCode() == SqlTypes.ENUM ) { | ||
|
|
||
| EnumJavaType<?> enumJavaType = (EnumJavaType<?>) singleJdbcMapping.getMappedJavaType(); | ||
| Object[] enumConstants = enumJavaType.getJavaTypeClass().getEnumConstants(); | ||
|
|
||
| sqlAppender.appendSql( "case " ); | ||
| singleExpression.accept( walker ); | ||
| for ( Object e : enumConstants ) { | ||
| Enum<?> enumValue = (Enum<?>) e; | ||
| sqlAppender.appendSql( " when " ); | ||
| sqlAppender.appendSingleQuoteEscapedString( (String) singleJdbcMapping.convertToRelationalValue( | ||
| enumValue.toString() ) ); | ||
| sqlAppender.appendSql( " then " ); | ||
| sqlAppender.appendSql( enumValue.ordinal() ); | ||
| } | ||
| sqlAppender.appendSql( " end" ); | ||
| } | ||
| else { | ||
| throw new QueryException( "Unsupported enum type passed to 'ordinal()' function: " + argumentType ); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getArgumentListSignature() { | ||
| return "(ENUM arg)"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
hibernate-core/src/test/java/org/hibernate/orm/test/hql/EnumTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * SPDX-License-Identifier: LGPL-2.1-or-later | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.hql; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.Jira; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DomainModel(annotatedClasses = { | ||
| EntityOfBasics.class, | ||
| EntityOfBasics.Gender.class, | ||
| }) | ||
| @SessionFactory | ||
| @Jira("https://hibernate.atlassian.net/browse/HHH-16861") | ||
| public class EnumTest { | ||
|
|
||
|
|
||
| @BeforeAll | ||
| public void setUp(SessionFactoryScope scope) { | ||
| scope.inTransaction(session -> { | ||
| EntityOfBasics male = new EntityOfBasics(); | ||
| male.setId( 20_000_000 ); | ||
| male.setGender( EntityOfBasics.Gender.MALE ); // Ordinal 0 | ||
| male.setOrdinalGender( EntityOfBasics.Gender.MALE ); // Ordinal 0 | ||
|
|
||
| EntityOfBasics female = new EntityOfBasics(); | ||
| female.setId( 20_000_001 ); | ||
| female.setGender( EntityOfBasics.Gender.FEMALE ); // Ordinal 1 | ||
| female.setOrdinalGender( EntityOfBasics.Gender.FEMALE ); // Ordinal 1 | ||
|
|
||
| session.persist( male ); | ||
| session.persist( female ); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testOrdinalFunctionOnOrdinalEnum(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
|
|
||
| List<Integer> femaleOrdinalFunction = session.createQuery( | ||
| "select ordinal(ordinalGender) " + | ||
| "from EntityOfBasics e " + | ||
| "where e.ordinalGender = :gender", | ||
| Integer.class | ||
| ) | ||
| .setParameter( "gender", EntityOfBasics.Gender.FEMALE ) | ||
| .getResultList(); | ||
|
|
||
| List<Integer> femaleWithCast = session.createQuery( | ||
| "select cast(e.ordinalGender as Integer) " + | ||
| "from EntityOfBasics e " + | ||
| "where e.ordinalGender = :gender", | ||
| Integer.class | ||
| ) | ||
| .setParameter( "gender", EntityOfBasics.Gender.FEMALE ) | ||
| .getResultList(); | ||
|
|
||
| assertThat( femaleOrdinalFunction ).hasSize( 1 ); | ||
| assertThat( femaleOrdinalFunction ).hasSameElementsAs( femaleWithCast ); | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testOrdinalFunctionOnStringEnum(SessionFactoryScope scope) { | ||
| scope.inTransaction( session -> { | ||
| List<Integer> femaleOrdinalFromString = session.createQuery( | ||
| "select ordinal(gender)" + | ||
| "from EntityOfBasics e " + | ||
| "where e.gender = :gender", | ||
| Integer.class | ||
| ) | ||
| .setParameter( "gender", EntityOfBasics.Gender.FEMALE ) | ||
| .getResultList(); | ||
|
|
||
| assertThat( femaleOrdinalFromString ).hasSize( 1 ); | ||
| assertThat( femaleOrdinalFromString ).hasSameElementsAs( List.of( 1 ) ); | ||
| } ); | ||
|
|
||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.