Commit graph

55 commits

Author SHA1 Message Date
Zach Daniel
37cc01957d
improvement!: 3.0 (#227)
* WIP

* chore: fix mix.lock merge issues

* improvement: upgrade to 3.0

* chore: remove `repo.to_tenant`

* chore: continue removal of unnecessary helper

* chore: use `Ash.ToTenant`
2024-03-27 16:52:28 -04:00
Zach Daniel
5a513a82c2 fix: handle fully fleshed out aggregate fields 2024-03-22 02:13:07 -04:00
Zach Daniel
a0e2693998 fix: don't fail on aggregate query generation
fixes #225
2024-03-20 18:37:38 -04:00
Zach Daniel
b6b2dd1dc6 fix: handle more subquery filter cases for aggregates 2024-02-24 20:20:52 -05:00
Zach Daniel
f6d029d85b improvement: add test for aggregates 2024-02-24 18:08:00 -05:00
Zach Daniel
d0041ce88d improvement: optimize aggregate query filtering 2024-02-21 14:03:28 -05:00
Zach Daniel
2b91ebe488 fix: ensure proper return value for single aggregate runs 2024-02-21 09:56:33 -05:00
Zach Daniel
b5b1077032 chore: fix recursion error in to_many_path? 2024-02-20 22:13:23 -05:00
Zach Daniel
38d1beb0d8 fix: properly handle to_many joins in aggregates
fix: honor aggregate query filters
2024-02-20 18:38:35 -05:00
Zach Daniel
f3ccb78f86 improvement: support join_filters on aggregates
fix: subquery relationships that have filters
2024-01-11 22:30:35 -05:00
Zach Daniel
b52fc0b429 chore: improve matrix behavior and update exists tests 2023-11-17 07:10:37 -05:00
Zach Daniel
cae89d856e fix: get resource from proper bindings on exists query 2023-11-13 17:42:37 -05:00
Zach Daniel
f67db5ee6d chore: handle ci_string type as storage type 2023-10-10 14:16:25 -04:00
Zach Daniel
772c12b5a3 fix: don't run main query if only exists aggs are specified
fixes: #170
2023-10-10 09:43:09 -04:00
Zach Daniel
99b2a5631c improvement: support in-line aggregates
fix: support non-atom named aggregates
2023-08-22 14:51:31 -04:00
Zach Daniel
ed62006b3f improvement: support unary -/1 operator 2023-07-17 22:35:46 -04:00
Zach Daniel
062e67392a fix: convert Ash.Resource.Aggregate to Ash.Query.Aggregate when adding 2023-07-13 23:27:08 -04:00
Zach Daniel
d2e071773e improvement: support exists aggregates 2023-07-13 00:13:50 -04:00
Zach Daniel
f59bbee443 improvement: raise better errors on conflicting locks 2023-05-17 13:17:37 -04:00
Zach Daniel
b457c3052e test: fix tests using filter option on aggregates 2023-05-10 23:14:55 -04:00
Zach Daniel
d417361012 fix: subquery aggregates when a distinct is being added 2023-04-22 01:17:38 -06:00
Zach Daniel
49b94fd183 fix: don't use :distinct when uniq? is not true 2023-02-17 15:21:50 -05:00
Zach Daniel
bbb45f3622 fix: sorting on optimized first aggregates 2023-02-09 17:49:37 -05:00
Zach Daniel
8e8a308cd2 improvement: support new uniq? option on count/list aggregates 2023-02-09 16:09:44 -05:00
Zach Daniel
80cd06818d improvement: optimized first aggregates where possible 2023-02-08 14:46:29 -05:00
Zach Daniel
8d45e92fc2 fix: do limit/offset outside of query if distinct is required 2023-02-07 19:15:44 -05:00
Zach Daniel
b41d383b47 fix: load by __order__ ascending 2023-02-07 17:43:53 -05:00
Alan Heywood
09092cc051 test: demonstrate issue loading aggregate
Calling Api.load! to load an aggregate on an existing record, where
the aggregated record has a relates_to_actor_via policy on it causes
the following error. Note that this error was recently fixed when
loading the aggregate as part of the query.

1) test relates to actor via has_many and with an aggregate (AshPostgres.AggregateTest)
     test/aggregate_test.exs:8
     ** (RuntimeError) Could not determined related for `exists/2` expression.

     Context Resource: %{aggregates: %{}, calculations: %{}, data_layer: AshPostgres.DataLayer, public?: false, relationship_path: [], resource: AshPostgres.Test.Comment, root_resource: AshPostgres.Test.Comment}
     Context Relationship Path: []
     At Path: []
     Path: [:organization, :users]
     Related: nil
     Expression: exists(organization.users, [id: "df84db36-b8de-485b-8e47-a86307588f79"])

     code: |> Api.load!(:count_of_comments, actor: user)
     stacktrace:
       (ash 2.5.13) lib/ash/filter/filter.ex:2121: Ash.Filter.add_expression_part/3
       (ash 2.5.13) lib/ash/filter/filter.ex:2037: anonymous fn/3 in Ash.Filter.parse_expression/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.13) lib/ash/filter/filter.ex:295: Ash.Filter.parse/5
       (ash 2.5.13) lib/ash/query/query.ex:1803: Ash.Query.do_filter/2
       (elixir 1.14.2) lib/map.ex:883: Map.update!/3
       (ash 2.5.13) lib/ash/engine/request.ex:654: Ash.Engine.Request.apply_filter/4
       (ash 2.5.13) lib/ash/engine/request.ex:561: Ash.Engine.Request.do_strict_check/3
       (ash 2.5.13) lib/ash/engine/request.ex:522: anonymous fn/2 in Ash.Engine.Request.strict_check/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.13) lib/ash/engine/request.ex:255: Ash.Engine.Request.do_next/1
       (ash 2.5.13) lib/ash/engine/request.ex:211: Ash.Engine.Request.next/1
       (ash 2.5.13) lib/ash/engine/engine.ex:650: Ash.Engine.advance_request/2
       (ash 2.5.13) lib/ash/engine/engine.ex:556: Ash.Engine.fully_advance_request/2
       (ash 2.5.13) lib/ash/engine/engine.ex:497: Ash.Engine.do_run_iteration/2
       (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ash 2.5.13) lib/ash/engine/engine.ex:440: Ash.Engine.run_iteration/1
       (ash 2.5.13) lib/ash/engine/engine.ex:257: Ash.Engine.run_to_completion/1
       (ash 2.5.13) lib/ash/engine/engine.ex:202: Ash.Engine.do_run/2
       (ash 2.5.13) lib/ash/engine/engine.ex:141: Ash.Engine.run/2
       (ash 2.5.13) lib/ash/actions/read.ex:170: Ash.Actions.Read.do_run/3
       (ash 2.5.13) lib/ash/actions/read.ex:90: Ash.Actions.Read.run/3
       (ash 2.5.13) lib/ash/api/api.ex:928: Ash.Api.load/4
       (ash 2.5.13) lib/ash/api/api.ex:902: Ash.Api.load!/4
       test/aggregate_test.exs:48: (test)
