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

feat: add interval type support #57

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/AggregateSpecsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const AggregateSpecsPlugin: Plugin = (builder) => {
builder.hook("build", (build) => {
const { pgSql: sql } = build;
const isNumberLike = (pgType: PgType): boolean => pgType.category === "N";
const isIntervalLike = (pgType: PgType): boolean =>
pgType.id === INTERVAL_OID;
/** Maps from the data type of the column to the data type of the sum aggregate */
/** BigFloat is our fallback type; it should be valid for almost all numeric types */
const convertWithMapAndFallback = (
Expand Down Expand Up @@ -48,7 +50,7 @@ const AggregateSpecsPlugin: Plugin = (builder) => {
id: "sum",
humanLabel: "sum",
HumanLabel: "Sum",
isSuitableType: isNumberLike,
isSuitableType: isNumberLike || isIntervalLike,
// I've wrapped it in `coalesce` so that it cannot be null
sqlAggregateWrap: (sqlFrag) =>
sql.fragment`coalesce(sum(${sqlFrag}), 0)`,
Expand Down Expand Up @@ -86,21 +88,21 @@ const AggregateSpecsPlugin: Plugin = (builder) => {
id: "min",
humanLabel: "minimum",
HumanLabel: "Minimum",
isSuitableType: isNumberLike,
isSuitableType: isNumberLike || isIntervalLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`min(${sqlFrag})`,
},
{
id: "max",
humanLabel: "maximum",
HumanLabel: "Maximum",
isSuitableType: isNumberLike,
isSuitableType: isNumberLike || isIntervalLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`max(${sqlFrag})`,
},
{
id: "average",
humanLabel: "mean average",
HumanLabel: "Mean average",
isSuitableType: isNumberLike,
isSuitableType: isNumberLike || isIntervalLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`avg(${sqlFrag})`,

// An AVG(...) ends up more precise than any individual value; see
Expand Down