{
  "matching_project_details": "WITH values AS MATERIALIZED (\n SELECT v.document_id,\n max(v.value) FILTER(WHERE field_id='3c5428c9-5790-454d-bbbf-b4af42995fcc') source_id,\n max(v.value) FILTER(WHERE field_id='2036daa8-bf54-4c32-a5b0-7e97158572d1') agency_id,\n max(v.value) FILTER(WHERE field_id='c0173c63-4571-4124-9b17-7f561455ddf8') posting_id,\n max(v.value) FILTER(WHERE field_id='8c6de9d4-24c9-4854-b6b9-35ab9fc603a5') bid_date\n FROM app_fields_values v WHERE v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539'\n AND v.field_id IN ('3c5428c9-5790-454d-bbbf-b4af42995fcc','2036daa8-bf54-4c32-a5b0-7e97158572d1','c0173c63-4571-4124-9b17-7f561455ddf8','8c6de9d4-24c9-4854-b6b9-35ab9fc603a5')\n AND v.owner_id IS NULL AND v.user_id IS NULL AND v.deleted_at IS NULL GROUP BY v.document_id\n), facts AS (\n SELECT d.id, lower(regexp_replace(btrim(d.name),'[[:space:]]+',' ','g')) title,\n v.source_id,v.agency_id,v.posting_id,parse_stored_date_value(v.bid_date) bid_date\n FROM app_documents d JOIN values v ON v.document_id=d.id\n WHERE d.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' AND d.owner_id IS NULL AND d.deleted_at IS NULL\n AND nullif(btrim(v.source_id),'') IS NOT NULL AND nullif(btrim(v.posting_id),'') IS NOT NULL\n)\nSELECT source_id,posting_id,agency_id,title,bid_date,count(*) projects,array_agg(id ORDER BY id) project_ids\nFROM facts WHERE nullif(title,'') IS NOT NULL GROUP BY source_id,posting_id,agency_id,title,bid_date HAVING count(*)>1",
  "matching_item_context": "WITH item_docs AS MATERIALIZED (SELECT id,name,sources FROM app_documents WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND owner_id IS NULL AND deleted_at IS NULL), project_docs AS MATERIALIZED (SELECT id FROM app_documents WHERE collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' AND owner_id IS NULL AND deleted_at IS NULL), raw_links AS MATERIALIZED (SELECT source_document_id item_id,target_document_id project_id FROM app_fields_relations WHERE field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6' AND owner_id IS NULL AND deleted_at IS NULL AND coalesce(is_mask,false)=false), links AS MATERIALIZED (\n SELECT r.item_id,r.project_id FROM raw_links r JOIN item_docs i ON i.id=r.item_id JOIN project_docs p ON p.id=r.project_id\n), vals AS MATERIALIZED (\n SELECT document_id,max(value) FILTER(WHERE field_id='16fcb967-9801-4d0c-b6d7-5043506686dc') code,\n max(value) FILTER(WHERE field_id='393fa95b-ca28-5e2b-a2d4-ff45917ef981') quantity,\n max(value) FILTER(WHERE field_id='ab3a1c68-0558-4aae-bf97-8c47a755b3d0') unit_price\n FROM app_fields_values WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND field_id IN ('16fcb967-9801-4d0c-b6d7-5043506686dc','393fa95b-ca28-5e2b-a2d4-ff45917ef981','ab3a1c68-0558-4aae-bf97-8c47a755b3d0')\n AND owner_id IS NULL AND user_id IS NULL AND deleted_at IS NULL GROUP BY document_id\n), ctx AS MATERIALIZED (\n SELECT source_document_id item_id,\n string_agg(field_id::text||':'||target_document_id::text,',' ORDER BY field_id,target_document_id) graph_context,\n count(*) FILTER(WHERE field_id='3cfea941-ba61-448c-a637-85594b658ec0') unit_links\n FROM app_fields_relations WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6'\n AND field_id IN ('3cfea941-ba61-448c-a637-85594b658ec0','116ee4c8-8b7e-4b82-878f-e5f9f9a91925','9b30ec2c-7d12-422d-aa27-9cf67d170769','4de73696-4fe0-5400-807e-de9445689fba')\n AND owner_id IS NULL AND deleted_at IS NULL AND coalesce(is_mask,false)=false GROUP BY source_document_id\n), facts AS (\n SELECT l.project_id,i.id item_id,lower(regexp_replace(btrim(i.name),'[[:space:]]+',' ','g')) name,\n nullif(lower(btrim(v.code)),'') code,nullif(btrim(v.quantity),'') quantity,nullif(btrim(v.unit_price),'') unit_price,\n coalesce(ctx.graph_context,'') graph_context,coalesce(ctx.unit_links,0) unit_links,\n md5(coalesce(array_to_string(ARRAY(SELECT unnest(i.sources) ORDER BY 1),','),'')) source_context,\n coalesce(cardinality(i.sources)>0,false) has_source_context\n FROM links l JOIN item_docs i ON i.id=l.item_id LEFT JOIN vals v ON v.document_id=i.id LEFT JOIN ctx ON ctx.item_id=i.id\n)\nSELECT project_id,code,name,quantity,unit_price,md5(graph_context) graph_context_hash,unit_links,source_context,has_source_context,count(*) items,array_agg(item_id ORDER BY item_id) item_ids\nFROM facts WHERE nullif(name,'') IS NOT NULL\nGROUP BY project_id,code,name,quantity,unit_price,graph_context,unit_links,source_context,has_source_context HAVING count(*)>1",
  "matching_item_content": "WITH item_docs AS MATERIALIZED (SELECT id,name,sources FROM app_documents WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND owner_id IS NULL AND deleted_at IS NULL), project_docs AS MATERIALIZED (SELECT id FROM app_documents WHERE collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' AND owner_id IS NULL AND deleted_at IS NULL), raw_links AS MATERIALIZED (SELECT source_document_id item_id,target_document_id project_id FROM app_fields_relations WHERE field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6' AND owner_id IS NULL AND deleted_at IS NULL AND coalesce(is_mask,false)=false), links AS MATERIALIZED (\n SELECT r.item_id,r.project_id FROM raw_links r JOIN item_docs i ON i.id=r.item_id JOIN project_docs p ON p.id=r.project_id\n), vals AS MATERIALIZED (\n SELECT document_id,max(value) FILTER(WHERE field_id='16fcb967-9801-4d0c-b6d7-5043506686dc') code,\n max(value) FILTER(WHERE field_id='393fa95b-ca28-5e2b-a2d4-ff45917ef981') quantity\n FROM app_fields_values WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND field_id IN ('16fcb967-9801-4d0c-b6d7-5043506686dc','393fa95b-ca28-5e2b-a2d4-ff45917ef981')\n AND owner_id IS NULL AND user_id IS NULL AND deleted_at IS NULL GROUP BY document_id\n), ctx AS MATERIALIZED (\n SELECT source_document_id item_id,\n string_agg(field_id::text||':'||target_document_id::text,',' ORDER BY field_id,target_document_id) graph_context,\n count(*) FILTER(WHERE field_id='3cfea941-ba61-448c-a637-85594b658ec0') unit_links\n FROM app_fields_relations WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6'\n AND field_id IN ('3cfea941-ba61-448c-a637-85594b658ec0')\n AND owner_id IS NULL AND deleted_at IS NULL AND coalesce(is_mask,false)=false GROUP BY source_document_id\n), facts AS (\n SELECT l.project_id,i.id item_id,lower(regexp_replace(btrim(i.name),'[[:space:]]+',' ','g')) name,\n nullif(lower(btrim(v.code)),'') code,nullif(btrim(v.quantity),'') quantity,null::text unit_price,\n coalesce(ctx.graph_context,'') graph_context,coalesce(ctx.unit_links,0) unit_links,\n md5(coalesce(array_to_string(ARRAY(SELECT unnest(i.sources) ORDER BY 1),','),'')) source_context,\n coalesce(cardinality(i.sources)>0,false) has_source_context\n FROM links l JOIN item_docs i ON i.id=l.item_id LEFT JOIN vals v ON v.document_id=i.id LEFT JOIN ctx ON ctx.item_id=i.id\n)\nSELECT project_id,code,name,quantity,md5(graph_context) unit_context_hash,unit_links,count(distinct source_context) source_context_variants,bool_and(has_source_context) all_have_source_context,count(*) items,array_agg(item_id ORDER BY item_id) item_ids FROM facts WHERE code IS NOT NULL AND nullif(name,'') IS NOT NULL AND quantity IS NOT NULL GROUP BY project_id,code,name,quantity,graph_context,unit_links HAVING count(*)>1",
  "repeated_item_codes": "WITH items AS MATERIALIZED (\n SELECT id,name FROM app_documents WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND owner_id IS NULL AND deleted_at IS NULL\n), projects AS MATERIALIZED (SELECT id FROM app_documents WHERE collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' AND owner_id IS NULL AND deleted_at IS NULL),\nlinks AS MATERIALIZED (SELECT source_document_id item_id,target_document_id project_id FROM app_fields_relations WHERE field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6' AND owner_id IS NULL AND deleted_at IS NULL AND coalesce(is_mask,false)=false),\ncodes AS MATERIALIZED (SELECT document_id,nullif(lower(btrim(value)),'') code FROM app_fields_values WHERE collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' AND field_id='16fcb967-9801-4d0c-b6d7-5043506686dc' AND owner_id IS NULL AND user_id IS NULL AND deleted_at IS NULL)\nSELECT l.project_id,c.code,count(*) items,count(distinct lower(regexp_replace(btrim(i.name),'[[:space:]]+',' ','g'))) name_variants,array_agg(i.id ORDER BY i.id) item_ids\nFROM links l JOIN items i ON i.id=l.item_id JOIN projects p ON p.id=l.project_id JOIN codes c ON c.document_id=i.id WHERE c.code IS NOT NULL\nGROUP BY l.project_id,c.code HAVING count(*)>1"
}
