Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions integration/rust/tests/integration/subqueries/tests/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<sq>) s, <sq> 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 (<sq>) s, <sq> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()]),
Expand Down
Loading