fix: fix the compare/2 implementations (#1232)

Co-authored-by: Andreas Donig <git@innwiese.de>
This commit is contained in:
Andreas Donig 2024-06-10 14:15:51 +02:00 committed by GitHub
parent 6b964b9384
commit 4153ba7ef3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 99 additions and 23 deletions

View file

@ -44,4 +44,25 @@ defmodule Ash.Query.Operator.Eq do
Ash.SatSolver.mutually_exclusive(predicates)
end)
end
@impl Ash.Filter.Predicate
def compare(
%__MODULE__{left: left, right: left_right},
%__MODULE__{left: left, right: right_right}
) do
if Comp.equal?(left_right, right_right) do
:mutually_inclusive
else
:mutually_exclusive
end
end
def compare(%__MODULE__{} = left, %Ash.Query.Operator.In{} = right) do
case Ash.Query.Operator.In.compare(right, left) do
:left_includes_right -> :right_includes_left
other -> other
end
end
def compare(_, _), do: :unknown
end

View file

@ -31,38 +31,35 @@ defmodule Ash.Query.Operator.In do
end
@impl Ash.Filter.Predicate
def compare(%__MODULE__{left: left, right: %MapSet{} = left_right}, %__MODULE__{
left: left,
right: %MapSet{} = right_right
}) do
if MapSet.equal?(left_right, right_right) do
:mutually_inclusive
else
if MapSet.disjoint?(left_right, right_right) do
:mutually_exclusive
else
:unknown
end
def compare(
%__MODULE__{left: left, right: %MapSet{} = mapset_left},
%__MODULE__{left: left, right: %MapSet{} = mapset_right}
) do
cond do
MapSet.equal?(mapset_left, mapset_right) -> :mutually_inclusive
MapSet.disjoint?(mapset_left, mapset_right) -> :mutually_exclusive
true -> :unknown
end
end
def compare(%__MODULE__{}, %Ash.Query.Operator.Eq{
right: %Ref{}
}),
do: false
def compare(%__MODULE__{}, %Ash.Query.Operator.Eq{right: %Ref{}}) do
:unknown
end
def compare(%__MODULE__{left: left, right: %MapSet{} = left_right}, %Ash.Query.Operator.Eq{
left: left,
right: value
}) do
if MapSet.member?(left_right, value) do
:left_implies_right
def compare(
%__MODULE__{left: left, right: %MapSet{} = mapset},
%Ash.Query.Operator.Eq{left: left, right: value}
) do
if MapSet.member?(mapset, value) do
:left_includes_right
else
:mutually_exclusive
end
end
def compare(_, _), do: :unknown
def compare(_, _) do
:unknown
end
def to_string(%{left: left, right: %MapSet{} = mapset}, opts) do
import Inspect.Algebra

View file

@ -0,0 +1,29 @@
defmodule Ash.Query.Operator.EqTest do
use ExUnit.Case
alias Ash.Query.Operator.Eq
alias Ash.Query.Operator.In
describe "compare/2" do
test "returns :mutually_inclusive for equal left and right values" do
left = %Eq{left: 1, right: 2}
right = %Eq{left: 1, right: 2}
assert Eq.compare(left, right) == :mutually_inclusive
end
test "returns :mutually_exclusive for different right values" do
left = %Eq{left: 1, right: 2}
right = %Eq{left: 1, right: 3}
assert Eq.compare(left, right) == :mutually_exclusive
end
test "returns :right_includes_left for Eq in In" do
left = %Eq{left: 1, right: 2}
right = %In{left: 1, right: MapSet.new([2, 3])}
assert Eq.compare(left, right) == :right_includes_left
end
end
end

View file

@ -0,0 +1,29 @@
defmodule Ash.Query.Operator.InTest do
use ExUnit.Case
alias Ash.Query.Operator.Eq
alias Ash.Query.Operator.In
describe "compare/2" do
test "returns :mutually_inclusive for equal left and right values" do
left = %In{left: 1, right: MapSet.new([2, 3])}
right = %In{left: 1, right: MapSet.new([2, 3])}
assert In.compare(left, right) == :mutually_inclusive
end
test "returns :mutually_exclusive for different right values" do
left = %In{left: 1, right: MapSet.new([2, 3])}
right = %In{left: 1, right: MapSet.new([4, 5])}
assert In.compare(left, right) == :mutually_exclusive
end
test "returns :left_includes_left for Eq in In" do
left = %In{left: 1, right: MapSet.new([2, 3])}
right = %Eq{left: 1, right: 2}
assert In.compare(left, right) == :left_includes_right
end
end
end