improvement: support the Duration form of ago, from_now, datetime_add… - #245
Merged
zachdaniel merged 1 commit intoAug 14, 2026
Merged
Conversation
… and date_add
`Ash.Query.Function.Ago`, `FromNow`, `DateTimeAdd` and `DateAdd` each accept a
`Duration` as well as an integer/interval-name pair, and evaluate it in Elixir, but
only the interval form was rendered here. The Duration form reached
`default_dynamic_expr/6`, matched nothing, and raised `Unsupported expression`, so it
worked on data layers that evaluate expressions at runtime and failed on every
SQL-backed one.
Adds a clause per function, rendering the duration as an interval parameter. Ecto has
a native `:duration` type and Postgres accepts a `%Duration{}` directly, so no
interval-name string building is needed, and multi-unit durations work.
The datetime operand is cast, as Ecto's own `datetime_add/3` does with
`type_unless_typed`; without it Postgres resolves `? - ?::interval` as interval
arithmetic. `date_add` casts back to `::date` for the same reason Ecto's does.
`from_now/1` is uncallable until the corresponding ash change releases, so its clause
is untestable downstream until then.
This was referenced Aug 14, 2026
Contributor
|
🚀 Thank you for your contribution! 🚀 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Tests will be contributed separately in ash_postgres as PR, will need this to release before it merges.
Summary
Partially delivers ash-project/ash_postgres#553 (Duration)
Requires ash PR ash-project/ash#2844 (for
from_now/1)Enables an ash_postgres test PR
Ash.Query.Function.Ago,FromNow,DateTimeAddandDateAddeach accept aDurationas well as an integer/interval-name pair, and evaluate it in Elixir. The Duration form is handled in ash for data layers that evaluate expressions using Ash Runtime, but wasn't handled by ash_sql raisingUnsupported expressionfor SQL backed data layers.This adds a clause per function, passing the duration as an interval parameter. Ecto
has a native
:durationtype and Postgres accepts a%Duration{}directly, so thereis no interval-name string building and multi-unit durations work.
Verified against PostgreSQL 19 with all four functions in both forms, including a
multi-unit and a negative duration.