heathen

heathen

Ecto.Adapters.SQL.query behavior and result interpretation

Hi again!

Since I haven’t got a solution with building my queries from dynamic request maps, I’ve decided to try to build a string with query and a list with binding params and use Ecto.Adapters.SQL.query to do DB requests.

But here I got another rock :slight_smile:

Here is a number of subsequent queries with count():

iex(12)> Ecto.Adapters.SQL.query(Repo, "select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486, 730000])", [])
[debug] QUERY OK db=2.9ms
select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486, 730000]) []
{:ok,
 %Postgrex.Result{columns: ["count"], command: :select, connection_id: 18519,
  num_rows: 1, rows: ['8']}}

iex(13)> Ecto.Adapters.SQL.query(Repo, "select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[730000])", [])        
[debug] QUERY OK db=2.5ms
select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[730000]) []
{:ok,
 %Postgrex.Result{columns: ["count"], command: :select, connection_id: 18517,
  num_rows: 1, rows: [[1]]}}

iex(14)> Ecto.Adapters.SQL.query(Repo, "select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486])", [])
[debug] QUERY OK db=3.0ms
select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486]) []
{:ok,
 %Postgrex.Result{columns: ["count"], command: :select, connection_id: 18520,
  num_rows: 1, rows: ['7']}}

iex(15)> Ecto.Adapters.SQL.query(Repo, "select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[000000])", [])
[debug] QUERY OK db=2.8ms
select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[000000]) []
{:ok,
 %Postgrex.Result{columns: ["count"], command: :select, connection_id: 18515,
  num_rows: 1, rows: [[0]]}}

The same queries (copied one-to-one via Ctrl+C\Ctrl+V) in the same order to the psql:

[ db ] # select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486, 730000]);
 count 
-------
    56
(1 row)
Time: 0,962 ms

[ db ] # select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[730000]);
 count 
-------
     1
(1 row)
Time: 0,798 ms

[ db ] # select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[733486]);
 count 
-------
    55
(1 row)
Time: 1,166 ms

[ db ] # select count(*) from nioktr as n LEFT OUTER JOIN nioktr_to_grnti as ng ON n.id = ng.nioktr_id LEFT OUTER JOIN spr_grnti as g ON g.id = ng.spr_grnti_id WHERE g.id = ANY(ARRAY[000000]);
 count 
-------
     0
(1 row)
Time: 1,130 ms

So I don’t understand how to interpret SQL.query results. Can somebody explain me, please?

Marked As Solved

michalmuskala

michalmuskala

You encountered the charlist issue, where lists of small integers are interpreted as erlang-style strings - charlists. That only affects the way they are printed, the data is still the same.

iex(1)> '8' == [56]
true

You can learn more on this by using the i/1 helper in iex on the data, i.e.

iex(4)> i '8'
Term
  '8'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to as
  "charlists" (more precisely, a charlist is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [56]
Reference modules
  List

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement