al2o3cr

al2o3cr

Typecasting tuple results with Ecto

Related but different from Ecto IN clauses with tuples

Is there any way to specify a type for an expression that returns what Postgrex describes as “Anonymous composite types” and decodes to tuples?

This arose in an Ecto fragment:

fragment("array_agg((?, ?))", o.id, o.inserted_at)

which produces an array of {integer, datetime} tuples. The inserted_at values are returned as NaiveDateTime because Postgrex decodes them that way.

Trying to fix that with Ecto.Query.API.type/2 fails:

# Aspirational API - does not work like this
type(fragment("array_agg((?, ?))", o.id, o.inserted_at), {:array, {:integer, :utc_datetime_usec}})

with this error message:

(ArgumentError) unsupported type `{:integer, :utc_datetime_usec}`. The type can either be an atom, a string or a tuple of the form `{:map, t}` or `{:array, t}` where `t` itself follows the same conditions.

and stacktrace:

(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:1286: Ecto.Adapters.Postgres.Connection.ecto_to_db/1
(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:723: Ecto.Adapters.Postgres.Connection.tagged_to_db/1
(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:698: Ecto.Adapters.Postgres.Connection.expr/3
(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:1238: Ecto.Adapters.Postgres.Connection.intersperse_map/4
(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:319: Ecto.Adapters.Postgres.Connection.select/3
(ecto_sql 3.5.3) lib/ecto/adapters/postgres/connection.ex:113: Ecto.Adapters.Postgres.Connection.all/2
(ecto_sql 3.5.3) lib/ecto/adapters/postgres.ex:102: Ecto.Adapters.Postgres.prepare/2
(ecto 3.5.5) lib/ecto/query/planner.ex:180: Ecto.Query.Planner.query_without_cache/4
(ecto 3.5.5) lib/ecto/query/planner.ex:150: Ecto.Query.Planner.query_prepare/6
(ecto 3.5.5) lib/ecto/query/planner.ex:125: Ecto.Query.Planner.query_with_cache/7
(ecto 3.5.5) lib/ecto/repo/queryable.ex:213: Ecto.Repo.Queryable.execute/4
(ecto 3.5.5) lib/ecto/repo/queryable.ex:17: Ecto.Repo.Queryable.all/3
...

Support for these kinds of data has been added in limited cases (for IN clauses with literals) but I couldn’t find any discussion of typecasting. Has this come up before?

If this feature did exist, what would the syntax look like? A bare tuple of types looks nice, but {:array, {:tuple, [:integer, :utc_datetime_usec]}} would be more in harmony with the other keywords (:array and :map ) and avoid wrangling variable-size tuples. It would match the format already used in the planner, though there is this note about “not wanting to allow fields of this type”.

Trying that syntax in Ecto fails to even compile:

# Aspirational API - does not work like this
type(fragment("array_agg((?, ?))", o.id, o.inserted_at), {:array, {:tuple, [:integer, :utc_datetime_usec]}})

# gets the error
** (Ecto.Query.CompileError) type/2 expects an alias, atom or source.field as second argument, got: `[:integer, :utc_datetime_usec]`
    (ecto 3.5.5) expanding macro: Ecto.Query.select/3
    iex:14: (file)
    (ecto 3.5.5) expanding macro: Ecto.Query.where/3
    iex:14: (file)
    (ecto 3.5.5) expanding macro: Ecto.Query.group_by/3
    iex:14: (file)
    (ecto 3.5.5) expanding macro: Ecto.Query.limit/3
    iex:14: (file)

First Post!

LostKobrakai

LostKobrakai

Does this help?

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement