Skip to content

Commit

Permalink
add filter clause support at AggregateFunctionBuilder. (#208)
Browse files Browse the repository at this point in the history
* add filter to AggregateFunctionNode.

* add filter handling @ DefaultQueryCompiler.visitAggregateFunction.

* add filter field @ OperationNodeTransformer.transformAggregateFunction.

* add filter methods @ AggregateFunctionBuilder.

* fix postgres-json test tsc error.

* add some filter unit tests @ aggregate-function.

* rename `filter` methods to `filterWhere`.

Have slept on it, think this is more aligned with Kysely's API philosophy.

* align with recent WhereNode changes.

* align with recent filter-parser refactor.

* remove .only from aggregate function module unit tests.

* AggregateFunctionBuilder.filterWhere typings tests.

* AggregateFunctionBuilder.filterWhereRef typings tests.

* add ts docs to filterWhere methods @ `AggregateFunctionBuilder`.
  • Loading branch information
igalklebanov authored Dec 15, 2022
1 parent cb8d192 commit 72b0054
Show file tree
Hide file tree
Showing 8 changed files with 828 additions and 10 deletions.
34 changes: 34 additions & 0 deletions src/operation-node/aggregate-function-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { freeze } from '../util/object-utils.js'
import { OperationNode } from './operation-node.js'
import { OverNode } from './over-node.js'
import { SimpleReferenceExpressionNode } from './simple-reference-expression-node.js'
import { WhereNode } from './where-node.js'

type AggregateFunction = 'avg' | 'count' | 'max' | 'min' | 'sum'

Expand All @@ -10,6 +11,7 @@ export interface AggregateFunctionNode extends OperationNode {
readonly func: AggregateFunction
readonly column: SimpleReferenceExpressionNode
readonly distinct?: boolean
readonly filter?: WhereNode
readonly over?: OverNode
}

Expand Down Expand Up @@ -41,6 +43,38 @@ export const AggregateFunctionNode = freeze({
})
},

cloneWithFilter(
aggregateFunctionNode: AggregateFunctionNode,
filter: OperationNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
filter: aggregateFunctionNode.filter
? WhereNode.cloneWithOperation(
aggregateFunctionNode.filter,
'And',
filter
)
: WhereNode.create(filter),
})
},

cloneWithOrFilter(
aggregateFunctionNode: AggregateFunctionNode,
filter: OperationNode
): AggregateFunctionNode {
return freeze({
...aggregateFunctionNode,
filter: aggregateFunctionNode.filter
? WhereNode.cloneWithOperation(
aggregateFunctionNode.filter,
'Or',
filter
)
: WhereNode.create(filter),
})
},

cloneWithOver(
aggregateFunctionNode: AggregateFunctionNode,
over?: OverNode
Expand Down
1 change: 1 addition & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ export class OperationNodeTransformer {
kind: 'AggregateFunctionNode',
column: this.transformNode(node.column),
distinct: node.distinct,
filter: this.transformNode(node.filter),
func: node.func,
over: this.transformNode(node.over),
})
Expand Down
Loading

0 comments on commit 72b0054

Please sign in to comment.