Commit f602829c authored by monty@mysql.com's avatar monty@mysql.com

Fix to get correct metadata when using temporary tables to create result. (Bug #2654)

parent 4bc6b551
......@@ -129,7 +129,7 @@ static CHARSET_INFO *charset_info= &my_charset_latin1;
static int embedded_server_arg_count=0;
static char *embedded_server_args[MAX_SERVER_ARGS];
static my_bool display_result_vertically= FALSE;
static my_bool display_result_vertically= FALSE, display_metadata= FALSE;
static const char *embedded_server_groups[] = {
"server",
......@@ -215,6 +215,7 @@ Q_WAIT_FOR_SLAVE_TO_STOP,
Q_REQUIRE_VERSION,
Q_ENABLE_WARNINGS, Q_DISABLE_WARNINGS,
Q_ENABLE_INFO, Q_DISABLE_INFO,
Q_ENABLE_METADATA, Q_DISABLE_METADATA,
Q_EXEC, Q_DELIMITER,
Q_DISPLAY_VERTICAL_RESULTS, Q_DISPLAY_HORIZONTAL_RESULTS,
Q_QUERY_VERTICAL, Q_QUERY_HORIZONTAL,
......@@ -289,6 +290,8 @@ const char *command_names[]=
"disable_warnings",
"enable_info",
"disable_info",
"enable_metadata",
"disable_metadata",
"exec",
"delimiter",
"vertical_results",
......@@ -1677,7 +1680,7 @@ void my_ungetc(int c)
my_bool end_of_query(int c)
{
uint i,j;
uint i;
char tmp[MAX_DELIMITER];
if (c != *delimiter)
......@@ -2220,7 +2223,8 @@ static void append_result(DYNAMIC_STRING *ds, MYSQL_RES *res)
int run_query(MYSQL* mysql, struct st_query* q, int flags)
{
MYSQL_RES* res= 0;
int i, error= 0, err= 0, counter= 0;
uint i;
int error= 0, err= 0, counter= 0;
DYNAMIC_STRING *ds;
DYNAMIC_STRING ds_tmp;
DYNAMIC_STRING eval_query;
......@@ -2347,17 +2351,59 @@ int run_query(MYSQL* mysql, struct st_query* q, int flags)
{
if (res)
{
MYSQL_FIELD *field, *field_end;
uint num_fields= mysql_num_fields(res);
if (display_metadata)
{
dynstr_append(ds,"Catalog\tDatabase\tTable\tTable_alias\tColumn\tColumn_alias\tName\tType\tLength\tMax length\tIs_null\tFlags\tDecimals\n");
for (field= mysql_fetch_fields(res), field_end= field+num_fields ;
field < field_end ;
field++)
{
char buff[22];
dynstr_append_mem(ds, field->catalog, field->catalog_length);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, field->db, field->db_length);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, field->org_table, field->org_table_length);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, field->table, field->table_length);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, field->org_name, field->org_name_length);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, field->name, field->name_length);
dynstr_append_mem(ds, "\t", 1);
int10_to_str((int) field->type, buff, 10);
dynstr_append(ds, buff);
dynstr_append_mem(ds, "\t", 1);
int10_to_str((int) field->length, buff, 10);
dynstr_append(ds, buff);
dynstr_append_mem(ds, "\t", 1);
int10_to_str((int) field->max_length, buff, 10);
dynstr_append(ds, buff);
dynstr_append_mem(ds, "\t", 1);
dynstr_append_mem(ds, (char*) (IS_NOT_NULL(field->flags) ?
"N" : "Y"), 1);
dynstr_append_mem(ds, "\t", 1);
int10_to_str((int) field->flags, buff, 10);
dynstr_append(ds, buff);
dynstr_append_mem(ds, "\t", 1);
int10_to_str((int) field->decimals, buff, 10);
dynstr_append(ds, buff);
dynstr_append_mem(ds, "\n", 1);
}
}
if (!display_result_vertically)
{
int num_fields= mysql_num_fields(res);
MYSQL_FIELD *fields= mysql_fetch_fields(res);
field= mysql_fetch_fields(res);
for (i = 0; i < num_fields; i++)
{
if (i)
dynstr_append_mem(ds, "\t", 1);
replace_dynstr_append_mem(ds, fields[i].name,
strlen(fields[i].name));
replace_dynstr_append_mem(ds, field[i].name,
strlen(field[i].name));
}
dynstr_append_mem(ds, "\n", 1);
}
......@@ -2622,6 +2668,8 @@ int main(int argc, char **argv)
case Q_DISABLE_WARNINGS: disable_warnings=1; break;
case Q_ENABLE_INFO: disable_info=0; break;
case Q_DISABLE_INFO: disable_info=1; break;
case Q_ENABLE_METADATA: display_metadata=1; break;
case Q_DISABLE_METADATA: display_metadata=0; break;
case Q_SOURCE: do_source(q); break;
case Q_SLEEP: do_sleep(q, 0); break;
case Q_REAL_SLEEP: do_sleep(q, 1); break;
......
drop table if exists t1,t2;
select 1, 1.0, -1, "hello", NULL;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def 1 8 1 1 N 32769 0
def 1.0 5 3 3 N 32769 1
def -1 8 1 2 N 32769 0
def hello 254 5 5 N 1 31
def NULL 6 0 0 Y 32768 0
1 1.0 -1 hello NULL
1 1.0 -1 hello NULL
create table t1 (a tinyint, b smallint, c mediumint, d int, e bigint, f float(3,2), g double(4,3), h decimal(5,4), i year, j date, k timestamp, l datetime, m enum('a','b'), n set('a','b'), o char(10));
select * from t1;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def test t1 t1 a a 1 4 0 Y 32768 0
def test t1 t1 b b 2 6 0 Y 32768 0
def test t1 t1 c c 9 9 0 Y 32768 0
def test t1 t1 d d 3 11 0 Y 32768 0
def test t1 t1 e e 8 20 0 Y 32768 0
def test t1 t1 f f 4 3 0 Y 32768 2
def test t1 t1 g g 5 4 0 Y 32768 3
def test t1 t1 h h 0 7 0 Y 32768 4
def test t1 t1 i i 13 4 0 Y 32864 0
def test t1 t1 j j 10 10 0 Y 0 0
def test t1 t1 k k 7 19 0 N 1089 0
def test t1 t1 l l 12 19 0 Y 0 0
def test t1 t1 m m 254 1 0 Y 256 0
def test t1 t1 n n 254 3 0 Y 2048 0
def test t1 t1 o o 254 10 0 Y 0 0
a b c d e f g h i j k l m n o
select a b, b c from t1 as t2;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def test t1 t2 a b 1 4 0 Y 32768 0
def test t1 t2 b c 2 6 0 Y 32768 0
b c
drop table t1;
CREATE TABLE t1 (id tinyint(3) default NULL, data varchar(255) default NULL);
INSERT INTO t1 VALUES (1,'male'),(2,'female');
CREATE TABLE t2 (id tinyint(3) unsigned default NULL, data char(3) default '0');
INSERT INTO t2 VALUES (1,'yes'),(2,'no');
select t1.id, t1.data, t2.data from t1, t2 where t1.id = t2.id;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def test t1 t1 id id 1 3 1 Y 32768 0
def test t1 t1 data data 253 255 6 Y 0 0
def test t2 t2 data data 254 3 3 Y 0 0
id data data
1 male yes
2 female no
select t1.id, t1.data, t2.data from t1, t2 where t1.id = t2.id order by t1.id;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def test t1 t1 id id 1 3 1 Y 32768 0
def test t1 t1 data data 253 255 6 Y 0 0
def test t2 t2 data data 254 3 3 Y 0 0
id data data
1 male yes
2 female no
select t1.id from t1 union select t2.id from t2;
Catalog Database Table Table_alias Column Column_alias Name Type Length Max length Is_null Flags Decimals
def test t1 t1 id id 1 3 1 Y 32768 0
id
1
2
drop table t1,t2;
#
# Test metadata
#
--disable_warnings
drop table if exists t1,t2;
--enable_warnings
--enable_metadata
#
# First some simple tests
#
select 1, 1.0, -1, "hello", NULL;
create table t1 (a tinyint, b smallint, c mediumint, d int, e bigint, f float(3,2), g double(4,3), h decimal(5,4), i year, j date, k timestamp, l datetime, m enum('a','b'), n set('a','b'), o char(10));
select * from t1;
select a b, b c from t1 as t2;
drop table t1;
#
# Test metadata from ORDER BY (Bug #2654)
#
CREATE TABLE t1 (id tinyint(3) default NULL, data varchar(255) default NULL);
INSERT INTO t1 VALUES (1,'male'),(2,'female');
CREATE TABLE t2 (id tinyint(3) unsigned default NULL, data char(3) default '0');
INSERT INTO t2 VALUES (1,'yes'),(2,'no');
select t1.id, t1.data, t2.data from t1, t2 where t1.id = t2.id;
select t1.id, t1.data, t2.data from t1, t2 where t1.id = t2.id order by t1.id;
select t1.id from t1 union select t2.id from t2;
drop table t1,t2;
--disable_metadata
......@@ -412,7 +412,7 @@ select * from t1 where b=2 or b is null order by a;
drop table t1;
#
# Bug #3155
# Bug #3155 - Strange results with index (x, y) ... WHERE ... ORDER BY pk
#
create table t1 (a int not null auto_increment, b int not null, c int not null, d int not null,
......
......@@ -414,7 +414,6 @@ _HOWTO
use Getopt::Long;
use Sys::Hostname;
use IPC::Open3;
#use CGI; #moved to use of CGI by monty
# ****************************
......@@ -527,7 +526,7 @@ if ($MySQLaccess::CMD) { #command-line version
}
}
if ($MySQLaccess::CGI) { #CGI-version
use CGI;
require CGI;
$Q = new CGI;
$Param{'help'} = $Q->param('help') ;
$Param{'host'} = $Q->param('host') || $Q->param('h') || $Param{'host'};
......
......@@ -301,7 +301,8 @@ Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
utype unireg_check_arg, const char *field_name_arg,
struct st_table *table_arg)
:ptr(ptr_arg),null_ptr(null_ptr_arg),
table(table_arg),table_name(table_arg ? table_arg->table_name : 0),
table(table_arg),orig_table(table_arg),
table_name(table_arg ? table_arg->table_name : 0),
field_name(field_name_arg),
query_id(0), key_start(0), part_of_key(0), part_of_sortkey(0),
unireg_check(unireg_check_arg),
......@@ -349,9 +350,10 @@ void Field_num::add_zerofill_and_unsigned(String &res) const
void Field_num::make_field(Send_field *field)
{
/* table_cache_key is not set for temp tables */
field->db_name=table->table_cache_key ? table->table_cache_key : "";
field->org_table_name=table->real_name;
field->table_name=table_name;
field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key :
"");
field->org_table_name= orig_table->real_name;
field->table_name= orig_table->table_name;
field->col_name=field->org_col_name=field_name;
field->charsetnr= charset()->number;
field->length=field_length;
......@@ -364,9 +366,10 @@ void Field_num::make_field(Send_field *field)
void Field_str::make_field(Send_field *field)
{
/* table_cache_key is not set for temp tables */
field->db_name=table->table_cache_key ? table->table_cache_key : "";
field->org_table_name=table->real_name;
field->table_name=table_name;
field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key :
"");
field->org_table_name= orig_table->real_name;
field->table_name= orig_table->table_name;
field->col_name=field->org_col_name=field_name;
field->charsetnr= charset()->number;
field->length=field_length;
......
......@@ -46,6 +46,7 @@ public:
char *ptr; // Position to field in record
uchar *null_ptr; // Byte where null_bit is
struct st_table *table; // Pointer for table
struct st_table *orig_table; // Pointer to original table
const char *table_name,*field_name;
LEX_STRING comment;
ulong query_id; // For quick test of used fields
......
......@@ -904,6 +904,7 @@ TABLE *delayed_insert::get_local_table(THD* client_thd)
{
if (!(*field= (*org_field)->new_field(&client_thd->mem_root,copy)))
return 0;
(*field)->orig_table= copy; // Remove connection
(*field)->move_field(adjust_ptrs); // Point at copy->record[0]
if (*org_field == found_next_number_field)
(*field)->table->found_next_number_field= *field;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment