Skip to content
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

HHH-16861 HQL ordinal() function #8991

Merged
merged 1 commit into from
Sep 30, 2024
Merged

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
Contributor

@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
Contributor

@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

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
Contributor

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