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

More convenient way to implement raw/custom operators #338

Closed
kblomster opened this issue Mar 26, 2024 · 5 comments
Closed

More convenient way to implement raw/custom operators #338

kblomster opened this issue Mar 26, 2024 · 5 comments

Comments

@kblomster
Copy link
Contributor

kblomster commented Mar 26, 2024

Problem

When we want to call a function that Jet doesn't support natively, we can use jet.Func("funcname", SomeExpression, ...). This is convenient; only the function name needs to be a hardcoded string, while the arguments can be any Jet expression. Jet will take care of serializing the expressions to SQL.

However, if we want to use an operator that Jet doesn't support, say for example ILIKE (aka. ~~*) in postgres, then we have to resort to jet.Raw, which is not nearly as convenient: we have to serialize the operands ourselves in some way. Expressions that are actual columns are easy enough, we can just say someColumn.Name() and put that into the raw string, but if it's a more complex expression I don't see an obvious way of doing it.

Proposed solution

I see that internally Jet does have a function that does almost exactly what I'd like: NewBinaryOperatorExpression(lhs Serializer, rhs Serializer, operator string). If the public API exposed a wrapper around this, something like jet.BinaryOperator(lhs jet.Expression, rhs jet.Expression, operator string) jet.Expression then I could do things like this to implement my own operators:

func ILIKE(lhs jet.StringExpression, rhs jet.StringExpression) jet.BoolExpression {
    return jet.BoolExp(jet.BinaryOperator(lhs, rhs, "~~*"))
}
@go-jet
Copy link
Owner

go-jet commented Mar 28, 2024

Makes sense.

@kblomster
Copy link
Contributor Author

Thanks! I'm happy with this so I'll close the issue :)

@kblomster
Copy link
Contributor Author

Actually, reopening this. I've cloned the wiki and added documentation about BinaryOperator to the FAQ in my cloned repo: https://github.com/kblomster/jet/wiki/FAQ#how-to-use-custom-or-currently-unsupported-sql-operators

I'm not sure how easy it is to get wiki changes upstreamed, but this gist suggests a workflow at least: https://gist.github.com/omaraboumrad/35654da0a376c57a2e0ab4d92ad0c339#core-contributors

Let me know if you have feedback on the docs.

@kblomster kblomster reopened this Apr 2, 2024
@go-jet
Copy link
Owner

go-jet commented Apr 16, 2024

Merged. 👍

@kblomster
Copy link
Contributor Author

Thanks! With that I'll consider this issue well addressed. :)

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

No branches or pull requests

2 participants