Bug #60386 mysql_list_fields() returns wrong charset for char/varchar/text columns in views
Submitted: 8 Mar 2011 8:23 Modified: 11 Aug 2011 12:10
Reporter: Bogdan Degtyariov Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Data Types Severity:S2 (Serious)
Version:5.5.9 OS:Any
Assigned to: CPU Architecture:Any
Tags: mysql_list_fields

[8 Mar 2011 8:23] Bogdan Degtyariov
Description:
MySQL Server 5.5 sets BINARY (63) character set for char/varchar/text columns in views. It is not the client library because the same test reads correct charsets from views in MySQL 5.1.

The test program can be linked using 5.1 and 5.5 client library with the same result: mysql_list_fields() gets BINARY charsetnr for columns in view, whereas columns in the original table are Latin1.

How to repeat:
/* Test case is small, so uploading it as text message */
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>

static void test_error(MYSQL *mysql, int status)
{
  if (status)
  {
    printf("Error: %s (errno: %d)\n",
           mysql_error(mysql), mysql_errno(mysql));
    exit(1);
  }
}

int main()
{
  MYSQL *mysql,*sock;
  MYSQL_RES *res;
  MYSQL_FIELD *field;
  unsigned int tab_charsetnr= 0;

  mysql= mysql_init(NULL);

  if (!(sock = mysql_real_connect(mysql, "localhost", "root", "", 
                                  "test", 3306, NULL, 0)))
  {
    printf("Couldn't connect to engine!\n%s\n\n", mysql_error(mysql));
    exit(1);
  }

  test_error(sock, mysql_query(sock, "drop table if exists char_tab"));
  test_error(sock, mysql_query(sock, "create table char_tab(txt varchar(32)"\
                                     "character set Latin1)"));

  test_error(sock, mysql_query(sock, "drop view if exists char_view"));
  test_error(sock, mysql_query(sock, "create view char_view as "\
                                     "select * from char_tab;"));

  /*
  Checking the table
  */
  res= mysql_list_fields(sock, "char_tab", NULL);
  while(field = mysql_fetch_field(res))
  {
      printf("field name %s\n", field->name);
      printf("field table %s\n", field->table);
      printf("field type %d\n", field->type);
      printf("field charset %d\n", field->charsetnr);
      tab_charsetnr= field->charsetnr;
      printf("\n\n");
  }
  mysql_free_result(res);

  /*
  Checking the view
  */
  res= mysql_list_fields(sock, "char_view", NULL);
  while(field = mysql_fetch_field(res))
  {
      printf("field name %s\n", field->name);
      printf("field table %s\n", field->table);
      printf("field type %d\n", field->type);
      printf("field charset %d\n", field->charsetnr);
      if(field->charsetnr != tab_charsetnr)
          printf("Error charset! Must be the same as for table %d!\n", 
                 tab_charsetnr);

      printf("\n\n");
  }
  mysql_free_result(res);

  mysql_close(sock);
  return 0;
}
[11 Aug 2011 12:10] Bogdan Degtyariov
Fixed in MySQL 5.5.15.

The test program displays:

field name txt
field table char_tab
field type 253
field charset 8

field name txt
field table char_view
field type 253
field charset 8

Closing the bug