Skip to content

Commit 4f1a084

Browse files
committed
improvement: don't cast integer/string equality check unnecessarily
1 parent 3f89d47 commit 4f1a084

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

lib/expr.ex

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,13 @@ defmodule AshSql.Expr do
13441344
determine_types(bindings.sql_behaviour, mod, [left, right], type)
13451345
end
13461346

1347+
bindings =
1348+
if no_cast_for_native_value?(left, left_type) or no_cast_for_native_value?(right, right_type) do
1349+
Map.put(bindings, :skip_cast_for_ref?, true)
1350+
else
1351+
bindings
1352+
end
1353+
13471354
{left_expr, acc} =
13481355
if left_type do
13491356
maybe_type_expr(
@@ -3554,7 +3561,11 @@ defmodule AshSql.Expr do
35543561
end
35553562

35563563
defp maybe_type_expr(query, expr, bindings, embedded?, acc, type) do
3557-
if type do
3564+
skip? =
3565+
no_cast_for_native_value?(expr, type) or
3566+
(is_struct(expr, Ash.Query.Ref) and bindings[:skip_cast_for_ref?])
3567+
3568+
if type && !skip? do
35583569
if get_path_array_type?(type) do
35593570
case strip_get_path_type(expr) do
35603571
%GetPath{} = get_path ->
@@ -3595,10 +3606,23 @@ defmodule AshSql.Expr do
35953606
)
35963607
end
35973608
else
3598-
do_dynamic_expr(query, expr, bindings, embedded?, acc, type)
3609+
do_dynamic_expr(query, expr, Map.put(bindings, :no_cast?, true), embedded?, acc, type)
35993610
end
36003611
end
36013612

3613+
defp no_cast_for_native_value?(expr, type) when is_binary(expr) do
3614+
unwrap_type_for_native_check(type) == Ash.Type.String
3615+
end
3616+
3617+
defp no_cast_for_native_value?(expr, type) when is_integer(expr) do
3618+
unwrap_type_for_native_check(type) == Ash.Type.Integer
3619+
end
3620+
3621+
defp no_cast_for_native_value?(_, _), do: false
3622+
3623+
defp unwrap_type_for_native_check({type, _}), do: type
3624+
defp unwrap_type_for_native_check(type), do: type
3625+
36023626
defp cant_return_nil?(%Ash.Query.Ref{attribute: %{allow_nil?: false}, relationship_path: []}) do
36033627
true
36043628
end

0 commit comments

Comments
 (0)