Skip to content

Commit

Permalink
fix: sUM aggregation is now coalesced to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
retro committed Apr 7, 2024
1 parent fefdb5c commit 0d6b615
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/dialect/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ export class BaseDialect {
if (value === "*") return value;
return `"${value}"`;
}
aggregateSum(sql: string) {
return `COALESCE(SUM(${sql}), 0)`;
}
}
5 changes: 4 additions & 1 deletion src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ export class Metric extends Member {
getAggregateSql(dialect: BaseDialect, context: unknown, modelAlias?: string) {
const { sql, bindings } = this.getSql(dialect, context, modelAlias);
return {
sql: `${this.props.aggregateWith.toUpperCase()}(${sql})`,
sql:
this.props.aggregateWith === "sum"
? dialect.aggregateSum(sql)
: `${this.props.aggregateWith.toUpperCase()}(${sql})`,
bindings,
};
}
Expand Down

0 comments on commit 0d6b615

Please sign in to comment.