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

Support Reuse of Custom Raw Expressions #370

Closed
aghchan opened this issue Jul 30, 2024 · 4 comments
Closed

Support Reuse of Custom Raw Expressions #370

aghchan opened this issue Jul 30, 2024 · 4 comments

Comments

@aghchan
Copy link

aghchan commented Jul 30, 2024

Is your feature request related to a problem? Please describe.
Unable to reuse functions of unsupported expressions/operations e.g. sample custom expressions below. would like to convert these expressions to string or take the expressions as input to write another custom function using these two as input

func WordSimilarity(word, tableName, columnName string) postgres.Expression {
return postgres.Raw("word_similarity('" + word + "', " + tableName + "." + columnName + ")")
}

func CosineDistance(embedding Vector, tableName, columnName string) postgres.BoolExpression {
return postgres.RawBool("'" + embedding.String() + "' <=> " + tableName + "." + columnName)
}

Describe the solution you'd like

  • supporting operations using custom expressions e.g. if I wrote custom expressions for unsupported functions, I want to sum up the results either via a func that takes expressions as inputs or a way to convert the expressions back to strings to input into a raw expressions
@houten11
Copy link

houten11 commented Jul 31, 2024

If you want to reuse your custom function or operators, they'll have to accept Expression as a input parameter.

func WordSimilarity(word1, word2 StringExpression) FloatExpression {
	return FloatExp(Func("word_similarity", word1, word2))
}

Note the use of Func - https://github.com/go-jet/jet/wiki/FAQ#how-to-call-custom-or-currently-unsupported-sql-function

Now you can call this function as:

WordSimilarity(String("John"), Actor.LastName)
WordSimilarity(String("James"), LTRIM(Actor.FirstName, String("A")))
etc...

For CosineDistance operator you can use the new CustomExpression function - #355 (comment)
This method is merged to the master branch but is not yet part of the latest release.

func CosineDistance(vector1, vector2 Expression) FloatExpression {
	return FloatExp(CustomExpression(vector1, Token("<=>"), vector2))
}

Since pgvector is currently unsupported, you'll have to add it yourself:

func PgVector(array ...string) Expression {
	return Raw("#1", RawArgs{"#1": <your pgvector type>})
}

Finally, you should be able to write:

CosineDistance(PgVector("john", "doe"), PgVector("mike", "jones", "jim", "nate"))
CosineDistance(PgVector("john"), Actor.PgVectorColumn)
etc...

@aghchan
Copy link
Author

aghchan commented Jul 31, 2024

thanks for the suggestion @houten11! I should have clarified further in my example. I want to be able to be able to sum the two functions together without writing a new function altogether (going to use this in the select clause) e.g. CosineDistance() + WordSimilarity()

my current workaround is not wrapping the raw queries into the expression types and then wrapping it after appending them together

@houten11
Copy link

Both functions returns FloatExpression, meaning you can write:

WordSimilarity(String("John"), Actor.LastName).ADD(
    CosineDistance(PgVector("john", "doe"), PgVector("mike", "jones", "jim", "nate")))

@aghchan
Copy link
Author

aghchan commented Jul 31, 2024

ahhh I totally goofed on that. Just need to use the FloatExpression instead of the generic Expression. thanks!

@aghchan aghchan closed this as completed Jul 31, 2024
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