Skip to content

Commit

Permalink
fix: don't use :same return type for most operators
Browse files Browse the repository at this point in the history
fix: don't use returns as basis type unless explicitly allowed
  • Loading branch information
zachdaniel committed Jul 14, 2024
1 parent a665a17 commit c5b118a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
28 changes: 22 additions & 6 deletions lib/ash/expr/expr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,9 @@ defmodule Ash.Expr do
{type, constraints}
end

def determine_types(mod, values, returns) do
def determine_types(mod, values, known_result) do
Code.ensure_compiled(mod)

basis =
if returns && returns != [:any, :same, {:array, :any}, {:array, :same}] do
returns
end

name =
cond do
function_exported?(mod, :operator, 0) ->
Expand Down Expand Up @@ -893,6 +888,27 @@ defmodule Ash.Expr do
length(typeset) == length(values)
end)
|> Enum.find_value({Enum.map(values, fn _ -> nil end), nil}, fn {typeset, returns} ->
basis =
cond do
!returns ->
nil

returns == :same ->
known_result

returns == {:array, :same} ->
case known_result do
{:array, type} ->
type

_ ->
nil
end

true ->
nil
end

types_and_values =
if typeset == :same do
Enum.map(values, &{:same, &1})
Expand Down
3 changes: 2 additions & 1 deletion lib/ash/query/operator/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Ash.Query.Operator.Basic do
div: [
symbol: :/,
no_nils: true,
evaluate_types: :numbers
evaluate_types: :numbers,
returns: [:float]
],
concat: [
symbol: :<>,
Expand Down
3 changes: 1 addition & 2 deletions lib/ash/query/operator/eq.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ defmodule Ash.Query.Operator.Eq do
operator: :==,
name: :eq,
predicate?: true,
types: [:any, :same],
returns: [:boolean]
types: [:any, :same]

def new(left, right) do
{:ok, struct(__MODULE__, left: left, right: right)}
Expand Down
10 changes: 9 additions & 1 deletion lib/ash/query/operator/operator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,15 @@ defmodule Ash.Query.Operator do
def predicate?, do: unquote(opts[:predicate?] || false)

@impl Ash.Query.Operator
def returns, do: unquote(opts[:returns] || [:same])
if unquote(opts[:predicate?]) do
def returns do
Enum.map(List.wrap(unquote(opts[:types])), fn _ -> :boolean end)
end
else
def returns do
unquote(opts[:returns] || :unknown)
end
end

@impl Ash.Query.Operator
def types do
Expand Down

0 comments on commit c5b118a

Please sign in to comment.