[MDEV-22064] result_metadata()->fetch_fields() returns incorrect orgname and orgtable for select statements with subquery in FROM clause Created: 2020-03-27  Updated: 2020-05-04  Resolved: 2020-05-04

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 5.5.64, 10.3.22
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Henry Olson Assignee: Unassigned
Resolution: Incomplete Votes: 0
Labels: need_feedback
Environment:

CentOS Linux release 7.7.1908
PHP 7.3.16



 Description   

The statement metadata does not correctly identify the underlying table and column. This is a regression vs e.g. MySQL 5.1.73.

This behavior has been reproduced in several server environments and also with the mariadb client for nodejs.

Impact: Cannot determine application-level metadata and security attributes for the tables and columns queried.

Repro (PHP):

<?php
 
/*
 * connect parameters
 */
$host = 'localhost';
$id = 'holson';
$pwd = 'xxxxxxxx';
$dbname = 'alias';
 
/*
 * connect
 */
$link = @mysqli_connect($host, $id, $pwd);
if ($link === false) {
    echo 'unable to connect to database - incorrect connect parametsrs?' . PHP_EOL;
    exit(1);
}
mysqli_select_db($link, $dbname);
 
/*
 * prepare database
 */
$ddl = "drop table if exists Site";
$r = mysqli_query($link, $ddl);
$ddl = "create table Site (Name varchar(50))";
$r = mysqli_query($link, $ddl);
 
/*
 * execute test case
 */
$sql="
    select
        SiteName
    from (select Name as SiteName from Site) as s
";
echo 'DDL: ' . $ddl . PHP_EOL;
echo 'SQL: ' . $sql . PHP_EOL;
echo 'Expect: orgname => Name, orgtable => Site'. PHP_EOL;
$stmt = mysqli_prepare($link, $sql);
$fields = $stmt->result_metadata()->fetch_fields();
echo 'Result ( $stmt->result_metadata()->fetch_fields() ): ';
print_r($fields);
 
?>



 Comments   
Comment by Elena Stepanova [ 2020-04-05 ]

I am getting orgname => SiteName everywhere – MySQL 5.1.73, 5.5.62, MariaDB 5.5.67, MariaDB 10.3.22.
orgtable is indeed different, orgtable => Site with MySQL, orgtable => s with MariaDB.
Apparently it is related to MariaDB optimizing the query. If you want the old behavior, you can set optimizer_switch=derived_merge=off.

Generated at Thu Feb 08 09:11:55 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.