Skip to content

Expand macros in window frames#4730

Merged
josevalim merged 1 commit into
elixir-ecto:masterfrom
dannote:allow-window-frame-macros
May 27, 2026
Merged

Expand macros in window frames#4730
josevalim merged 1 commit into
elixir-ecto:masterfrom
dannote:allow-window-frame-macros

Conversation

@dannote
Copy link
Copy Markdown
Contributor

@dannote dannote commented May 27, 2026

Allow :frame to expand macros before validating that the result is a fragment.

Today this works:

windows: [
  recent: [
    order_by: result.date,
    frame: fragment(
      "RANGE BETWEEN INTERVAL 10 DAYS PRECEDING AND INTERVAL 10 DAYS FOLLOWING EXCLUDE CURRENT ROW"
    )
  ]
]

but an equivalent helper is rejected before expansion:

defmacro range_between(start_boundary, end_boundary, opts \\ []) do
  exclude = Keyword.get(opts, :exclude, :no_others)

  quote do
    fragment(
      unquote("RANGE BETWEEN #{start_boundary} AND #{end_boundary} EXCLUDE #{exclude}")
    )
  end
end

windows: [
  recent: [
    order_by: result.date,
    frame:
      range_between(
        "INTERVAL 10 DAYS PRECEDING",
        "INTERVAL 10 DAYS FOLLOWING",
        exclude: "CURRENT ROW"
      )
  ]
]

I ran into this while working on QuackDB, a new DuckDB adapter for Elixir. DuckDB supports richer frame clauses such as ROWS, RANGE, GROUPS, and EXCLUDE, and QuackDB could expose reusable helpers for those clauses instead of requiring users to write frame fragments inline.

The validation stays the same: after expansion, the frame still has to be a fragment. Non-fragment expansions continue to raise the existing error.

@josevalim josevalim merged commit b4b8996 into elixir-ecto:master May 27, 2026
7 of 8 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants