whatyouhide/xandra

Inferring field type

Closed this issue · 4 comments

The title of this ticket is perhaps not the best.

What I'm suggesting is some mechanism to the Xandra.execute/3 call which the array of type/value tuples, could be, if an argument is not a tuple, inferred as to what type the field should be? For instance, if we assume that there's a table like:

CREATE TABLE app.requests (
    request_id text PRIMARY KEY,
    identifier text,
    type text,
    date date
)

And we insert a new record like this:

stmt = "INSERT INTO app.requests (request_id, identifier, type, date) VALUES(?, ?, ?, ?);"
date = Kernel.trunc(DateTime.to_unix(DateTime.utc_now()) / 86400) + Kernel.trunc(:math(2, 31))
Xandra.execute(conn, stmt, [{"text", req_id}, "foo", "thing", {"date", date}])

Is it not possible to infer that "foo" and "thing" are in fact, binaries, and therefore text?

There are 2 reliable ways to get inferring:

  1. use prepared queries (available today)
  2. build type server (we consider adding it after automatic node discovery)

@jeremytregunna I'm wondering if, for some reason, you don't use prepared queries?

@lexmag We do a lot of one-offs, so prepared queries don't buy us much. That said, this is good to know, I can give it a go.

@jeremytregunna of course it depend on the exact statements you use, but using prepared queries even for one-offs should be fine: https://hexdocs.pm/xandra/Xandra.html#prepare/3-prepared-queries-cache. :bowtie:

@lexmag This works just fine for our use cases, considering this closed then. Feel free to reopen, but I'm satisfied.