Skip to content

Commit

Permalink
integer parameter is implicitly converted to double
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexR2D2 committed Apr 2, 2023
1 parent ba8b7f1 commit b2c05bd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions c_src/term_to_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ bool nif::term_to_float(ErlNifEnv* env, ERL_NIF_TERM term, duckdb::Value& sink)
sink = move(duckdb::Value::FLOAT(a_double));
return true;
}
ErlNifSInt64 an_int64;
if (enif_get_int64(env, term, &an_int64)) {
sink = move(duckdb::Value::DOUBLE(an_int64));
return true;
}
return true;
}

Expand All @@ -31,6 +36,11 @@ bool nif::term_to_double(ErlNifEnv* env, ERL_NIF_TERM term, duckdb::Value& sink)
sink = move(duckdb::Value::DOUBLE(a_double));
return true;
}
ErlNifSInt64 an_int64;
if (enif_get_int64(env, term, &an_int64)) {
sink = move(duckdb::Value::DOUBLE(an_int64));
return true;
}
return true;
}

Expand Down
18 changes: 18 additions & 0 deletions duckdbex_sandbox.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,21 @@ Duckdbex.query(

File.read!("test_2.csv")
```

## Extensions

DuckDB has a number of extensions available for use. Not all of them are included by default in every distribution, but DuckDB has a mechanism that allows for remote installation.

<!-- livebook:{"break_markdown":true} -->

So, let's try the parquet extension. At first we must install it:

```elixir
{:ok, _} = Duckdbex.query(conn, "INSTALL 'parquet';")
```

Extension will be downloaded to the local host. Next, we must load it into our app:

```elixir
{:ok, _} = Duckdbex.query(conn, "LOAD 'parquet';")
```
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Duckdbex.MixProject do
use Mix.Project

@version "0.2.2"
@version "0.2.3"
@duckdb_version "0.7.1"

def project do
Expand Down
17 changes: 17 additions & 0 deletions test/duckdbex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,21 @@ defmodule DuckdbexTest do
assert {:ok, db} = Duckdbex.open()
assert is_integer(Duckdbex.number_of_threads(db))
end

test "when integer parameter is implicitly converted to double" do
assert {:ok, db} = Duckdbex.open()
assert {:ok, conn} = Duckdbex.connection(db)

assert {:ok, r} = Duckdbex.query(conn, "SELECT 1 WHERE 3434.2323/1000 < $1;", [10])

assert [[1]] = Duckdbex.fetch_all(r)
end

test "when double parameter is implicitly converted to integer" do
assert {:ok, db} = Duckdbex.open()
assert {:ok, conn} = Duckdbex.connection(db)

assert {:error, "invalid type of parameter #0"} =
Duckdbex.query(conn, "SELECT 1 WHERE 10 <= $1;", [10.0])
end
end

0 comments on commit b2c05bd

Please sign in to comment.