[MDEV-18685] mysql_list_fields() returns DEFAULT 0 instead of DEFAULT NULL for view columns Created: 2019-02-21  Updated: 2019-04-02

Status: Open
Project: MariaDB Server
Component/s: Scripts & Clients, Views
Affects Version/s: 5.5, 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4

Type: Bug Priority: Minor
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-18408 Assertion `0' failed in Item::val_nat... Closed

 Description   

I run this script:

CREATE OR REPLACE TABLE t1 (a INT DEFAULT NULL);
CREATE OR REPLACE VIEW v1 AS SELECT a FROM t1;

and then execute this client program:

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
 
 
int main(int argc, char **argv)
{
  MYSQL mysql,*sock;
  MYSQL_RES *res;
  MYSQL_FIELD *sql_field;
 
  mysql_init(&mysql);
  if (!(sock = mysql_real_connect(&mysql, "localhost", "root", NULL,
                                  "test", 0, "/tmp/mysql.sock", 0)))
  {
    fprintf(stderr,"mysql_real_connect failed: %s\n",mysql_error(&mysql));
    return 1;
  }
 
  res= mysql_list_fields(&mysql, "v1", NULL);
  if (!res)
  {
    fprintf(stderr, "res is NULL\n");
    return 1;
  }
 
  while ((sql_field= mysql_fetch_field(res)))
  {
    printf("DB: %s Table: %s Column name: %s Type: %d Default: %s Decimals: %d\n",
          sql_field->db,
          sql_field->table,
          sql_field->name,
          sql_field->type,
          sql_field->def,
          sql_field->decimals);
  }
 
  mysql_free_result(res);
  mysql_close(sock);
 
  return 0;
}

It produces the following output:

DB: test Table: v1 Column name: a Type: 3 Default: 0 Decimals: 0

Notice, Default is erroneously displayed as 0. The expected result is NULL.

The problem resides in Item_ident_for_show::val_int() which does not check field->is_null() before calling field->val_int().
All other value methods, e.g. val_real(), val_decimal(), etc, have the same problem. So a wrong DEFAULT is repeatable with all other data types.


Generated at Thu Feb 08 08:45:57 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.