diff options
author | Alexander Barkov <bar@mariadb.org> | 2016-12-29 07:40:49 +0400 |
---|---|---|
committer | Alexander Barkov <bar@mariadb.org> | 2016-12-29 07:40:49 +0400 |
commit | f6138883b14989cd4faf81dd4586238f3e4f590e (patch) | |
tree | aad308e59bfc70b81c4249bb96a66ab5583f2de9 /tests | |
parent | d8c695ead448ae36be7b98f586a67e858c270147 (diff) | |
download | mariadb-git-f6138883b14989cd4faf81dd4586238f3e4f590e.tar.gz |
MDEV-11672 mysql_list_field() returns wrong default values for VIEW
The problem happened because Item_ident_for_show::field_type() always
returned MYSQL_TYPE_DOUBLE and ignored the actual data type of the
referenced Field. As a result, the execution always used
Item_ident_for_show::val_real() to send the default value of the field,
so most default values for non-numeric types were displayed as '0'.
This patch:
1. Cleanup:
a. Removes Send_field::charsetnr, as it's been unused since
introduction of Item::charset_for_protocol() in MySQL-5.5.
b. Adds the "const" qualifier to Field::char_length().
This is needed for (5.a), see below.
2. Introduces a new virtual method Type_handler::charset_for_protocol(),
returning item->collation.collation for string data types, or
&my_charset_bin for non-string data types.
3. Changes Item::charset_for_protocol() from virtual to non-virtual.
It now calls type_handler()->charset_for_protocol().
As a good side effect, duplicate code in Item::charset_for_protocol() and
Item_temporal_hybrid_func::charset_for_protocol() is now gone.
4. Fixes Item_ident_for_show::field_type() to correctly return
its data type according to the data type of the referenced field.
This actually fixes the problem reported in MDEV-11672.
Now the default value is sent using a correct method, e.g.
val_str() for VARCHAR/TEXT, or val_int() for INT/BIGINT.
This required additional changes:
a. in DBUG_ASSERT in Protocol::store(const char *,size_t,CHARSET_INFO),
This method is now used by mysqld_list_fields(), which
(unlike normal SELECT queries) does not set
field_types/field_pos/field_count.
b. Item_ident_for_show::Item_ident_for_show() now set standard attributes
(collation, decimals, max_length, unsigned_flag) according to the
referenced field, to make charset_for_protocol() return the correct
value and to make mysqld_list_fields() correctly send default
values.
5. In order to share the code between Item_field::set_field() and
Item_ident_for_show::Item_ident_for_show():
a. Introduces a new method Type_std_attributes::set(const Field*)
b. To make (a) possible, moves Item::fix_char_length() from Item
to Type_std_attributes, also moves char_to_byte_length_safe()
from item.h to sql_type.h
c. Additionally, moves Item::fix_length_and_charset() and
Item::max_char_length() from Item to Type_std_attributes.
This is not directly needed for the fix and is done just for symmetry
with fix_char_length(), as these three methods are directly related
to each other.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mysql_client_test.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c index 58e3dda2b0d..a1c28f0d750 100644 --- a/tests/mysql_client_test.c +++ b/tests/mysql_client_test.c @@ -8398,6 +8398,76 @@ static void test_list_fields() } +static void test_list_fields_default() +{ + int rc, i; + myheader("test_list_fields_default"); + + rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1"); + myquery(rc); + + rc= mysql_query(mysql, + "CREATE TABLE t1 (" + " i1 INT NOT NULL DEFAULT 0," + " i3 BIGINT UNSIGNED NOT NULL DEFAULT 0xFFFFFFFFFFFFFFFF," + " s1 VARCHAR(10) CHARACTER SET latin1 NOT NULL DEFAULT 's1def'," + " d1 DECIMAL(31,1) NOT NULL DEFAULT 111111111122222222223333333333.9," + " t1 DATETIME(6) NOT NULL DEFAULT '2001-01-01 10:20:30.123456'," + " e1 ENUM('a','b') NOT NULL DEFAULT 'a'" + ")"); + myquery(rc); + + rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1"); + myquery(rc); + + rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1"); + myquery(rc); + + /* + Checking that mysql_list_fields() returns the same result + for a TABLE and a VIEW on the same table. + */ + for (i= 0; i < 2; i++) + { + const char *table_name= i == 0 ? "t1" : "v1"; + MYSQL_RES *result= mysql_list_fields(mysql, table_name, NULL); + mytest(result); + + rc= my_process_result_set(result); + DIE_UNLESS(rc == 0); + + verify_prepare_field(result, 0, "i1", "i1", MYSQL_TYPE_LONG, + table_name, table_name, current_db, + 11, "0"); + + verify_prepare_field(result, 1, "i3", "i3", MYSQL_TYPE_LONGLONG, + table_name, table_name, current_db, + 20, "18446744073709551615"); + + verify_prepare_field(result, 2, "s1", "s1", MYSQL_TYPE_VAR_STRING, + table_name, table_name, current_db, + 10, "s1def"); + + verify_prepare_field(result, 3, "d1", "d1", MYSQL_TYPE_NEWDECIMAL, + table_name, table_name, current_db, + 33, "111111111122222222223333333333.9"); + + verify_prepare_field(result, 4, "t1", "t1", MYSQL_TYPE_DATETIME, + table_name, table_name, current_db, + 26, "2001-01-01 10:20:30.123456"); + + verify_prepare_field(result, 5, "e1", "e1", MYSQL_TYPE_STRING, + table_name, table_name, current_db, + 1, "a"); + + mysql_free_result(result); + } + + myquery(mysql_query(mysql, "DROP VIEW v1")); + myquery(mysql_query(mysql, "DROP TABLE t1")); +} + + static void test_bug19671() { MYSQL_RES *result; @@ -19558,6 +19628,7 @@ static struct my_tests_st my_tests[]= { { "test_fetch_column", test_fetch_column }, { "test_mem_overun", test_mem_overun }, { "test_list_fields", test_list_fields }, + { "test_list_fields_default", test_list_fields_default }, { "test_free_result", test_free_result }, { "test_free_store_result", test_free_store_result }, { "test_sqlmode", test_sqlmode }, |