Page MenuHomePhabricator

Can't retrieve data from field with table alias
Closed, ResolvedPublic

Description

Fields from an aliased table can't be read into data.

Consider following query:

{{#get_db_data:db=bugs
|from=heat=h,profiles=p 
|join on=h.assigned_to=p.userid
|data=loginName=p.login_name,rate=count(*),heat=sum(h.heat)
|group by=p.login_name
}}
{| class="prettytable sortable" width="100%"
! Assigned to
! Count
! Sum
{{#for_external_table:<nowiki/>
{{!}}- style="background-color:#EEEEEE;"
{{!}} {{{loginName}}}
{{!}} {{{rate}}}
{{!}} {{{heat}}}
}}
|}
{{#clear_external_data:}}

The resulting table will hold empty entries for column loginName. If the assignment is changed to |data=loginName=login_name, the column is filled correctly.

While this is a doable workaround for some queries, this fails, if two tables containing identical column names need to be joined:

  • Giving an alias in data will result in an empty column
  • Removing the alias will fail the query due to ambiguous names

I've noticed this behavior, while migrating a MediaWiki 1.26 to 1.36. In 1.26 the queries can retrieve the data from the aliased fields. The breaking changes seem to have been introduced with REL1_31.

Event Timeline

Can you reproduce undesired behaviour on this database?

Can you reproduce undesired behaviour on this database?

I'm behind a corporate proxy, which follows a restrictive policy for outgoing connections. Thus I can't access this database. Sorry.

Described behavior shouldn't be limited to my database. A similar issue has been reported on the extension's discussion page.

I can give an example for the wiki-db itself:

External data configuration

$edgDBServer['wiki'] = getenv('WIKI_DATABASE_HOST');
$edgDBServerType['wiki'] = "mysql"; 
$edgDBName['wiki'] =  getenv('WIKI_DATABASE');
$edgDBUser['wiki'] =  getenv('WIKI_DATABASE_USER');
$edgDBPass['wiki'] =  getenv('WIKI_DATABASE_PASSWORD');

Query to show latest revision of a page

{{#get_db_data:
db=wiki
|from=page=p,revision=r
|join on=p.page_id=r.rev_page
|where=p.page_latest=r.rev_id
|data=title=p.page_title,latest=r.rev_id
|limit=100
}}
{| class="prettytable sortable" width="100%"
! Title
! Latest Rev
{{#for_external_table:<nowiki/>
{{!}}- style="background-color:#EEEEEE;"
{{!}} {{{title}}}
{{!}} {{{latest}}}
}}
|}

With the aliases, a table with empty rows and columns is shown.

I can see identical behavior with External Data 2.3 and 2.4.1.

alex-mashin triaged this task as Unbreak Now! priority.

Change 709670 had a related patch set uploaded (by Alex Mashin; author: mashin):

[mediawiki/extensions/ExternalData@master] Enable table {{#get_db_data:}} to work with table prefixes, including aliases, in |data= param.

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

Awesome @alex-mashin!

Current master patched with your set works.

Thanks for the quick fix!

Change 709670 merged by jenkins-bot:

[mediawiki/extensions/ExternalData@master] Enable table {{#get_db_data:}} to work with table prefixes, including aliases, in |data= param.

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