From 5317afe3f3a63181203a1551e2d74218a73bc8be Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 27 Sep 2026 13:32:57 +0000 Subject: [PATCH] Support OR in WHERE An OR becomes one predicate, {:or, branches}, each branch an AND-ed list of predicates, so AND binds tighter and nesting is kept. It is checked on the rows read, with SQL NULL semantics per branch, while the conditions AND-ed with it still narrow the read. NOT over AND now works too, as the OR it means. An OR of equalities and INs on one field is rewritten to an IN, so an index or the primary key serves it as one lookup per value. IN lists are now deduplicated, since each value is read on its own and a repeated one returned its rows twice. Predicate.fields/1 gives every field a predicate reads, for the planner's residual columns, the primary-key check and _tenant. In a query across tenants an OR on _tenant alone picks the tenants; mixing _tenant with other fields is refused, as each tenant's rows are filtered before they get _tenant. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko --- README.md | 27 +++++++++++-- lib/efsql/complete.ex | 2 +- lib/efsql/fanout.ex | 9 ++++- lib/efsql/parser.ex | 20 +++++++--- lib/efsql/planner.ex | 7 ++-- lib/efsql/predicate.ex | 22 +++++++++-- lib/efsql/rewrite.ex | 44 ++++++++++++++++++--- lib/efsql/tui/help.ex | 4 +- test/complete_test.exs | 1 + test/cross_tenant_test.exs | 17 ++++++++ test/or_test.exs | 79 ++++++++++++++++++++++++++++++++++++++ test/parser_test.exs | 33 ++++++++++++++-- test/predicate_test.exs | 30 ++++++++++++++- test/rewrite_test.exs | 31 +++++++++++++++ test/tui_app_test.exs | 2 +- 15 files changed, 298 insertions(+), 30 deletions(-) create mode 100644 test/or_test.exs diff --git a/README.md b/README.md index acb1aa1..da10d62 100644 --- a/README.md +++ b/README.md @@ -170,8 +170,7 @@ its case (`"CreatedAt"`). Most SQL keywords work as bare names, so columns like so if you forget. Comments (`-- ...` and `/* ... */`) can go anywhere. A syntax error reports the -line and column it was found at, and features efsql doesn't support (`OR`, -joins, `HAVING`, functions other than aggregates) are rejected by name. +line and column it was found at, and features efsql doesn't support (joins, `HAVING`, functions other than aggregates) are rejected by name. ### Storage IDs @@ -278,6 +277,26 @@ select col_a, col_b from tenant_id.table_name where index_col between 'baz' and 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). +### OR + +```sql +select col_a from tenant_id.table_name where status = 'paid' or total > 100; +select col_a from tenant_id.table_name where city = 'Osaka' and (age < 20 or age > 60); +select col_a from tenant_id.table_name where status = 'paid' or status = 'shipped'; +``` + +`AND` binds tighter than `OR`, so `a and b or c` is `(a and b) or c`. An +`OR` is checked on the rows read, while the conditions `AND`-ed with it +still narrow what is read: in the second query, an index on `city` serves +`city = 'Osaka'`. `OR`-ed equalities on one field are an `IN` +(`status in ('paid', 'shipped')`), read as one lookup per value when an +index or the primary key serves the field. As in SQL, a NULL field fails +only its own branch: a row whose `status` is NULL still matches +`status = 'paid' or total > 100` when its total is over 100. + +In a query across tenants, an `OR` on `_tenant` alone picks the tenants, +but one can't mix `_tenant` with other fields. + ### Negation and ILIKE ```sql @@ -290,8 +309,8 @@ select col_a from tenant_id.table_name where not (status = 'paid' or total > 100 ``` `NOT` works on any condition, and is read as its opposite: `not (a < 1)` is -`a >= 1`, `not (a = 1 or b = 2)` is `a <> 1 and b <> 2`. `not (a and b)` -would need `OR`, which isn't supported. +`a >= 1`, `not (a = 1 or b = 2)` is `a <> 1 and b <> 2`, and `not (a and b)` +is `not a or not b`. As in SQL, a NULL field matches no comparison, negated ones included: a row whose `status` is NULL matches neither `status = 'paid'` nor diff --git a/lib/efsql/complete.ex b/lib/efsql/complete.ex index 14a12c1..4ba3525 100644 --- a/lib/efsql/complete.ex +++ b/lib/efsql/complete.ex @@ -13,7 +13,7 @@ defmodule Efsql.Complete do """ @statement_start ~w(select) - @post_expr ~w(and group order limit) + @post_expr ~w(and or group order limit) @operators ~w(= <> != > >= < <= like ilike in between not is) def complete(input, context) do diff --git a/lib/efsql/fanout.ex b/lib/efsql/fanout.ex index 01bbc4d..5628834 100644 --- a/lib/efsql/fanout.ex +++ b/lib/efsql/fanout.ex @@ -70,7 +70,14 @@ defmodule Efsql.Fanout do end {tenant_preds, row_preds} = - Enum.split_with(logical.predicates, &(Predicate.field(&1) == :_tenant)) + Enum.split_with(logical.predicates, &(Predicate.fields(&1) == [:_tenant])) + + # Each tenant's own reads check row_preds before its rows get _tenant. + for pred <- row_preds, :_tenant in Predicate.fields(pred) do + raise Unsupported, + "an OR can't mix _tenant with other fields; " <> + "put the condition on _tenant on its own (where _tenant = 'a' and (x = 1 or y = 2))" + end names = storage_id diff --git a/lib/efsql/parser.ex b/lib/efsql/parser.ex index ec45301..9298e3a 100644 --- a/lib/efsql/parser.ex +++ b/lib/efsql/parser.ex @@ -2,7 +2,7 @@ defmodule Efsql.Parser do @moduledoc """ SQL text to `Efsql.Logical.Select`: parses with `Efsql.SQL.Parser`, then translates the syntax tree. Syntax problems raise `Efsql.SQL.SyntaxError`; - valid SQL that efsql can't execute (`OR`, comparing two columns, ...) + valid SQL that efsql can't execute (comparing two columns, ...) raises `Efsql.Exception.Unsupported`. Purely a translation — normalization (range merging, LIKE rewriting) @@ -53,7 +53,7 @@ defmodule Efsql.Parser do defp check_tenant_field(logical) do used = if(is_list(logical.projection), do: logical.projection, else: []) ++ - Enum.map(logical.predicates, &Efsql.Predicate.field/1) ++ + Enum.flat_map(logical.predicates, &Efsql.Predicate.fields/1) ++ Enum.map(logical.order, &elem(&1, 1)) ++ (logical.group_by || []) ++ Enum.map(logical.aggregates, &elem(&1, 2)) @@ -182,8 +182,7 @@ defmodule Efsql.Parser do # NOT is pushed down to the conditions it negates, each of which has an # exact opposite: NOT (a = 1) is a <> 1, NOT (a IN ...) is a NOT IN ..., - # and NOT (a OR b) is NOT a AND NOT b. NOT (a AND b) is NOT a OR NOT b, - # which needs OR. + # NOT (a OR b) is NOT a AND NOT b, and NOT (a AND b) is NOT a OR NOT b. defp push_not({:not, expr}), do: negate(expr) defp push_not({op, left, right}) when op in [:and, :or], @@ -208,6 +207,9 @@ defmodule Efsql.Parser do defp conjuncts({:and, left, right}, acc), do: conjuncts(left, conjuncts(right, acc)) defp conjuncts(expr, acc), do: [expr | acc] + defp disjuncts({:or, left, right}, acc), do: disjuncts(left, disjuncts(right, acc)) + defp disjuncts(expr, acc), do: [expr | acc] + defp predicate({:compare, op, {:column, _} = column, {:column, _}}), do: raise(Unsupported, "comparing two fields (#{describe(column)} #{op} ...) is not supported") @@ -255,8 +257,14 @@ defmodule Efsql.Parser do defp predicate({:is_null, {:column, field}, negated?}), do: {if(negated?, do: :not_null, else: :is_null), field(field)} - defp predicate({:or, _, _}), - do: raise(Unsupported, "OR is not supported (nor NOT over AND, which means OR)") + # `a OR b OR (c AND d)` is one OR of three branches, each an AND-ed list. + defp predicate({:or, _, _} = expr) do + branches = + for branch <- disjuncts(expr, []), + do: branch |> conjuncts([]) |> Enum.map(&predicate/1) + + {:or, branches} + end defp predicate({:not, expr}), do: raise(Unsupported, "#{describe(expr)} is not a condition; NOT needs a condition") diff --git a/lib/efsql/planner.ex b/lib/efsql/planner.ex index 32acf12..35c8e20 100644 --- a/lib/efsql/planner.ex +++ b/lib/efsql/planner.ex @@ -357,7 +357,7 @@ defmodule Efsql.Planner do defp takes(fields, residual, sort) do needed = - (Enum.map(residual, &Predicate.field/1) ++ Enum.map(sort, fn {_dir, f} -> f end)) + (Enum.flat_map(residual, &Predicate.fields/1) ++ Enum.map(sort, fn {_dir, f} -> f end)) |> Enum.uniq() case needed -- fields do @@ -367,11 +367,12 @@ defmodule Efsql.Planner do end defp ensure_residual_evaluable!(pred) do - if Predicate.field(pred) == @pk_field do + if @pk_field in Predicate.fields(pred) do # Rows carry the key under its field's name, not `_`, so a condition # on `_` must be one the key range can serve. raise Unsupported, - "on the primary key '_', only =, <, <=, >, >=, BETWEEN and IN are supported" + "on the primary key '_', only =, <, <=, >, >=, BETWEEN and IN are supported " <> + "(and an OR of = and IN); use the primary key field name for anything else" end end diff --git a/lib/efsql/predicate.ex b/lib/efsql/predicate.ex index f2c0751..9266abd 100644 --- a/lib/efsql/predicate.ex +++ b/lib/efsql/predicate.ex @@ -13,6 +13,9 @@ defmodule Efsql.Predicate do case-insensitive `:ilike` / `:not_ilike` * `{:in, field, values}` / `{:not_in, field, values}` * `{:is_null, field}` / `{:not_null, field}` + * `{:or, branches}` — true when any branch is, each branch itself an + AND-ed list of predicates (`a = 1 OR (b = 2 AND c = 3)` is + `{:or, [[a = 1], [b = 2, c = 3]]}`) SQL's `NOT` never reaches here: `Efsql.Parser` pushes it into the condition it negates (`NOT (a < 1)` is `a >= 1`). @@ -33,6 +36,7 @@ defmodule Efsql.Predicate do | {:like | :not_like | :ilike | :not_ilike, field(), String.t()} | {:in | :not_in, field(), [term()]} | {:is_null | :not_null, field()} + | {:or, [[t()]]} @pk_field :_ @@ -45,7 +49,7 @@ defmodule Efsql.Predicate do <=: [:lt, :eq] } - @doc "The field a predicate constrains." + @doc "The field a predicate constrains; see `fields/1` for an OR." @spec field(t()) :: field() def field({:cmp, _op, field, _value}), do: field def field({kind, field, _lower, _upper}) when kind in [:range, :not_range], do: field @@ -53,6 +57,13 @@ defmodule Efsql.Predicate do def field({kind, field, _values}) when kind in [:in, :not_in], do: field def field({kind, field}) when kind in [:is_null, :not_null], do: field + @doc "Every field a predicate reads, in order, without duplicates." + @spec fields(t()) :: [field()] + def fields({:or, branches}), + do: branches |> Enum.concat() |> Enum.flat_map(&fields/1) |> Enum.uniq() + + def fields(predicate), do: [field(predicate)] + # -- evaluation -- @doc """ @@ -77,6 +88,10 @@ defmodule Efsql.Predicate do defp eval({:in, field, values}, row), do: on_value(row, field, &member?(&1, values)) defp eval({:not_in, field, values}, row), do: on_value(row, field, &(not member?(&1, values))) + # A NULL makes a branch false, not the whole OR: `a = 1 OR b = 2` holds + # when a is NULL and b is 2. + defp eval({:or, branches}, row), do: Enum.any?(branches, &matches?(row, &1)) + defp eval({:is_null, field}, row), do: Map.get(row, field) == nil defp eval({:not_null, field}, row), do: Map.get(row, field) != nil @@ -120,9 +135,10 @@ defmodule Efsql.Predicate do * `:in` - an `IN` list, read as one lookup per value when an index or the key can serve them * `:index` - an equality or range an index may serve - * `:filter` - only checked on the rows read: LIKE, IS NULL, and + * `:filter` - only checked on the rows read: LIKE, IS NULL, OR, and every negation (`<>`, NOT IN, NOT BETWEEN, NOT LIKE), which an index - can't serve + can't serve (an OR of equalities on one field becomes an IN first; + see `Efsql.Rewrite`) """ @spec pushdown(t()) :: :key | :in | :index | :filter def pushdown({:cmp, :!=, _field, _value}), do: :filter diff --git a/lib/efsql/rewrite.ex b/lib/efsql/rewrite.ex index e098839..f7924fa 100644 --- a/lib/efsql/rewrite.ex +++ b/lib/efsql/rewrite.ex @@ -2,12 +2,13 @@ defmodule Efsql.Rewrite do @moduledoc """ Normalization passes over an `Efsql.Logical` query. Each pass is a pure `logical -> logical` function; `normalize/1` runs them in order. New - rewrite rules (OR to DNF, NOT pushdown, contradiction detection, ...) are - added here as further passes. + rewrite rules (contradiction detection, ...) are added here as further + passes. (NOT is pushed down earlier, by `Efsql.Parser`.) """ alias Efsql.Logical alias Efsql.Predicate + alias Efsql.Types @lower_ops ~w[> >=]a @upper_ops ~w[< <=]a @@ -16,6 +17,7 @@ defmodule Efsql.Rewrite do logical |> pass_merge_ranges() |> pass_like_prefix() + |> pass_or_in() |> pass_in_singleton() end @@ -123,13 +125,43 @@ defmodule Efsql.Rewrite do end end - # `field in (v)` is an equality, and `field not in (v)` an inequality. + # `a = 1 OR a = 2 OR a IN (3, 4)` is `a IN (1, 2, 3, 4)`, which an index + # or the primary key serves as one lookup per value; any other OR is + # only checked on the rows read. + def pass_or_in(%Logical.Select{predicates: preds} = logical) do + %Logical.Select{logical | predicates: Enum.map(preds, &or_in/1)} + end + + defp or_in({:or, branches} = pred) do + values = Enum.map(branches, &in_values/1) + + case Enum.uniq_by(values, &elem(&1, 0)) do + [{field, _}] when field != nil -> {:in, field, Enum.flat_map(values, &elem(&1, 1))} + _ -> pred + end + end + + defp or_in(pred), do: pred + + defp in_values([{:cmp, :==, field, value}]), do: {field, [value]} + defp in_values([{:in, field, values}]), do: {field, values} + defp in_values(_branch), do: {nil, []} + + # An IN list holds each value once, since each is read on its own (`a IN + # (1, 1)` would read a = 1 twice). Then `field in (v)` is an equality, + # and `field not in (v)` an inequality. def pass_in_singleton(%Logical.Select{predicates: preds} = logical) do predicates = Enum.map(preds, fn - {:in, field, [value]} -> {:cmp, :==, field, value} - {:not_in, field, [value]} -> {:cmp, :!=, field, value} - pred -> pred + {kind, field, values} when kind in [:in, :not_in] -> + case {kind, Enum.uniq_by(values, &Types.equality_key/1)} do + {:in, [value]} -> {:cmp, :==, field, value} + {:not_in, [value]} -> {:cmp, :!=, field, value} + {kind, values} -> {kind, field, values} + end + + pred -> + pred end) %Logical.Select{logical | predicates: predicates} diff --git a/lib/efsql/tui/help.ex b/lib/efsql/tui/help.ex index 4c1583e..fb51fe5 100644 --- a/lib/efsql/tui/help.ex +++ b/lib/efsql/tui/help.ex @@ -61,8 +61,10 @@ defmodule Efsql.Tui.Help do {:sql, "where status <> 'paid' also !=, not in, not between"}, {:sql, "where not (a = 1 or b = 2)"}, {:sql, "where city = 'Osaka' and age > 40"}, + {:sql, "where city = 'Osaka' or (city = 'Kyoto' and age > 40)"}, "Literals: 'text', 42, 1.5, true, false, null.", - {:note, "or is not supported; conditions combine with and only"}, + {:note, "and binds tighter than or; an or is checked on the rows read,"}, + {:note, "except or-ed equalities on one field, read like in (...)"}, {:note, "a null field matches no comparison, like in SQL"} ]}, {"Ordering and limits", diff --git a/test/complete_test.exs b/test/complete_test.exs index 6d6c64b..52a2a86 100644 --- a/test/complete_test.exs +++ b/test/complete_test.exs @@ -55,6 +55,7 @@ defmodule Efsql.CompleteTest do {_, candidates} = Complete.complete("select id from users where name is null ", @context) assert "and" in candidates + assert "or" in candidates end test "order by offers fields" do diff --git a/test/cross_tenant_test.exs b/test/cross_tenant_test.exs index a45a3e9..cbc9379 100644 --- a/test/cross_tenant_test.exs +++ b/test/cross_tenant_test.exs @@ -94,6 +94,23 @@ defmodule EfsqlTest.Integration.CrossTenant do assert rows == [%{_tenant: context[:other], name: "Dora"}] end + test "an OR on _tenant chooses the tenants; mixed with other fields it is refused", + context do + either = "_tenant = '#{context[:tenant_id]}' or _tenant = '#{context[:other]}'" + + assert {%Plan{access: {:fan_out, [_, _]}}, rows, _} = + Efsql.qall("select name from *.users where #{either} order by name;") + + assert Enum.map(rows, & &1.name) == ["Alice", "Bob", "Charles", "Dora", "Eve"] + + assert_raise Unsupported, ~r/an OR can't mix _tenant with other fields/, fn -> + Efsql.all( + "select name from *.users where #{context[:both]} and " <> + "(_tenant = '#{context[:other]}' or name = 'Alice');" + ) + end + end + test "more tenants than the limit is refused", context do assert_raise Unsupported, ~r/would read 2 tenants, more than the limit of 1/, fn -> Efsql.all("select name from *.users where #{context[:both]};", max_tenants: 1) diff --git a/test/or_test.exs b/test/or_test.exs new file mode 100644 index 0000000..0d761d4 --- /dev/null +++ b/test/or_test.exs @@ -0,0 +1,79 @@ +defmodule EfsqlTest.Integration.Or do + use EfsqlTest.Case, async: true + + alias Efsql.Exception.Unsupported + alias Efsql.Physical.Plan + + # Alice "Lorem ipsum", Bob "foobar", Charles with no notes. + + defp names(context, condition, rest \\ "") do + "select name from #{context[:tenant_id]}.users where #{condition}#{rest};" + |> Efsql.all() + |> Enum.map(& &1.name) + end + + defp sorted_names(context, condition), do: context |> names(condition) |> Enum.sort() + + test "OR across fields, checked on the rows read", context do + {%Plan{access: {:pk_range, _, nil, nil, _}, ops: ops}, _rows, _} = + Efsql.qall( + "select name from #{context[:tenant_id]}.users where name = 'Alice' or notes = 'foobar';" + ) + + assert [{:filter, [{:or, _}]} | _] = ops + assert sorted_names(context, "name = 'Alice' or notes = 'foobar'") == ["Alice", "Bob"] + end + + test "a NULL fails only its own branch", context do + assert sorted_names(context, "notes = 'foobar' or name = 'Charles'") == ["Bob", "Charles"] + assert sorted_names(context, "notes <> 'foobar' or notes is null") == ["Alice", "Charles"] + end + + test "AND binds tighter than OR, and parentheses group", context do + assert sorted_names(context, "name = 'Alice' and notes is null or name = 'Bob'") == ["Bob"] + + assert sorted_names(context, "name = 'Alice' and (notes is null or name = 'Bob')") == [] + + assert sorted_names(context, "name >= 'B' and (notes is null or notes like 'foo%')") == + ["Bob", "Charles"] + end + + test "NOT over AND is an OR", context do + assert sorted_names(context, "not (name >= 'B' and notes is null)") == ["Alice", "Bob"] + end + + test "an OR of equalities on an indexed field reads each value", context do + {%Plan{access: {:union, [_, _]}}, rows, _} = + Efsql.qall( + "select name from #{context[:tenant_id]}.users " <> + "where name = 'Alice' or name = 'Charles' or name in ('Alice');" + ) + + assert rows |> Enum.map(& &1.name) |> Enum.sort() == ["Alice", "Charles"] + end + + test "an OR of keys reads each key", context do + {%Plan{access: {:union, [_, _]}}, rows, _} = + Efsql.qall("select id from #{context[:tenant_id]}.users where _ = '0001' or _ = '0003';") + + assert rows |> Enum.map(& &1.id) |> Enum.sort() == ["0001", "0003"] + end + + test "an IN reads a repeated value once", context do + assert names(context, "name in ('Bob', 'Bob')") == ["Bob"] + end + + test "ORDER BY and LIMIT apply after the OR", context do + assert names(context, "name = 'Alice' or notes is null", " order by name desc limit 1") == + ["Charles"] + end + + test "the primary key '_' in an OR only as a key lookup", context do + assert_raise Unsupported, ~r/on the primary key '_'.*an OR of = and IN/, fn -> + names(context, "_ = '0001' or name = 'Bob'") + end + + # the key's own field works anywhere + assert sorted_names(context, "id = '0001' or name = 'Bob'") == ["Alice", "Bob"] + end +end diff --git a/test/parser_test.exs b/test/parser_test.exs index c232a58..b3e72c9 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -497,7 +497,35 @@ defmodule Efsql.ParserTest do {"not a isnull", [{:not_null, :a}]}, {"not (a = 1 or b like 'x%')", [{:cmp, :!=, :a, 1}, {:not_like, :b, "x%"}]}, {"c = 2 and not (a = 1 or not b = 3)", - [{:cmp, :==, :c, 2}, {:cmp, :!=, :a, 1}, {:cmp, :==, :b, 3}]} + [{:cmp, :==, :c, 2}, {:cmp, :!=, :a, 1}, {:cmp, :==, :b, 3}]}, + {"not (a = 1 and b is null)", [{:or, [[{:cmp, :!=, :a, 1}], [{:not_null, :b}]]}]} + ] do + assert %Logical.Select{predicates: ^predicates} = + parse("select id from t.users where #{condition};"), + condition + end + end + + test "OR is one predicate of AND-ed branches" do + for {condition, predicates} <- [ + {"a = 1 or b = 2", [{:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :b, 2}]]}]}, + {"a = 1 or b = 2 or c is null", + [{:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :b, 2}], [{:is_null, :c}]]}]}, + {"a = 1 or (b = 2 and c < 3)", + [{:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :b, 2}, {:cmp, :<, :c, 3}]]}]}, + # AND binds tighter than OR + {"a = 1 and b = 2 or c = 3", + [{:or, [[{:cmp, :==, :a, 1}, {:cmp, :==, :b, 2}], [{:cmp, :==, :c, 3}]]}]}, + {"a = 1 and (b = 2 or c = 3)", + [{:cmp, :==, :a, 1}, {:or, [[{:cmp, :==, :b, 2}], [{:cmp, :==, :c, 3}]]}]}, + {"a = 1 or (b = 2 and (c = 3 or d = 4))", + [ + {:or, + [ + [{:cmp, :==, :a, 1}], + [{:cmp, :==, :b, 2}, {:or, [[{:cmp, :==, :c, 3}], [{:cmp, :==, :d, 4}]]}] + ]} + ]} ] do assert %Logical.Select{predicates: ^predicates} = parse("select id from t.users where #{condition};"), @@ -512,8 +540,7 @@ defmodule Efsql.ParserTest do test "unsupported conditions say what is unsupported" do for {condition, message} <- [ - {"a = 1 or b = 2", "OR is not supported"}, - {"not (a = 1 and b = 2)", "OR is not supported"}, + {"a = 1 or b", "the field b is not a condition"}, {"not a", "the field a is not a condition; NOT needs a condition"}, {"a ilike 1", "an ILIKE pattern must be a string"}, {"a = b", "comparing two fields"}, diff --git a/test/predicate_test.exs b/test/predicate_test.exs index 0020475..3b5ba2c 100644 --- a/test/predicate_test.exs +++ b/test/predicate_test.exs @@ -70,6 +70,24 @@ defmodule Efsql.PredicateTest do end end + test "OR holds when any branch does, a NULL failing only its own branch" do + assert holds?({:or, [[{:cmp, :==, :age, 1}], [{:cmp, :==, :name, "Alice"}]]}) + refute holds?({:or, [[{:cmp, :==, :age, 1}], [{:cmp, :==, :name, "Bob"}]]}) + assert holds?({:or, [[{:cmp, :==, :notes, "x"}], [{:cmp, :>, :age, 20}]]}) + refute holds?({:or, [[{:cmp, :==, :notes, "x"}], [{:cmp, :!=, :notes, "x"}]]}) + + # a branch is AND-ed, and may hold another OR + refute holds?({:or, [[{:cmp, :==, :age, 30}, {:is_null, :name}], [{:is_null, :age}]]}) + + assert holds?( + {:or, + [ + [{:is_null, :age}], + [{:cmp, :==, :age, 30}, {:or, [[{:is_null, :name}], [{:like, :name, "A%"}]]}] + ]} + ) + end + test "every predicate must hold" do assert Predicate.matches?(@row, []) refute Predicate.matches?(@row, [{:cmp, :==, :age, 30}, {:is_null, :name}]) @@ -91,7 +109,16 @@ defmodule Efsql.PredicateTest do {{:not_ilike, :l, "x"}, :l} ] do assert Predicate.field(predicate) == field + assert Predicate.fields(predicate) == [field] end + + assert Predicate.fields( + {:or, + [ + [{:cmp, :==, :a, 1}, {:is_null, :b}], + [{:or, [[{:in, :a, [2]}], [{:is_null, :c}]]}] + ]} + ) == [:a, :b, :c] end test "pushdown says how the planner can serve a predicate" do @@ -112,7 +139,8 @@ defmodule Efsql.PredicateTest do {:cmp, :!=, :a, 1}, {:cmp, :!=, :_, "k"}, {:not_in, :a, [1]}, - {:not_range, :a, {:>=, 1}, {:<=, 2}} + {:not_range, :a, {:>=, 1}, {:<=, 2}}, + {:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :b, 2}]]} ] do assert Predicate.pushdown(predicate) == :filter end diff --git a/test/rewrite_test.exs b/test/rewrite_test.exs index 441a486..20e6048 100644 --- a/test/rewrite_test.exs +++ b/test/rewrite_test.exs @@ -84,6 +84,37 @@ defmodule Efsql.RewriteTest do end end + describe "OR" do + test "an OR of equalities and INs on one field is an IN" do + assert [{:in, :a, [1, 2, 3]}] = + normalize([{:or, [[{:cmp, :==, :a, 1}], [{:in, :a, [2, 3]}]]}]) + + assert [{:in, :_, ["k1", "k2"]}] = + normalize([{:or, [[{:cmp, :==, :_, "k1"}], [{:cmp, :==, :_, "k2"}]]}]) + end + + test "each value once, and one value is an equality" do + assert [{:in, :a, [1, 2]}] = + normalize([{:or, [[{:cmp, :==, :a, 1}], [{:in, :a, [1, 2]}]]}]) + + assert [{:cmp, :==, :a, 1}] = + normalize([{:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :a, 1}]]}]) + + assert [{:in, :p, [%Decimal{}, 2]}] = + normalize([{:in, :p, [Decimal.new("1.0"), Decimal.new("1.00"), 2]}]) + end + + test "any other OR stays" do + for pred <- [ + {:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :b, 2}]]}, + {:or, [[{:cmp, :==, :a, 1}], [{:cmp, :>, :a, 5}]]}, + {:or, [[{:cmp, :==, :a, 1}], [{:cmp, :==, :a, 2}, {:is_null, :b}]]} + ] do + assert [^pred] = normalize([pred]) + end + end + end + test "Predicate.take_first takes the first match out, keeping the rest in order" do preds = [{:is_null, :a}, {:cmp, :>, :b, 1}, {:cmp, :>, :c, 2}] gt? = &match?({:cmp, :>, _, _}, &1) diff --git a/test/tui_app_test.exs b/test/tui_app_test.exs index c5120e3..ae60afb 100644 --- a/test/tui_app_test.exs +++ b/test/tui_app_test.exs @@ -487,7 +487,7 @@ defmodule Efsql.Tui.AppTest do # the whole page is reachable by scrolling all = for s <- 0..40, do: frame_text(%{help | help_scroll: s}) assert Enum.any?(all, &(&1 =~ "('u0006', *)")) - assert Enum.any?(all, &(&1 =~ "or is not supported")) + assert Enum.any?(all, &(&1 =~ "and binds tighter than or")) end test "ctrl-c cancels a running task before quitting" do