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

EntityID as Number #1597

Closed
smelfungus opened this issue Sep 17, 2022 · 7 comments
Closed

EntityID as Number #1597

smelfungus opened this issue Sep 17, 2022 · 7 comments

Comments

@smelfungus
Copy link

Hello! Thank you for that amazing framework.
Is there a way to get an underlying number (long) column expression from EntityID provided by LongIdTable?
For instance, to implement id-based shuffling with a modulo:
AnyTable.id mod shuffleSeed

@Tapac
Copy link
Contributor

Tapac commented Sep 21, 2022

Do you mean SQL mod not kotlin one?

@smelfungus
Copy link
Author

@Tapac yes! So, at the end of the day, to have the ability to write something like that:

.orderBy(
    MyTable.id mod seed to SortOrder.ASC,
    MyTable.id to SortOrder.ASC
)

@AlexeySoshin
Copy link
Contributor

@dummyco
I think this should be already supported:

.orderBy(
    Expression.build { MyTable.id mod seed } to SortOrder.ASC,
    MyTable.id to SortOrder.ASC
)

@smelfungus
Copy link
Author

@AlexeySoshin it seems like ModOp is expecting <T : Number?, S : Number?> expressions, but EntityID does not satisfy it

@AlexeySoshin
Copy link
Contributor

AlexeySoshin commented Sep 27, 2022

After looking at the code some more, my comment above is incorrect, and what you need is the rem operator, not the mod operator.
As you correctly point out, ModOp executes the mod operation in the database, and that's what you want.

The reason ModOp doesn't work with EntityID is that the type is tight at the moment:

class ModOp<T : Number?, S : Number?>(

We could either loosen the type, which may result in runtime errors, or we may introduce another ModOp for a specific case of EntityID<Number?>

I attached a draft PR for the second case. Having difficulties testing it at the moment, though.

@smelfungus
Copy link
Author

@AlexeySoshin, thank you for looking into it and posting a PR. The solution with separate EntityID-friendly operators and Op-s sounds good. I wonder if Exposed should have this support for all the numeric (and maybe not only numeric but any applicable) operations to a type, wrapped by EntityID. Maybe even some sort of a generic approach that would allow Exposed to unwrap the EntityID type and apply all the standard ops. Thinking out loud 😅

@Tapac
Copy link
Contributor

Tapac commented Nov 14, 2022

Different mod/rem operations with EntityID columns are support since 0.41.1

@Tapac Tapac closed this as completed Nov 14, 2022
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

No branches or pull requests

3 participants