Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Test

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

env:
# Keep in step with release.yml.
ELIXIR_VERSION: "1.19"
OTP_VERSION: "28"
FDB_VERSION: "7.3.69"
ERLFDB_COMPILE_API_VERSION: "730"
MIX_ENV: test

jobs:
test:
name: mix test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{ env.OTP_VERSION }}
elixir-version: ${{ env.ELIXIR_VERSION }}

# The client links the erlfdb NIF; the server package provides the
# fdbserver binary that EctoFoundationDB.Sandbox runs the tests against.
- name: Install FoundationDB
run: |
BASE="https://github.com/apple/foundationdb/releases/download/${FDB_VERSION}"
curl -fsSL -o fdb-clients.deb "$BASE/foundationdb-clients_${FDB_VERSION}-1_amd64.deb"
curl -fsSL -o fdb-server.deb "$BASE/foundationdb-server_${FDB_VERSION}-1_amd64.deb"
sudo dpkg -i fdb-clients.deb fdb-server.deb

- uses: actions/cache@v4
with:
path: |
deps
_build
key: test-${{ runner.os }}-otp${{ env.OTP_VERSION }}-elixir${{ env.ELIXIR_VERSION }}-fdb${{ env.FDB_VERSION }}-mix-${{ hashFiles('mix.lock') }}
restore-keys: test-${{ runner.os }}-otp${{ env.OTP_VERSION }}-elixir${{ env.ELIXIR_VERSION }}-fdb${{ env.FDB_VERSION }}-mix-

- run: mix deps.get --only test
- run: mix deps.compile
- run: mix compile
- run: mix test
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,75 @@ select col_a, col_b from tenant_id.table_name where index_col >= 'baz' and index
select col_a, col_b from tenant_id.table_name where index_col between 'baz' and 'zaz';
```

Since efsql doesn't have access to the Ecto schema, type checking is loosened. For example, a `naive_datetime` indexed column must be queried using its string representation.
Since efsql doesn't have access to the Ecto schema, type checking is loosened: a value must be written as the type the column stores. For datetime columns, use a [typed literal](#typed-literals).

### Filter by NULL

```sql
select col_a from tenant_id.table_name where col_b is null;
select col_a from tenant_id.table_name where col_b is not null;
```

A field that is `nil` or absent from the stored record is NULL. The
PostgreSQL shorthands `col_b isnull` and `col_b notnull` also work. As in SQL,
`col_b = null` would never be true, so efsql rejects it with a pointer to
`IS NULL`.

### Typed literals

A quoted literal is always a string. To compare against an Elixir term that
has no SQL literal, annotate the literal with a type using the PostgreSQL cast
operator `::`, or the standard `CAST(... AS ...)`:

```sql
-- atoms
select col_a from tenant_id.table_name where status = 'active'::atom;
select col_a from tenant_id.table_name where status = cast('active' as atom);
select col_a from tenant_id.table_name where status in ('active'::atom, 'pending'::atom);

-- module names are atoms too
select col_a from tenant_id.table_name where kind = 'Elixir.MyApp.Widget'::atom;

-- NaiveDateTime, for :naive_datetime / :naive_datetime_usec fields
select col_a from tenant_id.table_name where inserted_at >= '2024-03-01 12:00:00'::timestamp;
select col_a from tenant_id.table_name where inserted_at >= '2024-03-01'::timestamp;

-- DateTime, for :utc_datetime / :utc_datetime_usec fields
select col_a from tenant_id.table_name where seen_at >= '2024-03-01T12:00:00Z'::timestamptz;
select col_a from tenant_id.table_name where seen_at >= '2024-03-01T14:00:00+02:00'::timestamptz;

