hauleth

hauleth

I am working on big update to my ecto_function library. It is inspired by Nx support for defn functions, and my thought was “well, why not, check if it will be possible to do something similar for DB queries”. It seems that it is more or less possible:

defq clamp(val) do
  cond do
    val < 0 -> 0
    val > 1 -> 1
    true -> val
  end
end

Which when used like:

from e in "entires",
  select: clamp(e.value)

Will be translated to:

SELECT
  CASE
  WHEN e.value < 0 THEN 0
  WHEN e.value > 1 THEN 1
  ELSE e.value
  END
FROM entries e

This may be useful when one would like to move more computation to the DB where sometimes it may make more sense (move your computation to your data instead of moving data to your computation).

Question is whether there will be will for such constructs and whether community will find something like that useful.

There is already working PoC on the GitHub

https://github.com/hauleth/ecto_function/pull/4

But I would live to get some more insights before I dig more into it.

Showing Posts 1 to 3

kelvinst

kelvinst

I really like all these libs that make devs lives easier. I have some concerns with this approach though:

  1. It’s not very explicit. I mean, having just the letter q by the end of def does not make it very clear that this is a macro that is going to magically turn your elixir code into an Ecto.Query
  2. Magic is normally hard to customize. I can see myself using your library for simple queries and falling back to Ecto’s default query builder for more complex stuff. But some people really like to push the limits of what you initially intended for the lib (specially people that do not have much experience with magic), so that can backfire to a lot of unexpected results.

Anyway, I liked your idea and I might even give it a try on one of my livestream side projects in the future (if it ever makes to a final version of your lib) :smiley:

hauleth

hauleth OP

Actually it doesn’t change it into Ecto.Query, it changes it into macro that produces fragment/1.

defq clamp(val) do
  cond do
    val < 0 -> 0
    val > 1 -> 1
    true -> val
  end
end

Will expand to:

defmacro clamp(val) do
  quote do
    fragment(
      "CASE WHEN ? THEN ? WHEN ? THEN ? ELSE ? END",
      unquote(val) < 0, 0,
      unquote(val) > 1, 1,
      unquote(val)
    )
  end
end

As shown above it is meant to be used within regular Ecto.Query queries, it will just provide a way to define more complex queries without manually writing fragments.

kelvinst

kelvinst

I see. My concern still applies though. By looking at the code, it only feels like that is going to run a condition and return a plain value, but instead, the code is magically turned into a fragment.

I’m not saying that no one should use it because of that though, just saying that one might want to be careful with the amount of logic put on these functions, as something might not work exactly as expected.

Also, have you ever checked dynamic/2? Not sure if it wouldn’t actually make sense to use that one instead of fragments.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews