Skip to content

Conversation

@lucamolteni
Copy link
Contributor

@lucamolteni lucamolteni commented Sep 20, 2024

Created the hql ordinal(field) function equivalent to cast(enum as Integer).
Implementation taken from CastStrEmulation


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-16861

@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Sep 20, 2024

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

Copy link
Member

@gavinking gavinking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good so far, but I think what would be really nice is if in the case of a STRING enum mapping, it would generate a SQL case expression mapping the string values to their ordinal values.

@lucamolteni
Copy link
Contributor Author

This looks good so far, but I think what would be really nice is if in the case of a STRING enum mapping, it would generate a SQL case expression mapping the string values to their ordinal values.

I'm working on that part now

@gavinking
Copy link
Member

I'm working on that part now

Awesome, that's great, thanks man.

Copy link
Member

@beikov beikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good first step. A few comments.

@lucamolteni lucamolteni changed the title [HHH-16861] HQL ordinal() function HHH-16861 HQL ordinal() function Sep 25, 2024
Copy link
Member

@beikov beikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting there :)
After you fix the last few things, please squash everything to a single commit and rebase

@lucamolteni lucamolteni marked this pull request as ready for review September 26, 2024 09:21
The `ordinal` function returns the `ordinal` property of Java enums, for both enums mapped as ORDINAL and enums mapped as STRING generating different SQL in each case

`ordinal(field)` is equivalent to `cast(enum as Integer)`, implementation taken from CastStrEmulation when used on ordinal mapped enums.

Lexer and parser don't need to be changed as there is nakedIdentifier that matches custom function names

`ordinal` function is validated to work only on Java enum fields

Use convertToRelationalValue to generate enum value inside the SQL query

Co-authored-by: Christian Beikov <[email protected]>
@beikov beikov merged commit f8e4e6e into hibernate:main Sep 30, 2024
19 of 20 checks passed
@beikov
Copy link
Member

beikov commented Sep 30, 2024

Thanks for the great work @lucamolteni. Would very much appreciate if you could add some documentation for this function as well e.g. in QueryLanguage.adoc

@gavinking
Copy link
Member

Thanks @lucamolteni !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants