static void test_bug20023()
{
  MYSQL con;

  int sql_big_selects_orig= 0;
  /*
    Type of max_join_size is ha_rows, which might be ulong or off_t
    depending on the platform or configure options. Preserve the string
    to avoid type overflow pitfalls.
  */
  char max_join_size_orig[32];

  int sql_big_selects_2= 0;
  int sql_big_selects_3= 0;
  int sql_big_selects_4= 0;
  int sql_big_selects_5= 0;

  char query_buffer[MAX_TEST_QUERY_LENGTH];

  /* Create a new connection. */

  DIE_UNLESS(mysql_client_init(&con));

  DIE_UNLESS(mysql_real_connect(&con,
                                opt_host,
                                opt_user,
                                opt_password,
                                opt_db ? opt_db : "test",
                                opt_port,
                                opt_unix_socket,
                                CLIENT_FOUND_ROWS));

  /***********************************************************************
    Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
  ***********************************************************************/

  query_int_variable(&con,
                     "@@session.sql_big_selects",
                     &sql_big_selects_orig);

  query_str_variable(&con,
                     "@@global.max_join_size",
                     max_join_size_orig,
                     sizeof(max_join_size_orig));

  /***********************************************************************
    Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
  ***********************************************************************/

  /* Issue COM_CHANGE_USER. */

  bug20023_change_user(&con);

  /* Query SQL_BIG_SELECTS. */

  query_int_variable(&con,
                     "@@session.sql_big_selects",
                     &sql_big_selects_2);

  /* Check that SQL_BIG_SELECTS is reset properly. */

  DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);

  /***********************************************************************
    Test that if MAX_JOIN_SIZE set to non-default value,
    SQL_BIG_SELECTS will be 0.
  ***********************************************************************/

  /* Set MAX_JOIN_SIZE to some non-default value. */

  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));

  /* Issue COM_CHANGE_USER. */

  bug20023_change_user(&con);

  /* Query SQL_BIG_SELECTS. */

  query_int_variable(&con,
                     "@@session.sql_big_selects",
                     &sql_big_selects_3);

  /* Check that SQL_BIG_SELECTS is 0. */

  DIE_UNLESS(sql_big_selects_3 == 0);

  /***********************************************************************
    Test that if MAX_JOIN_SIZE set to default value,
    SQL_BIG_SELECTS will be 1.
  ***********************************************************************/

  /* Set MAX_JOIN_SIZE to the default value (2^64-1). */

  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = cast(-1 as unsigned int)"));
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));

  /* Issue COM_CHANGE_USER. */

  bug20023_change_user(&con);

  /* Query SQL_BIG_SELECTS. */

  query_int_variable(&con,
                     "@@session.sql_big_selects",
                     &sql_big_selects_4);

  /* Check that SQL_BIG_SELECTS is 1. */

  DIE_UNLESS(sql_big_selects_4 == 1);

  /***********************************************************************
    Restore MAX_JOIN_SIZE.
    Check that SQL_BIG_SELECTS will be the original one.
  ***********************************************************************/

  /* Restore MAX_JOIN_SIZE. */

  my_snprintf(query_buffer,
           sizeof (query_buffer),
           "SET @@global.max_join_size = %s",
           max_join_size_orig);

  DIE_IF(mysql_query(&con, query_buffer));

  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = cast(-1 as unsigned int)"));
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));

  /* Issue COM_CHANGE_USER. */

  bug20023_change_user(&con);

  /* Query SQL_BIG_SELECTS. */

  query_int_variable(&con,
                     "@@session.sql_big_selects",
                     &sql_big_selects_5);

  /* Check that SQL_BIG_SELECTS is 1. */

  DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);

  /***********************************************************************
    That's it. Cleanup.
  ***********************************************************************/

  mysql_close(&con);
}