-
-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MBS-9253: List EP release groups above singles #3387
Draft
reosarevok
wants to merge
1
commit into
metabrainz:master
Choose a base branch
from
reosarevok:MBS-9253
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
\set ON_ERROR_STOP 1 | ||
|
||
BEGIN; | ||
|
||
CREATE OR REPLACE FUNCTION get_artist_release_group_rows( | ||
release_group_id INTEGER | ||
) RETURNS SETOF artist_release_group AS $$ | ||
BEGIN | ||
-- PostgreSQL 12 generates a vastly more efficient plan when only | ||
-- one release group ID is passed. A condition like | ||
-- `rg.id = any(...)` can be over 200x slower, even with only one | ||
-- release group ID in the array. | ||
RETURN QUERY EXECUTE $SQL$ | ||
SELECT DISTINCT ON (a_rg.artist, rg.id) | ||
a_rg.is_track_artist, | ||
a_rg.artist, | ||
-- Withdrawn releases were once official by definition | ||
bool_and(r.status IS NOT NULL AND r.status != 1 AND r.status != 5), | ||
(CASE | ||
WHEN rg.type = 3 THEN 2 -- Sort EPs above singles | ||
WHEN rg.type = 2 THEN 3 -- Sort singles below EPs | ||
ELSE rg.type | ||
END | ||
)::SMALLINT, | ||
array_agg( | ||
DISTINCT st.secondary_type ORDER BY st.secondary_type) | ||
FILTER (WHERE st.secondary_type IS NOT NULL | ||
)::SMALLINT[], | ||
integer_date( | ||
rgm.first_release_date_year, | ||
rgm.first_release_date_month, | ||
rgm.first_release_date_day | ||
), | ||
left(rg.name, 1)::CHAR(1), | ||
rg.id | ||
FROM ( | ||
SELECT FALSE AS is_track_artist, rgacn.artist, rg.id AS release_group | ||
FROM release_group rg | ||
JOIN artist_credit_name rgacn ON rgacn.artist_credit = rg.artist_credit | ||
UNION ALL | ||
SELECT TRUE AS is_track_artist, tacn.artist, r.release_group | ||
FROM release r | ||
JOIN medium m ON m.release = r.id | ||
JOIN track t ON t.medium = m.id | ||
JOIN artist_credit_name tacn ON tacn.artist_credit = t.artist_credit | ||
) a_rg | ||
JOIN release_group rg ON rg.id = a_rg.release_group | ||
LEFT JOIN release r ON r.release_group = rg.id | ||
JOIN release_group_meta rgm ON rgm.id = rg.id | ||
LEFT JOIN release_group_secondary_type_join st ON st.release_group = rg.id | ||
$SQL$ || (CASE WHEN release_group_id IS NULL THEN '' ELSE 'WHERE rg.id = $1' END) || | ||
$SQL$ | ||
GROUP BY a_rg.is_track_artist, a_rg.artist, rgm.id, rg.id | ||
ORDER BY a_rg.artist, rg.id, a_rg.is_track_artist | ||
$SQL$ | ||
USING release_group_id; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- We update the table for any existing RGs of type Single or EP | ||
-- with this one-off script; the updated function will keep it up after this. | ||
DO $$ | ||
DECLARE | ||
release_group_ids INTEGER[]; | ||
release_group_id INTEGER; | ||
BEGIN | ||
SELECT array_agg(DISTINCT rg.id) | ||
INTO release_group_ids | ||
FROM release_group rg | ||
WHERE rg.type = 2 OR rg.type = 3; -- Single or EP | ||
|
||
IF coalesce(array_length(release_group_ids, 1), 0) > 0 THEN | ||
-- If the user hasn't generated `artist_release_group`, then we | ||
-- shouldn't update or insert to it. MBS determines whether to | ||
-- use this table based on it being non-empty, so a partial | ||
-- table would manifest as partial data on the website and | ||
-- webservice. | ||
PERFORM 1 FROM artist_release_group LIMIT 1; | ||
IF FOUND THEN | ||
DELETE FROM artist_release_group WHERE release_group = any(release_group_ids); | ||
|
||
FOREACH release_group_id IN ARRAY release_group_ids LOOP | ||
-- We handle each release group ID separately because | ||
-- the `get_artist_release_group_rows` query can be | ||
-- planned much more efficiently that way. | ||
INSERT INTO artist_release_group | ||
SELECT * FROM get_artist_release_group_rows(release_group_id); | ||
END LOOP; | ||
END IF; | ||
END IF; | ||
END $$; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
-- Generated by CompileSchemaScripts.pl from: | ||
-- 20241017-mbs-9253.sql | ||
\set ON_ERROR_STOP 1 | ||
BEGIN; | ||
SET search_path = musicbrainz, public; | ||
SET LOCAL statement_timeout = 0; | ||
-------------------------------------------------------------------------------- | ||
SELECT '20241017-mbs-9253.sql'; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION get_artist_release_group_rows( | ||
release_group_id INTEGER | ||
) RETURNS SETOF artist_release_group AS $$ | ||
BEGIN | ||
-- PostgreSQL 12 generates a vastly more efficient plan when only | ||
-- one release group ID is passed. A condition like | ||
-- `rg.id = any(...)` can be over 200x slower, even with only one | ||
-- release group ID in the array. | ||
RETURN QUERY EXECUTE $SQL$ | ||
SELECT DISTINCT ON (a_rg.artist, rg.id) | ||
a_rg.is_track_artist, | ||
a_rg.artist, | ||
-- Withdrawn releases were once official by definition | ||
bool_and(r.status IS NOT NULL AND r.status != 1 AND r.status != 5), | ||
(CASE | ||
WHEN rg.type = 3 THEN 2 -- Sort EPs above singles | ||
WHEN rg.type = 2 THEN 3 -- Sort singles below EPs | ||
ELSE rg.type | ||
END | ||
)::SMALLINT, | ||
array_agg( | ||
DISTINCT st.secondary_type ORDER BY st.secondary_type) | ||
FILTER (WHERE st.secondary_type IS NOT NULL | ||
)::SMALLINT[], | ||
integer_date( | ||
rgm.first_release_date_year, | ||
rgm.first_release_date_month, | ||
rgm.first_release_date_day | ||
), | ||
left(rg.name, 1)::CHAR(1), | ||
rg.id | ||
FROM ( | ||
SELECT FALSE AS is_track_artist, rgacn.artist, rg.id AS release_group | ||
FROM release_group rg | ||
JOIN artist_credit_name rgacn ON rgacn.artist_credit = rg.artist_credit | ||
UNION ALL | ||
SELECT TRUE AS is_track_artist, tacn.artist, r.release_group | ||
FROM release r | ||
JOIN medium m ON m.release = r.id | ||
JOIN track t ON t.medium = m.id | ||
JOIN artist_credit_name tacn ON tacn.artist_credit = t.artist_credit | ||
) a_rg | ||
JOIN release_group rg ON rg.id = a_rg.release_group | ||
LEFT JOIN release r ON r.release_group = rg.id | ||
JOIN release_group_meta rgm ON rgm.id = rg.id | ||
LEFT JOIN release_group_secondary_type_join st ON st.release_group = rg.id | ||
$SQL$ || (CASE WHEN release_group_id IS NULL THEN '' ELSE 'WHERE rg.id = $1' END) || | ||
$SQL$ | ||
GROUP BY a_rg.is_track_artist, a_rg.artist, rgm.id, rg.id | ||
ORDER BY a_rg.artist, rg.id, a_rg.is_track_artist | ||
$SQL$ | ||
USING release_group_id; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- We update the table for any existing RGs of type Single or EP | ||
-- with this one-off script; the updated function will keep it up after this. | ||
DO $$ | ||
DECLARE | ||
release_group_ids INTEGER[]; | ||
release_group_id INTEGER; | ||
BEGIN | ||
SELECT array_agg(DISTINCT rg.id) | ||
INTO release_group_ids | ||
FROM release_group rg | ||
WHERE rg.type = 2 OR rg.type = 3; -- Single or EP | ||
|
||
IF coalesce(array_length(release_group_ids, 1), 0) > 0 THEN | ||
-- If the user hasn't generated `artist_release_group`, then we | ||
-- shouldn't update or insert to it. MBS determines whether to | ||
-- use this table based on it being non-empty, so a partial | ||
-- table would manifest as partial data on the website and | ||
-- webservice. | ||
PERFORM 1 FROM artist_release_group LIMIT 1; | ||
IF FOUND THEN | ||
DELETE FROM artist_release_group WHERE release_group = any(release_group_ids); | ||
|
||
FOREACH release_group_id IN ARRAY release_group_ids LOOP | ||
-- We handle each release group ID separately because | ||
-- the `get_artist_release_group_rows` query can be | ||
-- planned much more efficiently that way. | ||
INSERT INTO artist_release_group | ||
SELECT * FROM get_artist_release_group_rows(release_group_id); | ||
END LOOP; | ||
END IF; | ||
END IF; | ||
END $$; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,5 +230,10 @@ | |
"20240223-mbs-13421-fks.sql", | ||
"20240319-mbs-13514.sql" | ||
] | ||
}, | ||
"30": { | ||
"all": [ | ||
"20241017-mbs-9253.sql" | ||
] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with your solution here as it's pretty straightforward, though I wondered if we should be using
release_group_primary_type.child_order
for this at first. Unless we want the general order of the types in a list to differ from the discography display order? (It would also require a new trigger onrelease_group_primary_type
for when thechild_order
changes.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use that I guess, yes - it never even crossed my mind since I never think about it at all... Wouldn't that be more annoying though since it'd require new triggers and a join here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it'd also require one more join. The advantage would be that we could tweak the order again in the future without a schema change. (My only worry would be the trigger taking forever due to the number of rows needing to be updated after a
child_order
change. We could do it from psql withstatement_timeout
disabled, but it might time out from the UI.) Let's see what @yvanzo thinks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh I think further order changes are unlikely enough that there might not be a point here - we rarely add primary types and I cannot think of any that would require special sorting.
For secondary types that might actually be useful though, if we decide to implement a specific order for those (MBS-13790).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that seems obvious:
child_order
is purposed for ordering, nottype
.I’m not sure what trigger(s) you are speaking about.
About search, neither
type
norchild_order
are looked after, onlygid
andname
, so there won’t be any issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Triggers to update
artist_release_group
wheneverrelease_group_primary_type
changes, since that means we need that data in here for the sort (and that means we also need an extra join here, of course). I don't expect to change thechild_order
often so it might be okay though 🤷♂️