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

Add support for custom operators #641

Merged
merged 3 commits into from
Jun 13, 2023
Merged

Add support for custom operators #641

merged 3 commits into from
Jun 13, 2023

Conversation

rakbladsvalsen
Copy link
Contributor

@rakbladsvalsen rakbladsvalsen commented May 18, 2023

PR Info

See SeaORM discord

New Features

  • Add support for arbitrary custom operators
  • Add Regex op for Postgres
  • Add RegexCaseInsensitive op for Postgres

Notes

This PR is still a little bit rough around the edges, but hey, I'm open to comments. :)

@rakbladsvalsen
Copy link
Contributor Author

rakbladsvalsen commented May 18, 2023

Some questions:

  • Should I add a new type for custom operators? Currently I just used Alias (since it implements Into Iden), but using "alias" for a custom operator feels weird. Maybe a new CustOp type would make sense?
  • I nuked the Copy trait from BinOper, but this has the downside of adding a clone call somewhere. This is my first time contributing to sea-query, and I'm rather ashamed to ask this: Is there an easy way to make Ident Copy? I tried adding the bound but I epicly failed at it.

EDIT: it appears we don't need BinOper to be Copy at all. In fact, there was some call that was inadvertently performing a bitwise copy, but I changed it to use a reference and voilá! there's no clone now.

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Hey @rakbladsvalsen, thanks for the contribution!! I got a few comments :)

Comment on lines -2522 to +2524
pub(crate) fn get_bin_oper(&self) -> Option<BinOper> {
pub(crate) fn get_bin_oper(&self) -> Option<&BinOper> {
match self {
Self::Binary(_, oper, _) => Some(*oper),
Self::Binary(_, oper, _) => Some(oper),
Copy link
Member

Choose a reason for hiding this comment

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

Okay, an optimization

src/types.rs Outdated
@@ -72,6 +72,8 @@ impl Clone for SeaRc<dyn Iden> {
}
}

impl Eq for SeaRc<dyn Iden> {}
Copy link
Member

Choose a reason for hiding this comment

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

Hmmmm... Eq for dyn Iden

Consider this. Where both dyn Iden yield the same identifier id but it's not the same Rust structure / enum.

#[derive(Iden)]
struct Id;

// Both yield the string "id"
assert_eq!(Id.to_string(), "id");
assert_eq!(Alias::new("id").to_string(), "id");

// How about this??
assert_eq!(Id, Alias::new("id"));

Copy link
Contributor Author

@rakbladsvalsen rakbladsvalsen May 24, 2023

Choose a reason for hiding this comment

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

Isn't this already covered by the PartialEq implementation that already exists for DynIden? The current implementation checks two things: whether the .to_string() contents are equal and whether the vtable pointer is the same for the two objects, though this only works for dyn objects.

Anyway, if we wanted to provide the ability to do something like the last line in your example, it'd be necessary to implement something like fn get_iden_id() to allow direct comparison between Iden objects, i.e. Id.get_iden_id() == Alias::new("id").get_iden_id() (which is probably already doable using to_string()).

Though I must say, this feels rather unnatural. I don't think comparison between two different struct types is something a lot of users are doing. There are no feature requests for this either, so I think we're good in that regard.

Copy link
Member

Choose a reason for hiding this comment

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

@tyt2y3 thoughts?

src/types.rs Outdated
@@ -158,7 +160,7 @@ pub enum UnOper {
}

/// Binary operator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
Copy link
Member

Choose a reason for hiding this comment

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

We should not remove the Copy attribute.

src/types.rs Outdated
@@ -185,6 +187,7 @@ pub enum BinOper {
RShift,
As,
Escape,
Custom(DynIden),
Copy link
Member

@tyt2y3 tyt2y3 May 29, 2023

Choose a reason for hiding this comment

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

May be we can have a new type CustomOper, which is simply a wrapped &'static str.

This would have avoided removing Copy.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I was not aware we need to remove the Copy derive. So, yes, &'static str is a better alternative.

@rakbladsvalsen what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback guys! This PR should be good to go.

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

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

Thanks!! @rakbladsvalsen

@rakbladsvalsen rakbladsvalsen requested a review from tyt2y3 June 2, 2023 14:31
Copy link
Member

@ikrivosheev ikrivosheev left a comment

Choose a reason for hiding this comment

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

@rakbladsvalsen thank you! LGTM!

Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Awesome, looks neat!

@billy1624 billy1624 merged commit 4f2c525 into SeaQL:master Jun 13, 2023
@rakbladsvalsen rakbladsvalsen deleted the custom-op branch June 13, 2023 17:51
@github-actions
Copy link

🎉 Released In 0.29.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants