vlad.grb

vlad.grb

Trying to wrap existing Date to support infinity special value

Ecto: 2.2.9

Implemented this

defmodule Utils.Ecto.Types.Date do
  @behaviour Ecto.Type

  @doc false
  def type, do: :date

  @doc false
  def cast("infinity"), do: {:ok, :"infinity"}
  def cast("-infinity"), do: {:ok, :"-infinity"}
  def cast(value), do: Ecto.Type.cast(:date, value)

  @doc false
  def load("infinity"), do: {:ok, :infinity}
  def load("-infinity"), do: {:ok, :"-infinity"}
  def load(value), do: Ecto.Type.load(:date, value, nil)

  @doc false
  def dump(:infinity), do: {:ok, "infinity"}
  def dump(:"-infinity"), do: {:ok, "-infinity"}
  def dump(value), do: Ecto.Type.dump(:date, value, nil)
end

When trying to insert a record getting

  1) test support infinite values (FooBar.Hello)
     test/foo_bar/hello_test.exs:63
     ** (CaseClauseError) no case clause matching: {"-infinity"}
     stacktrace:
       (foo_bar) lib/ecto/types/date.ex:715: FooBar.PostgresTypes.encode_params/3
       (postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
       (db_connection) lib/db_connection.ex:1079: DBConnection.describe_run/5
       (db_connection) lib/db_connection.ex:1150: anonymous fn/4 in DBConnection.run_meter/5
       (db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
       (ecto) lib/ecto/adapters/postgres/connection.ex:86: Ecto.Adapters.Postgres.Connection.execute/4
       (ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
       (ecto) lib/ecto/adapters/sql.ex:542: Ecto.Adapters.SQL.struct/8
       (ecto) lib/ecto/repo/schema.ex:547: Ecto.Repo.Schema.apply/4
       (ecto) lib/ecto/repo/schema.ex:213: anonymous fn/14 in Ecto.Repo.Schema.do_insert/4
       (ecto) lib/ecto/association.ex:556: Ecto.Association.Has.on_repo_change/4
       (ecto) lib/ecto/association.ex:338: anonymous fn/7 in Ecto.Association.on_repo_change/6
       (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto) lib/ecto/association.ex:335: Ecto.Association.on_repo_change/6
       (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto) lib/ecto/association.ex:301: Ecto.Association.on_repo_change/3
       (ecto) lib/ecto/repo/schema.ex:708: Ecto.Repo.Schema.process_children/4
       (ecto) lib/ecto/repo/schema.ex:774: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
       (db_connection) lib/db_connection.ex:1374: DBConnection.transaction_nested/2
       (db_connection) lib/db_connection.ex:1234: DBConnection.transaction_meter/3

Tried to read the source but it is too much metacode inside which I can’t understand.

Showing Posts 1 to 10

1player

1player

Looks like postgrex isn’t able to encode an "infinity" date string, though I might be mistaken. I don’t see any mention of it in postgrex/lib/postgrex/extensions/date.ex at master · elixir-ecto/postgrex · GitHub

But there seems to be some confusion with your usage of atoms and strings.

"infinity" is not equal to :"infinity", and your code seems to use atoms or strings interchangeably.

EDIT: Nope, postgrex doesn’t support infinity dates at the moment: postgrex can't handle all possible postgres date/timestamp/timestamptz values · Issue #274 · elixir-ecto/postgrex · GitHub

vlad.grb

vlad.grb OP

This is very bad :disappointed_relieved:

vlad.grb

vlad.grb OP

I tried to add missing match for extension

defmodule Utils.Ecto.Adapters.Postgres.Date do
  @moduledoc false
  import Postgrex.BinaryUtils, warn: false
  use Postgrex.BinaryExtension, send: "date_send"

  @gd_epoch :calendar.date_to_gregorian_days({2000, 1, 1})
  @max_year 5874897

  def init(opts), do: opts

  def encode(_) do
    quote location: :keep do
      {year, month, day} when year <= unquote(@max_year) ->
        date = {year, month, day}
        <<4 :: int32, :calendar.date_to_gregorian_days(date) - unquote(@gd_epoch) :: int32>>
      "-infinity" ->
        IO.puts "AWWWH CHUBAKA SAYS: #{x}"
        <<4 :: int32, :calendar.date_to_gregorian_days({2019, 3, 8}) - unquote(@gd_epoch) :: int32>>
      "infinity" ->
        IO.puts "AWWWH CHUBAKA SAYS: #{x}"
        <<4 :: int32, :calendar.date_to_gregorian_days({2019, 3, 8}) - unquote(@gd_epoch) :: int32>>
    end
  end

  def decode(_) do
    quote location: :keep do
      <<4 :: int32, days :: int32>> ->
        :calendar.gregorian_days_to_date(days + unquote(@gd_epoch))
      "-infinity" ->
        IO.puts "YOU SHALL NOT PASS: #{x}"
        :calendar.gregorian_days_to_date(0 + unquote(@gd_epoch))
      "infinity" ->
        IO.puts "YOU SHALL NOT PASS: #{x}"
        :calendar.gregorian_days_to_date(0 + unquote(@gd_epoch))
    end
  end
end

On insert I put dummy values to check what is the output from db.

CREATE TABLE sandbox(
    id serial primary key,
    date date
);

INSERT INTO sandbox (date) VALUES ('infinity');

Then

defmodule Sand do
  use Ecto.Schema

  schema "sandbox" do
    field :date, Scene.Utils.Ecto.Types.Date
  end
end

And then

s = Repo.one(Sand)
%Sand{
  __meta__: #Ecto.Schema.Metadata<:loaded, "sandbox">,
  date: %Inspect.Error{
    message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Date, calendar: Calendar.ISO, day: 11, month: 7, year: 5881610}"
  },
  id: 1
}

But

iex(8)> %{calendar: Calendar.ISO, year: year, month: month, day: day} = s.date
%Inspect.Error{
  message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: Date, calendar: Calendar.ISO, day: 11, month: 7, year: 5881610}"
}
iex(9)> year
5881610

And the doc says that the max date is 5874897 AD :thinking:

Qqwy

Qqwy

TypeCheck Core Team

I do wonder: What is your intended use for this?

vlad.grb

vlad.grb OP

Hmm. Unbounded dates? :slight_smile:

For example:

SELECT * FROM xxx WHERE dateRange @> someDate;
mgwidmann

mgwidmann

If I’m not mistaken, this won’t work as the database DATE type cannot be stored as the string "infinity". The column would have to be a varchar and then you would have to perform date parsing in the where clause (and indexing by it would be poor as well). The best thing I can recommend is when the date is exactly equal to the highest and lowest values in the datetime, those are markers for infinity and negative infinity (looks like 4713 BC and 294276 AD for postgres). Try making your custom type use those values instead.

stefanluptak

stefanluptak

What about using NULL for the purpose of infinity? Wouldn’t that be a solution? Then using something like SELECT * FROM xxx WHERE dateRange @> someDate OR someDate IS NULL;.

jwarlander

jwarlander

I can’t speak for your particular domain of course, but in my world I generally tend to use 9999-12-31 as the “infinity” date.. I imagine if I had to decide on “-infinity”, I’d go for 0001-01-01.

Though that’s with the reservation that I generally just have to deal with event timestamps over the lifetime of the company where I work.

Thus.. you could potentially have your extension translate :infinity to 9999-12-31, etc.

vlad.grb

vlad.grb OP

NULL is not less or greater than now() it just gives you nothing. So it won’t work.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@vlad.grb This is why he uses OR someDate IS NULL

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews