Skip to content

Add Postgres GENERATE_SERIES function #397

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

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions postgres/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ func DATE_TRUNC(field unit, source Expression, timezone ...string) TimestampExpr
return jet.NewTimestampFunc("DATE_TRUNC", jet.FixedLiteral(unitToString(field)), source)
}

// GENERATE_SERIES generates a series of values from start to stop, with a step size of step.
func GENERATE_SERIES(start Expression, stop Expression, step ...Expression) Expression {
if len(step) > 0 {
return jet.NewFunc("GENERATE_SERIES", []Expression{start, stop, step[0]}, nil)
}

return jet.NewFunc("GENERATE_SERIES", []Expression{start, stop}, nil)
}

// --------------- Conditional Expressions Functions -------------//

// COALESCE function returns the first of its arguments that is not null.
Expand Down
13 changes: 13 additions & 0 deletions postgres/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ func TestDATE_TRUNC(t *testing.T) {
"DATE_TRUNC('DAY', NOW() + INTERVAL '1 HOUR', 'Australia/Sydney')",
)
}

func TestGENERATE_SERIES(t *testing.T) {
assertSerialize(
t,
GENERATE_SERIES(NOW(), NOW().ADD(INTERVAL(10, DAY))),
"GENERATE_SERIES(NOW(), NOW() + INTERVAL '10 DAY')",
)
assertSerialize(
t,
GENERATE_SERIES(NOW(), NOW().ADD(INTERVAL(10, DAY)), INTERVAL(2, DAY)),
"GENERATE_SERIES(NOW(), NOW() + INTERVAL '10 DAY', INTERVAL '2 DAY')",
)
}
Loading