Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-18685

mysql_list_fields() returns DEFAULT 0 instead of DEFAULT NULL for view columns

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Minor
    • Resolution: Unresolved
    • 5.5, 10.1, 10.2, 10.3, 10.4
    • 10.4
    • Scripts & Clients, Views
    • None

    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.

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.