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

enable jet.Windows passed in RawArgs to Raw expressions? #106

Closed
sreuland opened this issue Dec 1, 2021 · 7 comments
Closed

enable jet.Windows passed in RawArgs to Raw expressions? #106

sreuland opened this issue Dec 1, 2021 · 7 comments

Comments

@sreuland
Copy link

sreuland commented Dec 1, 2021

Hello I'm using some of the Raw Expressions to use custom functions of postgres that aren't mapped in gojet yet, such as percentile_disc and within group, I'm running into limitation of using the Raw format, where I have within group in raw, but it needs to contain a standard ORDER BY clause inside it, there's no way for me to substitute the jet.ORDER_BY window into the Raw string via RawArgs, could that be enabled as a feature?

would look something like:

args := jet.RawArgs {
   	    "PERCENTILE": percentile, 
   	    "ORDER_BY": jet.ORDER_BY(table.MyTable.MyCol1.DIV(table.MyTable.MyCol2))}
projection := jet.CAST(jet.CEIL(jet.RawFloat("percentile_disc(0.:PERCENTILE) WITHIN GROUP (:ORDER_BY)", args))).AS_BIGINT().AS("MyProjectedColumn")  

Otherwise, have to resort to hardcoding the full raw snippet including the ORDER BY clause and its table/column references, which defeats the purpose of using the lib. Is it possible to achieve this any other ways?

@go-jet
Copy link
Owner

go-jet commented Dec 1, 2021

Hello, try using this method, until those aggregate functions are added.

func PERCENTILE_DISC(fraction float64, withinGroup Column) Expression {
    raw := fmt.Sprintf("PERCENTILE_DISC(#fraction) WITHIN GROUP (ORDER BY %s.%s)", withinGroup.TableName(), withinGroup.Name())
    return Raw(raw, RawArgs{"#fraction": fraction})
}

@sreuland
Copy link
Author

sreuland commented Dec 2, 2021

Hello, thanks for alternative suggestion, it won't scale well into some of the more intricate uses cases with different expressions in the order by, like having to represent a quotient from two db columns etc, will end up with several variations of this snippet for each form, would like to avoid that from onset.

If that can be accomplished through Raw substitution would be one way to resolve, or if there was way for clients to implement their own expressions in a given dialect, we could attempt to implement our own expressions with sql builders for specific custom dialect functions we unfortunately have to deal with that aren't mapped yet, like PERCENTILE_DISC and WITHIN_GROUP. Thanks for quick response, much appreciated!

@go-jet
Copy link
Owner

go-jet commented Dec 3, 2021

Expression serialize method is not exposed(for safety reasons), so I don't see alternative. You'll have to hard code expression part into new functions.
There is a Func helper you can use to implement missing functions, but in this case it wont work because PERCENTILE_DISC is complex function of two parts.

@go-jet go-jet added this to the Version 2.7.0 milestone Dec 3, 2021
@go-jet
Copy link
Owner

go-jet commented Dec 7, 2021

You can now find postgres PERCENTILE_DISC implementation on aggregate-func branch(the branch is in sync with master). Sample usage can be found here.

@sreuland
Copy link
Author

Looks great, I will try out the branch, thanks for adding that support.

@sreuland
Copy link
Author

confirmed using aggregate-func branch, changes for PERCENTILE_DISC and WITHIN_GROUP worked, thanks!

@go-jet
Copy link
Owner

go-jet commented Jan 20, 2022

Order set aggregate functions added in Release 2.7.0.
Sample usage:

stmt := SELECT(
PERCENTILE_DISC(Float(0.1)).WITHIN_GROUP_ORDER_BY(Invoice.InvoiceId).AS("percentile_disc_1"),
PERCENTILE_DISC(Invoice.Total.DIV(Float(100))).WITHIN_GROUP_ORDER_BY(Invoice.InvoiceDate.ASC()).AS("percentile_disc_2"),
PERCENTILE_DISC(RawFloat("(select array_agg(s) from generate_series(0, 1, 0.2) as s)")).
WITHIN_GROUP_ORDER_BY(Invoice.BillingAddress.DESC()).AS("percentile_disc_3"),
PERCENTILE_CONT(Float(0.3)).WITHIN_GROUP_ORDER_BY(Invoice.Total).AS("percentile_cont_1"),
PERCENTILE_CONT(Float(0.2)).WITHIN_GROUP_ORDER_BY(INTERVAL(1, HOUR).DESC()).AS("percentile_cont_int"),
MODE().WITHIN_GROUP_ORDER_BY(Invoice.BillingPostalCode.DESC()).AS("mode_1"),
).FROM(

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