2023-01-31 10:35:33 +10:00
Alan Heywood
1dbd7f619d Update test to demo issue with policy on aggregate
The issue occurs when an aggregate is loaded, and the aggregated
resource itself has a relates_to_actor_via policy.

The following error is produced at test/aggregate_test.exs:42

  1) test relates to actor via has_many and with an aggregate (AshPostgres.AggregateTest)
     test/aggregate_test.exs:8
     ** (RuntimeError) Could not determined related for `exists/2` expression.

     Context Resource: %{aggregates: %{count_of_comments: #count<comments from #Ash.Query<resource: AshPostgres.Test.Comment>>}, calculations: %{}, data_layer: AshPostgres.DataLayer, public?: false, relationship_path: [], resource: AshPostgres.Test.Post, root_resource: AshPostgres.Test.Post}
     Context Relationship Path: []
     At Path: []
     Path: [:post, :organization, :users]
     Related: nil
     Expression: exists(post.organization.users, [id: "236f08bc-6a3a-4a78-9911-2c24189dafac"])

     code: |> Api.read_one!()
     stacktrace:
       (ash 2.5.12) lib/ash/filter/filter.ex:2121: Ash.Filter.add_expression_part/3
       (ash 2.5.12) lib/ash/filter/filter.ex:2037: anonymous fn/3 in Ash.Filter.parse_expression/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.12) lib/ash/filter/filter.ex:295: Ash.Filter.parse/5
       (ash 2.5.12) lib/ash/filter/filter.ex:995: Ash.Filter.add_to_filter/6
       (ash 2.5.12) lib/ash/query/query.ex:1797: Ash.Query.do_filter/2
       (elixir 1.14.2) lib/map.ex:883: Map.update!/3
       (ash 2.5.12) lib/ash/engine/request.ex:650: Ash.Engine.Request.apply_filter/4
       (ash 2.5.12) lib/ash/engine/request.ex:557: Ash.Engine.Request.do_strict_check/3
       (ash 2.5.12) lib/ash/engine/request.ex:518: anonymous fn/2 in Ash.Engine.Request.strict_check/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.12) lib/ash/engine/request.ex:255: Ash.Engine.Request.do_next/1
       (ash 2.5.12) lib/ash/engine/request.ex:211: Ash.Engine.Request.next/1
       (ash 2.5.12) lib/ash/engine/engine.ex:650: Ash.Engine.advance_request/2
       (ash 2.5.12) lib/ash/engine/engine.ex:556: Ash.Engine.fully_advance_request/2
       (ash 2.5.12) lib/ash/engine/engine.ex:497: Ash.Engine.do_run_iteration/2
       (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ash 2.5.12) lib/ash/engine/engine.ex:257: Ash.Engine.run_to_completion/1
       (ash 2.5.12) lib/ash/engine/engine.ex:202: Ash.Engine.do_run/2
       (ash 2.5.12) lib/ash/engine/engine.ex:141: Ash.Engine.run/2
       (ash 2.5.12) lib/ash/actions/read.ex:170: Ash.Actions.Read.do_run/3
       (ash 2.5.12) lib/ash/actions/read.ex:90: Ash.Actions.Read.run/3
       (ash 2.5.12) lib/ash/api/api.ex:1005: Ash.Api.read_one/3
       (ash 2.5.12) lib/ash/api/api.ex:998: Ash.Api.read_one!/3
       test/aggregate_test.exs:43: (test)
