From 521afa7e484883c43b89e0f69b8d18b31d850a5b Mon Sep 17 00:00:00 2001 From: jkaczman Date: Wed, 18 Nov 2026 11:19:38 -0800 Subject: [PATCH] fix: support cross-shard subqueries that only return constants (e.g., Rails .limit(N).count) --- .../integration/subqueries/tests/select.sql | 23 +++++++++++++++++++ .../multi_step/subquery/clause/from_clause.rs | 18 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/integration/rust/tests/integration/subqueries/tests/select.sql b/integration/rust/tests/integration/subqueries/tests/select.sql index c9a923246..33f600c8f 100644 --- a/integration/rust/tests/integration/subqueries/tests/select.sql +++ b/integration/rust/tests/integration/subqueries/tests/select.sql @@ -225,6 +225,29 @@ SELECT * FROM ( -- sql: SELECT * FROM (SELECT u.c1, ($2::pg_catalog._varchar)[u.c2 + 1:u.c2 + u.c3] FROM unnest($1::pg_catalog._int8, $3::pg_catalog._int4, $4::pg_catalog._int4) u(c1, c2, c3)) s(id, tags) ORDER BY id SELECT * FROM (SELECT id, tags FROM subusers) s ORDER BY id; +-- case +-- what: SELECT COUNT(*) FROM () s, only returns constants +-- used by: Rails, which counts at most N rows for .limit(N).count (e.g., FollowRequest.where(..).limit(40).count) with +-- SELECT COUNT(*) FROM (SELECT 1 AS one FROM .. LIMIT N) subquery_for_count +-- - https://github.com/rails/rails/blob/v7.1.4/activerecord/lib/active_record/relation/calculations.rb#L453-L458 +-- - https://github.com/rails/rails/blob/v7.1.4/activerecord/lib/active_record/relation/calculations.rb#L626-L639 +-- used by: Mastodon, which counts the pending follow requests sent to accounts when the accounts log in with FollowRequest.where(target_account: ..).limit(40).count +-- - https://github.com/mastodon/mastodon/blob/v4.3.0/app/serializers/rest/credential_account_serializer.rb#L17 +-- route select: 0,1 +-- route select: any +-- sql: SELECT count(*) FROM (SELECT 1 FROM unnest($1::pg_catalog._int4) u(c1)) subquery_for_count(one) +SELECT COUNT(*) FROM (SELECT 1 AS one FROM subusers WHERE status = 'VIP' LIMIT 40) subquery_for_count; + +-- case +-- what: SELECT COUNT(*) FROM () s, only returns constants & returns zero rows +-- used by: Mastodon, which counts the pending follow requests sent to accounts when the accounts log in +-- For accounts without pending follow requests (most accounts), the derived table (SELECT 1 AS one FROM follow_requests WHERE target_account_id = .. LIMIT 40) doesn't return any rows +-- - https://github.com/mastodon/mastodon/blob/v4.3.0/app/serializers/rest/credential_account_serializer.rb#L17 +-- route select: 0,1 +-- route select: any +-- sql: SELECT count(*) FROM (SELECT 1 FROM unnest($1::pg_catalog._int4) u(c1)) subquery_for_count(one) WHERE false +SELECT COUNT(*) FROM (SELECT 1 AS one FROM subusers WHERE status = 'nobody' LIMIT 40) subquery_for_count; + -- case -- what: Derived table with an inner-table that returns an empty response. -- Want to make sure that the OIDs returned are correct (if present) diff --git a/pgdog/src/frontend/client/query_engine/multi_step/subquery/clause/from_clause.rs b/pgdog/src/frontend/client/query_engine/multi_step/subquery/clause/from_clause.rs index 7279ab6f9..b932efb09 100644 --- a/pgdog/src/frontend/client/query_engine/multi_step/subquery/clause/from_clause.rs +++ b/pgdog/src/frontend/client/query_engine/multi_step/subquery/clause/from_clause.rs @@ -269,6 +269,24 @@ impl<'a> SubqueryMulti<'a> { )); } + // If the subquery only returns constants, we don't have any columns to send, however unnest() needs at least one argument + // E.g., to count at most 40 rows (.limit(40).count), Rails sends SELECT COUNT(*) FROM (SELECT 1 AS one FROM users WHERE .. LIMIT 40) subquery_for_count, where the subquery returns the constant 1 for each row that it matches (up to 40) + // + // To solve this, we send an array with a NULL per row that the subquery returned to ensure that unnest(..) returns as many rows as the subquery + // E.g., if the subquery returns 3 rows, we send {NULL,NULL,NULL}, and SELECT 1 FROM unnest($1::pg_catalog._int4) u(c1), which + // returns the same exact response: 1 for each of the 3 rows + if param_refs.is_empty() { + param_refs.push(push_unnest_argument( + mem, + old_bind, + INT4_OID, + "_int4", + std::iter::repeat_n(None, data_rows.len()), + )); + + unnest_column_names.push(mem.make_string(Some("c1")).uncast()); + } + let list_of_param_refs = mem.make_list(param_refs.as_slice()); let unnest_func_call = mem.make_func_call( mem.make_list(&[mem.make_string(Some("unnest")).uncast()]),