From 86ac85ead2bdaa74fab2ba77e2959b090f5fd98a Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Fri, 12 Jul 2024 14:23:49 +0530 Subject: [PATCH] fix: LIMIT logic --- ...60e940a06ed4311bc9a371d5c5df448f75bf.json} | 4 +- cala-ledger/src/account_set/repo.rs | 54 +++++++++---------- ...60e940a06ed4311bc9a371d5c5df448f75bf.json} | 4 +- 3 files changed, 30 insertions(+), 32 deletions(-) rename cala-ledger/.sqlx/{query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json => query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json} (56%) rename cala-server/.sqlx/{query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json => query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json} (56%) diff --git a/cala-ledger/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json b/cala-ledger/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json similarity index 56% rename from cala-ledger/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json rename to cala-ledger/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json index e3e1974f..28dc1274 100644 --- a/cala-ledger/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json +++ b/cala-ledger/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH member_accounts AS (\n SELECT\n member_account_id AS member_id,\n member_account_id,\n NULL::uuid AS member_account_set_id,\n created_at\n FROM cala_account_set_member_accounts\n WHERE\n transitive IS FALSE\n AND account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), member_sets AS (\n SELECT\n member_account_set_id AS member_id,\n NULL::uuid AS member_account_id,\n member_account_set_id,\n created_at\n FROM cala_account_set_member_account_sets\n WHERE\n account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n )\n SELECT * FROM member_accounts\n UNION ALL\n SELECT * FROM member_sets\n ORDER BY created_at DESC\n ", + "query": "\n WITH member_accounts AS (\n SELECT\n member_account_id AS member_id,\n member_account_id,\n NULL::uuid AS member_account_set_id,\n created_at\n FROM cala_account_set_member_accounts\n WHERE\n transitive IS FALSE\n AND account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), member_sets AS (\n SELECT\n member_account_set_id AS member_id,\n NULL::uuid AS member_account_id,\n member_account_set_id,\n created_at\n FROM cala_account_set_member_account_sets\n WHERE\n account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), all_members AS (\n SELECT * FROM member_accounts\n UNION ALL\n SELECT * FROM member_sets\n )\n SELECT * FROM all_members\n ORDER BY created_at DESC\n LIMIT $3\n ", "describe": { "columns": [ { @@ -38,5 +38,5 @@ null ] }, - "hash": "90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903" + "hash": "52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf" } diff --git a/cala-ledger/src/account_set/repo.rs b/cala-ledger/src/account_set/repo.rs index be5d3e5f..0b64243f 100644 --- a/cala-ledger/src/account_set/repo.rs +++ b/cala-ledger/src/account_set/repo.rs @@ -83,8 +83,8 @@ impl AccountSetRepo { transitive IS FALSE AND account_set_id = $1 AND (created_at < $2 OR $2 IS NULL) - ORDER BY created_at DESC - LIMIT $3 + ORDER BY created_at DESC + LIMIT $3 ), member_sets AS ( SELECT member_account_set_id AS member_id, @@ -95,13 +95,16 @@ impl AccountSetRepo { WHERE account_set_id = $1 AND (created_at < $2 OR $2 IS NULL) - ORDER BY created_at DESC - LIMIT $3 + ORDER BY created_at DESC + LIMIT $3 + ), all_members AS ( + SELECT * FROM member_accounts + UNION ALL + SELECT * FROM member_sets ) - SELECT * FROM member_accounts - UNION ALL - SELECT * FROM member_sets + SELECT * FROM all_members ORDER BY created_at DESC + LIMIT $3 "#, id as AccountSetId, after, @@ -116,29 +119,24 @@ impl AccountSetRepo { member_created_at: last.created_at.expect("created_at not set"), }); } - let mut account_ids = Vec::new(); - let mut account_set_ids = Vec::new(); - - for row in rows.into_iter() { - if let Some(member_account_id) = row.member_account_id { - account_ids.push(( - AccountSetMemberId::Account(AccountId::from(member_account_id)), - row.created_at.expect("created at should always be present"), - )); - } else if let Some(member_account_set_id) = row.member_account_set_id { - account_set_ids.push(( - AccountSetMemberId::AccountSet(AccountSetId::from(member_account_set_id)), - row.created_at.expect("created at should always be present"), - )); - } - } - let mut ids = account_ids + + let account_set_members = rows .into_iter() .take(args.first) - .chain(account_set_ids.into_iter().take(args.first)) - .collect::>(); - ids.sort_by_key(|(_, created_at)| std::cmp::Reverse(*created_at)); - let account_set_members = ids.into_iter().map(AccountSetMember::from).collect(); + .map( + |row| match (row.member_account_id, row.member_account_set_id) { + (Some(member_account_id), _) => AccountSetMember::from(( + AccountSetMemberId::Account(AccountId::from(member_account_id)), + row.created_at.expect("created at should always be present"), + )), + (_, Some(member_account_set_id)) => AccountSetMember::from(( + AccountSetMemberId::AccountSet(AccountSetId::from(member_account_set_id)), + row.created_at.expect("created at should always be present"), + )), + _ => unreachable!(), + }, + ) + .collect::>(); Ok(query::PaginatedQueryRet { entities: account_set_members, diff --git a/cala-server/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json b/cala-server/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json similarity index 56% rename from cala-server/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json rename to cala-server/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json index e3e1974f..28dc1274 100644 --- a/cala-server/.sqlx/query-90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903.json +++ b/cala-server/.sqlx/query-52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n WITH member_accounts AS (\n SELECT\n member_account_id AS member_id,\n member_account_id,\n NULL::uuid AS member_account_set_id,\n created_at\n FROM cala_account_set_member_accounts\n WHERE\n transitive IS FALSE\n AND account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), member_sets AS (\n SELECT\n member_account_set_id AS member_id,\n NULL::uuid AS member_account_id,\n member_account_set_id,\n created_at\n FROM cala_account_set_member_account_sets\n WHERE\n account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n )\n SELECT * FROM member_accounts\n UNION ALL\n SELECT * FROM member_sets\n ORDER BY created_at DESC\n ", + "query": "\n WITH member_accounts AS (\n SELECT\n member_account_id AS member_id,\n member_account_id,\n NULL::uuid AS member_account_set_id,\n created_at\n FROM cala_account_set_member_accounts\n WHERE\n transitive IS FALSE\n AND account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), member_sets AS (\n SELECT\n member_account_set_id AS member_id,\n NULL::uuid AS member_account_id,\n member_account_set_id,\n created_at\n FROM cala_account_set_member_account_sets\n WHERE\n account_set_id = $1\n AND (created_at < $2 OR $2 IS NULL)\n ORDER BY created_at DESC\n LIMIT $3\n ), all_members AS (\n SELECT * FROM member_accounts\n UNION ALL\n SELECT * FROM member_sets\n )\n SELECT * FROM all_members\n ORDER BY created_at DESC\n LIMIT $3\n ", "describe": { "columns": [ { @@ -38,5 +38,5 @@ null ] }, - "hash": "90a48895b1be1d3a6acbadcb64699b244360fad37df1d29f871ddf057debe903" + "hash": "52884ebc5f4d5fd2259e02e1110560e940a06ed4311bc9a371d5c5df448f75bf" }