-- Date and Time, for :date and :time / :time_usec fields
select col_a from tenant_id.table_name where birthday = '2024-03-01'::date;
select col_a from tenant_id.table_name where opens_at < '09:30:00'::time;
```

| Type | Elixir term | Alias |
| --- | --- | --- |
| `atom` | `Atom` | |
| `timestamp` | `NaiveDateTime` | `naive_datetime` |
| `timestamptz` | `DateTime` (UTC) | `utc_datetime` |
| `date` | `Date` | |
| `time` | `Time` | |

Elixir has two datetime types and Ecto stores whichever the field declares,
so pick the literal type that matches the column: a `NaiveDateTime` never
equals a `DateTime`, and index lookups compare the exact encoding. A
`timestamp` literal with a time zone offset is rejected rather than having the
offset silently dropped; a `timestamptz` literal without one is taken as UTC.
A bare date means midnight. Fractional seconds are optional, and values
compare equal regardless of precision.

Numbers need no type: a numeric literal compares by value against a
`:decimal` field, so `where price < 100` and `where price = 0.1` work. This
holds for filtering and sorting, but not for an index on a decimal field,
which the adapter keys by the `Decimal`'s term encoding rather than its value.

Ecto stores `Ecto.Enum` fields as strings, so query those with a plain string
literal, not an atom.

`BETWEEN` and `LIKE` don't accept typed literals; write a range as
`inserted_at >= '2024-01-01'::timestamp and inserted_at < '2025-01-01'::timestamp`
instead.

### Limit

Expand Down
9 changes: 2 additions & 7 deletions lib/efsql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ defmodule Efsql do

import Ecto.Query

def lex_and_parse(sql) do
{:ok, context, tokens} = SQL.Lexer.lex(sql)
SQL.Parser.parse(tokens, context)
end
def lex_and_parse(sql), do: Efsql.Parser.parse(sql)

def hello() do
tenant = EctoFoundationDB.Tenant.open!(Efsql.Repo, "localhost")
Expand Down Expand Up @@ -48,9 +45,7 @@ defmodule Efsql do
end

def sql_to_logical(sql, tenants \\ %{}) do
{:ok, context, tokens} = SQL.Lexer.lex(sql)
{:ok, _context, parsed} = SQL.Parser.parse(tokens, context)
logical = %Efsql.Logical.Select{} = Efsql.Parser.to_logical(parsed)
logical = %Efsql.Logical.Select{} = Efsql.Parser.sql_to_logical(sql)
resolve_tenant(logical, tenants)
end

Expand Down
13 changes: 11 additions & 2 deletions lib/efsql/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Efsql.Complete do

@statement_start ~w(select)
@post_expr ~w(and order limit)
@operators ~w(= > >= < <= like in between not)
@operators ~w(= > >= < <= like in between not is)

def complete(input, context) do
{word_start, word} = current_word(input)
Expand All @@ -33,8 +33,17 @@ defmodule Efsql.Complete do

defp candidates_for([], _context, _tokens), do: @statement_start

defp candidates_for([last | _], context, tokens) do
defp candidates_for([last | earlier], context, tokens) do
cond do
last == "is" ->
~w(null not)

last == "not" and List.first(earlier) == "is" ->
~w(null)

last == "null" ->
@post_expr

last == "select" or last == "," ->
fields(context, tokens) ++ ["*"]

Expand Down
38 changes: 30 additions & 8 deletions lib/efsql/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ defmodule Efsql.Executor do
then folds the operator pipeline over them.

SQL NULL semantics: a comparison, LIKE, or IN against a NULL (nil) field is
false — including NOT LIKE. Sorting places NULLs last ascending and first
descending, matching PostgreSQL's defaults.
false — including NOT LIKE. IS NULL matches a nil or absent field. Sorting places NULLs last ascending and first
descending, matching PostgreSQL's defaults. Values compare with
`Efsql.Types.compare/2`, so datetimes order chronologically.
"""

alias Efsql.Physical.Plan
alias Efsql.Types

@cmp_ops ~w[== > >= < <=]a
@cmp_results %{
==: [:eq],
>: [:gt],
>=: [:gt, :eq],
<: [:lt],
<=: [:lt, :eq]
}

def run(%Plan{access: access, ops: ops}) do
Enum.reduce(ops, fetch(access), &apply_op/2)
Expand Down Expand Up @@ -72,7 +81,7 @@ defmodule Efsql.Executor do
defp eval({:cmp, op, field, param}, row) when op in @cmp_ops do
case Map.get(row, field) do
nil -> false
value -> apply(Kernel, op, [value, param])
value -> Types.compare(value, param) in Map.fetch!(@cmp_results, op)
end
end

Expand All @@ -83,10 +92,13 @@ defmodule Efsql.Executor do
defp eval({:in, field, values}, row) do
case Map.get(row, field) do
nil -> false
value -> value in values
value -> Enum.any?(values, &(Types.compare(value, &1) == :eq))
end
end

defp eval({:is_null, field}, row), do: Map.get(row, field) == nil
defp eval({:not_null, field}, row), do: Map.get(row, field) != nil

defp eval({:like, field, pattern}, row) do
case Map.get(row, field) do
nil -> false
Expand Down Expand Up @@ -117,10 +129,20 @@ defmodule Efsql.Executor do

defp compare(a, b, [{dir, field} | rest]) do
case {Map.get(a, field), Map.get(b, field)} do
{v, v} -> compare(a, b, rest)
{nil, _} -> if dir == :asc, do: :gt, else: :lt
{_, nil} -> if dir == :asc, do: :lt, else: :gt
{va, vb} -> order(if(va < vb, do: :lt, else: :gt), dir)
{v, v} ->
compare(a, b, rest)

{nil, _} ->
if dir == :asc, do: :gt, else: :lt

{_, nil} ->
if dir == :asc, do: :lt, else: :gt

{va, vb} ->
case Types.compare(va, vb) do
:eq -> compare(a, b, rest)
cmp -> order(cmp, dir)
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/efsql/logical.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Efsql.Logical do
bound, `lower_op` in `:> :>=`, `upper_op` in `:< :<=`
* `{:like, field, pattern}` / `{:not_like, field, pattern}`
* `{:in, field, values}`
* `{:is_null, field}` / `{:not_null, field}`

The primary key is the pseudo-field `:_`.

Expand All @@ -36,4 +37,6 @@ defmodule Efsql.Logical do
def predicate_field({:like, field, _pattern}), do: field
def predicate_field({:not_like, field, _pattern}), do: field
def predicate_field({:in, field, _values}), do: field
def predicate_field({:is_null, field}), do: field
def predicate_field({:not_null, field}), do: field
end
Loading
Loading