2023-01-30 16:09:48 +10:00
Zach Daniel
04a64b8b94 chore: credo 2023-01-29 18:24:11 -05:00
Alan Heywood
f290ae47b8 Add failing test for policy + aggregate issue
The conditions for this issue to occur seem to be:

- DataLayer is Postgres
- Resource has a relates_to_actor_via policy on read
- The relates_to_actor_via path includes a has_many relationship
- An aggregate is loaded

The following error is produced:

  1) test relates to actor via has_many and with an aggregate (AshPostgres.AggregateTest)
     test/aggregate_test.exs:8
     ** (Ash.Error.Unknown.UnknownError) ** (ArgumentError) No such entity nil found.

     code: |> Api.read_one!(actor: user)
     stacktrace:
       nil.spark_dsl_config()
       (spark 0.3.8) lib/spark/dsl/extension.ex:129: Spark.Dsl.Extension.dsl!/1
       (spark 0.3.8) lib/spark/dsl/extension.ex:158: Spark.Dsl.Extension.get_persisted/3
       (ash 2.5.10) lib/ash/filter/filter.ex:2986: Ash.Filter.do_hydrate_refs/2
       (ash 2.5.10) lib/ash/policy/check/relates_to_actor_via.ex:3: Ash.Policy.Check.RelatesToActorVia.try_eval/2
       (ash 2.5.10) lib/ash/policy/check/relates_to_actor_via.ex:3: Ash.Policy.Check.RelatesToActorVia.try_strict_check/3
       (ash 2.5.10) lib/ash/policy/checker.ex:63: Ash.Policy.Checker.do_strict_check_facts/3
       (ash 2.5.10) lib/ash/policy/checker.ex:88: anonymous fn/2 in Ash.Policy.Checker.strict_check_policies/3
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.10) lib/ash/policy/checker.ex:9: anonymous fn/2 in Ash.Policy.Checker.strict_check_facts/1
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.10) lib/ash/policy/authorizer.ex:790: Ash.Policy.Authorizer.do_strict_check_facts/1
       (ash 2.5.10) lib/ash/policy/authorizer.ex:372: Ash.Policy.Authorizer.strict_check/2
       (ash 2.5.10) lib/ash/engine/request.ex:550: Ash.Engine.Request.do_strict_check/3
       (ash 2.5.10) lib/ash/engine/request.ex:518: anonymous fn/2 in Ash.Engine.Request.strict_check/2
       (elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
       (elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
       (ash 2.5.10) lib/ash/engine/request.ex:255: Ash.Engine.Request.do_next/1
       (ash 2.5.10) lib/ash/engine/request.ex:211: Ash.Engine.Request.next/1
       (ash 2.5.10) lib/ash/engine/engine.ex:650: Ash.Engine.advance_request/2
       (ash 2.5.10) lib/ash/engine/engine.ex:556: Ash.Engine.fully_advance_request/2
       (ash 2.5.10) lib/ash/engine/engine.ex:497: Ash.Engine.do_run_iteration/2
       (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ash 2.5.10) lib/ash/engine/engine.ex:440: Ash.Engine.run_iteration/1
       (ash 2.5.10) lib/ash/engine/engine.ex:257: Ash.Engine.run_to_completion/1
       (ash 2.5.10) lib/ash/engine/engine.ex:202: Ash.Engine.do_run/2
       (ash 2.5.10) lib/ash/engine/engine.ex:141: Ash.Engine.run/2
       (ash 2.5.10) lib/ash/actions/read.ex:170: Ash.Actions.Read.do_run/3
       (ash 2.5.10) lib/ash/actions/read.ex:90: Ash.Actions.Read.run/3
       (ash 2.5.10) lib/ash/api/api.ex:1005: Ash.Api.read_one/3
       (ash 2.5.10) lib/ash/api/api.ex:998: Ash.Api.read_one!/3
       test/aggregate_test.exs:44: (test)
2023-01-29 16:15:16 +10:00
Zach Daniel
3c496512e2 fix: use parent_expr instead of this
fix: various expression & type building fixes
2023-01-06 17:05:23 -05:00
Zach Daniel
e81e77840a fix: don't group aggregates that reference relationships in their filters 2022-12-21 16:58:12 -05:00
Zach Daniel
e271a27d3c improvement: avg/min/max/custom aggregate support
fix: fix various problems with the model behind aggregates
2022-12-07 20:32:38 -05:00
Zach Daniel
52fcba79ac improvement: inform users about postgres incompatibility with multidimensional arrays 2022-12-01 20:24:49 -05:00
Darren Black
f088601cf9
Failing tests for aggregates on arrays (#121) 2022-11-30 21:26:58 -05:00
Zach Daniel
fd5ac60072 improvement: exists filters necessitate multiple aggregate joins (for now) 2022-09-28 18:01:20 -04:00
Zach Daniel
e8643d4aa0 fix: properly type cast top level fragments
chore: update tests not to use deprecated helper
2022-09-21 13:36:18 -04:00
Zach Daniel
4a69edce2d important: update to ash 2.0 branch 2022-08-24 12:07:38 -04:00
Zach Daniel
c352a1f1fa fix: support upsert_identity with base_filter 2022-07-01 19:12:14 -04:00
Zach Daniel
da07ed7b6c fix: handle various join bugs 2022-06-29 15:08:49 -04:00
kernel-io
7675191172
failing custom aggregate test (#82)
Co-authored-by: kernel-io <kernel>
Co-authored-by: Zach Daniel <zach@zachdaniel.dev>
2022-03-09 19:45:06 -05:00
Zach Daniel
efaa7c6cc6 fix: properly handle relationship filter bindings
fix: don't consider fields changed with only source -> name changes
2022-02-14 17:44:17 -05:00
Zach Daniel
e6ff1d8b4b fix: fix aggregate/base filters 2022-02-11 16:06:51 -05:00
Zach Daniel
cd9701a6ea improvement: support referencing aggregates from aggregate filters
fix: don't select more fields than necessary
2022-02-09 11:49:19 -05:00
Zach Daniel
5b88f476f2 chore: remove empty test 2021-12-17 13:25:45 -05:00
Zach Daniel
2fdcea7ef8 improvement: support default on aggregates
chore: update to latest ash patterns for testing
2021-10-25 18:53:34 -04:00
Zach Daniel
be7e2a89bc fix: left_lateral_join for many_to_many aggregates
fix: properly reference nested aggregate fields for join
fix: properly determine fallback table for polymorphic resources
2021-07-05 14:12:21 -04:00
Zach Daniel
f13f31330f fix: properly set aggregate query sources 2021-07-02 12:41:44 -04:00
Zach Daniel
f0b901173c fix: properly group many_to_many aggregates 2021-05-12 13:17:26 -04:00