forked from go-jet/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Postgres] Add order set aggregate functions support.
- Loading branch information
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package jet | ||
|
||
// MODE computes the most frequent value of the aggregated argument | ||
func MODE() *OrderSetAggregateFunc { | ||
return newOrderSetAggregateFunction("MODE", nil) | ||
} | ||
|
||
// PERCENTILE_CONT computes a value corresponding to the specified fraction within the ordered set of | ||
// aggregated argument values. This will interpolate between adjacent input items if needed. | ||
func PERCENTILE_CONT(fraction FloatExpression) *OrderSetAggregateFunc { | ||
return newOrderSetAggregateFunction("PERCENTILE_CONT", fraction) | ||
} | ||
|
||
// PERCENTILE_DISC computes the first value within the ordered set of aggregated argument values whose position | ||
// in the ordering equals or exceeds the specified fraction. The aggregated argument must be of a sortable type. | ||
func PERCENTILE_DISC(fraction FloatExpression) *OrderSetAggregateFunc { | ||
return newOrderSetAggregateFunction("PERCENTILE_DISC", fraction) | ||
} | ||
|
||
// OrderSetAggregateFunc implementation of order set aggregate function | ||
type OrderSetAggregateFunc struct { | ||
name string | ||
fraction FloatExpression | ||
orderBy Window | ||
} | ||
|
||
func newOrderSetAggregateFunction(name string, fraction FloatExpression) *OrderSetAggregateFunc { | ||
return &OrderSetAggregateFunc{ | ||
name: name, | ||
fraction: fraction, | ||
} | ||
} | ||
|
||
// WITHIN_GROUP_ORDER_BY specifies ordered set of aggregated argument values | ||
func (p *OrderSetAggregateFunc) WITHIN_GROUP_ORDER_BY(orderBy OrderByClause) Expression { | ||
p.orderBy = ORDER_BY(orderBy) | ||
return newOrderSetAggregateFuncExpression(*p) | ||
} | ||
|
||
func newOrderSetAggregateFuncExpression(aggFunc OrderSetAggregateFunc) *orderSetAggregateFuncExpression { | ||
ret := &orderSetAggregateFuncExpression{ | ||
OrderSetAggregateFunc: aggFunc, | ||
} | ||
|
||
ret.ExpressionInterfaceImpl.Parent = ret | ||
|
||
return ret | ||
} | ||
|
||
type orderSetAggregateFuncExpression struct { | ||
ExpressionInterfaceImpl | ||
OrderSetAggregateFunc | ||
} | ||
|
||
func (p *orderSetAggregateFuncExpression) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) { | ||
out.WriteString(p.name) | ||
WRAP(p.fraction).serialize(statement, out, FallTrough(options)...) | ||
out.WriteString("WITHIN GROUP") | ||
p.orderBy.serialize(statement, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters