Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(soup): grouped query (#5385)

Will Hutchinsonb1bfc611d851
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
index 3cad3a5ed6..5e2a4e3e5e 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
@@ -289,7 +289,7 @@ static GROUPED_DOCUMENT_DETAIL_CLAUSE: &str = r#"
d."createdAt"::timestamptz as "created_at",
d."updatedAt"::timestamptz as "updated_at",
d."projectId" as "project_id",
- NULL as "is_persistent",
+ NULL::boolean as "is_persistent",
di.sha as "sha",
dt.sub_type as "sub_type",
uh."updatedAt"::timestamptz as "viewed_at",
@@ -338,22 +338,22 @@ static GROUPED_CHAT_DETAIL_CLAUSE: &str = r#"
SELECT
'chat' as "item_type",
c.id as "id",
- NULL as "document_version_id",
+ NULL::text as "document_version_id",
c."userId" as "user_id",
c.name as "name",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
+ NULL::text as "branched_from_id",
+ NULL::bigint as "branched_from_version_id",
+ NULL::bigint as "document_family_id",
+ NULL::text as "file_type",
c."createdAt"::timestamptz as "created_at",
c."updatedAt"::timestamptz as "updated_at",
c."projectId" as "project_id",
c."isPersistent" as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
+ NULL::text as "sha",
+ NULL::document_sub_type_value as "sub_type",
uh."updatedAt"::timestamptz as "viewed_at",
gi.sort_ts as "sort_ts",
- NULL as "is_completed",
+ NULL::boolean as "is_completed",
c."deletedAt"::timestamptz as "deleted_at",
NULL::jsonb as "calendar_event",
gi.group_key as "group_key",
@@ -370,22 +370,22 @@ static GROUPED_PROJECT_DETAIL_CLAUSE: &str = r#"
SELECT
'project' as "item_type",
p.id as "id",
- NULL as "document_version_id",
+ NULL::text as "document_version_id",
p."userId" as "user_id",
p.name as "name",
- NULL as "branched_from_id",
- NULL as "branched_from_version_id",
- NULL as "document_family_id",
- NULL as "file_type",
+ NULL::text as "branched_from_id",
+ NULL::bigint as "branched_from_version_id",
+ NULL::bigint as "document_family_id",
+ NULL::text as "file_type",
p."createdAt"::timestamptz as "created_at",
p."updatedAt"::timestamptz as "updated_at",
p."parentId" as "project_id",
- NULL as "is_persistent",
- NULL as "sha",
- NULL as "sub_type",
+ NULL::boolean as "is_persistent",
+ NULL::text as "sha",
+ NULL::document_sub_type_value as "sub_type",
uh."updatedAt"::timestamptz as "viewed_at",
gi.sort_ts as "sort_ts",
- NULL as "is_completed",
+ NULL::boolean as "is_completed",
p."deletedAt"::timestamptz as "deleted_at",
NULL::jsonb as "calendar_event",
gi.group_key as "group_key",
diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
index 01030f0241..9d47df466c 100644
--- a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
@@ -2,13 +2,15 @@ use super::*;
use crate::outbound::pg_soup_repo::expanded::dynamic::{
GroupedDynamicCursorArgs, expanded_dynamic_cursor_soup_grouped,
};
-use item_filters::ast::EntityFilterAst;
+use filter_ast::Expr;
+use item_filters::ast::{EntityFilterAst, chat::ChatLiteral, document::DocumentLiteral};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use models_grouping::date_bucket_sql_key;
use models_grouping::{GroupingConfig, date_bucket_order};
use models_pagination::{Identify, Query, SimpleSortMethod};
use sqlx::{Pool, Postgres};
+use std::sync::Arc;
#[test]
fn date_bucket_select_contains_all_keys() {
@@ -398,3 +400,94 @@ async fn tagged_calendar_event_participates_in_grouped_property_soup(
Ok(())
}
+
+/// Regression: when a filter makes an arm of the Combined UNION impossible,
+/// the remaining arms must still type-align. Postgres resolves UNION column
+/// types pairwise left-to-right, and untyped `NULL`s across two arms resolve
+/// to `text`, which then clashed with the calendar-event arm's typed NULLs
+/// ("UNION types text and boolean cannot be matched").
+#[sqlx::test(
+ fixtures(
+ path = "../../../../../macro_db_client/fixtures",
+ scripts("mixed_items_expanded")
+ ),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn grouped_soup_runs_when_chat_arm_excluded(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ // Importance(false) never matches a chat, so the chat arm is omitted and
+ // Combined unions document ∪ project ∪ calendar_event.
+ let filter = EntityFilterAst {
+ chat_filter: Some(Arc::new(Expr::val(ChatLiteral::Importance(false)))),
+ ..EntityFilterAst::mock_empty()
+ };
+
+ let items = expanded_dynamic_cursor_soup_grouped(
+ &pool,
+ GroupedDynamicCursorArgs {
+ user_id: user_id.copied(),
+ limit: 50,
+ cursor: Query::Sort(SimpleSortMethod::ViewedUpdated, filter),
+ exclude_frecency: false,
+ grouping: GroupingConfig {
+ field: GroupByField::EntityType,
+ group_key: None,
+ per_group_limit: None,
+ },
+ },
+ )
+ .await?
+ .collect::<Vec<_>>();
+
+ assert!(!items.is_empty(), "documents and projects should remain");
+ assert!(
+ items.iter().all(|item| item.key != "chat"),
+ "chats must be filtered out"
+ );
+
+ Ok(())
+}
+
+/// Same regression as [`grouped_soup_runs_when_chat_arm_excluded`] for the
+/// document arm: chat ∪ project ∪ calendar_event previously failed with
+/// "UNION types text and bigint cannot be matched".
+#[sqlx::test(
+ fixtures(
+ path = "../../../../../macro_db_client/fixtures",
+ scripts("mixed_items_expanded")
+ ),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn grouped_soup_runs_when_document_arm_excluded(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ // A nil document id filter is impossible, so the document arm is omitted.
+ let filter = EntityFilterAst {
+ document_filter: Some(Arc::new(Expr::val(DocumentLiteral::Id(uuid::Uuid::nil())))),
+ ..EntityFilterAst::mock_empty()
+ };
+
+ let items = expanded_dynamic_cursor_soup_grouped(
+ &pool,
+ GroupedDynamicCursorArgs {
+ user_id: user_id.copied(),
+ limit: 50,
+ cursor: Query::Sort(SimpleSortMethod::ViewedUpdated, filter),
+ exclude_frecency: false,
+ grouping: GroupingConfig {
+ field: GroupByField::EntityType,
+ group_key: None,
+ per_group_limit: None,
+ },
+ },
+ )
+ .await?
+ .collect::<Vec<_>>();
+
+ assert!(!items.is_empty(), "chats and projects should remain");
+ assert!(
+ items.iter().all(|item| item.key != "document"),
+ "documents must be filtered out"
+ );
+
+ Ok(())
+}