Page MenuHomePhabricator

Wikimedia\Rdbms\DBQueryError: Error 1054: Unknown column 'articles_added' in 'WHERE'Function: MediaWiki\Pager\IndexPager::buildQueryInfo (MediaWiki\Extension\CampaignEvents\Pager\EventContributionsEditorsPager)
Closed, ResolvedPublicPRODUCTION ERROR

Description

Seen when sorting the view by one of the aggregates and then using pagination. The actual column in the error message depends on the column used for sorting, but here for example we have:

SELECT  cec_user_name,COALESCE(cec_user_name, ""),cec_user_id,cep_private,SUM((CASE WHEN (cec.cec_edit_flags & 1) != 0 THEN 1 ELSE 0 END)) AS `articles_added`,COUNT(DISTINCT (CASE WHEN (cec.cec_edit_flags & 1) = 0 THEN CONCAT(cec.cec_wiki,'|',cec.cec_page_id) ELSE NULL END)) AS `articles_edited`,COUNT(*) AS `edit_count`,SUM(cec_bytes_delta) AS `bytes`  FROM `ce_event_contributions` `cec` JOIN `ce_participants` `cep` ON ((cec.cec_event_id = cep.cep_event_id) AND (cec.cec_user_id = cep.cep_user_id) AND cep.cep_unregistered_at IS NULL)   WHERE cec.cec_event_id = 3112 AND cec.cec_deleted = 0 AND ((cep.cep_private = 0 OR cec.cec_user_id = 47053011)) AND (articles_added > '1' OR (articles_added = '1' AND (cec_user_id > '81237378')))  GROUP BY cec_user_name,COALESCE(cec_user_name, ""),cec_user_id,cep_private ORDER BY articles_added,cec_user_id LIMIT 51

Details

Request URL
https://meta.wikimedia.org/w/index.php?title=Special:EventDetails/3112&module=editors&sort=articles_created&tab=ContributionsPanel&offset=1%7C81237378
Related Changes in Gerrit:

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

This is the same issue as T416569: those names are actually aliases, so they can't be used in WHERE in MySQL. There is a similar error in SQLite:

Wikimedia\Rdbms\DBQueryError : Error 1: misuse of aggregate: SUM()
Function: MediaWiki\Pager\IndexPager::buildQueryInfo (MediaWiki\Extension\CampaignEvents\Pager\EventContributionsEditorsPager)
Query: SELECT  cec_user_name,COALESCE(cec_user_name, ""),cec_user_id,cep_private,SUM((CASE WHEN (cec.cec_edit_flags & 1) != 0 THEN 1 ELSE 0 END)) AS "articles_added",COUNT(DISTINCT (CASE WHEN (cec.cec_edit_flags & 1) = 0 THEN (cec.cec_wiki) || ('|') || (cec.cec_page_id) ELSE NULL END)) AS "articles_edited",COUNT(*) AS "edit_count",SUM(cec_bytes_delta) AS "bytes"  FROM "unittest_ce_event_contributions" "cec" JOIN "unittest_ce_participants" "cep" ON ((cec.cec_event_id = cep.cep_event_id) AND (cec.cec_user_id = cep.cep_user_id) AND cep.cep_unregistered_at IS NULL)   WHERE cec.cec_event_id = 1 AND cec.cec_deleted = 0 AND cep.cep_private = 0 AND (bytes < '99' OR (bytes = '99' AND (cec_user_id < '1')))  GROUP BY cec_user_name,COALESCE(cec_user_name, ""),cec_user_id,cep_private ORDER BY bytes DESC,cec_user_id DESC LIMIT 2

Actually not quite the same, because while in the other task the alias was a simple coalesce, here it's actually an aggregate, so it can't be used in WHERE (which happens when paginating) and needs to be in HAVING instead. So, first of all we hit T308694: Sorting in TablePager doesn't work for aggregates. Maybe we should finally fix that, as it would also let us simplify the code for MyEvents pagination. I have implemented a simple workaround locally by overriding getOffsetCondsAndSortOptions, but it wouldn't pass on postgres as noted in T308694. Also, this table is larger and I don't know if the subquery approach would work well here, performance-wise. Then, even after we fix that, we'd hit T315465: Allow subclasses of IndexPager to process the offsets from the querystring before using them to build SQL: aggregates are integers, but the Pager logic tries to compare them with strings from the URL, which doesn't work in SQLite. That one should be relatively simpler to fix, but it needs a core change.

Change #1269481 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/extensions/CampaignEvents@master] Fix aggregate pagination in EventContributionsEditorsPager

https://gerrit.wikimedia.org/r/1269481

✅ Test done sorting and paginating by articles created (articles_created / articles_added aggregate) in Special:EventDetails Contributions (Editors module), confirming no Wikimedia\Rdbms\DBQueryError: Unknown column 'articles_added' in 'WHERE' occurs.

Change #1269481 merged by jenkins-bot:

[mediawiki/extensions/CampaignEvents@master] Fix aggregate pagination in EventContributionsEditorsPager

https://gerrit.wikimedia.org/r/1269481