Commit 452a1331 authored by brian@zim.(none)'s avatar brian@zim.(none)

The CSV format has always relied on numbers being quoted, which doesn't always...

The CSV format has always relied on numbers being quoted, which doesn't always happen. This fixes that so that numbers can now be unquoted (and the output does this as well so that the log takes up less space). 
parent 6ff70926
...@@ -5210,16 +5210,32 @@ create table bug22080_3 (id int,string varchar(64)) Engine=CSV; ...@@ -5210,16 +5210,32 @@ create table bug22080_3 (id int,string varchar(64)) Engine=CSV;
insert into bug22080_1 values(1,'string'); insert into bug22080_1 values(1,'string');
insert into bug22080_1 values(2,'string'); insert into bug22080_1 values(2,'string');
insert into bug22080_1 values(3,'string'); insert into bug22080_1 values(3,'string');
"1","string" 1,"string"
2","string" 2","string"
"3","string" 3,"string"
check table bug22080_2; check table bug22080_2;
Table Op Msg_type Msg_text Table Op Msg_type Msg_text
test.bug22080_2 check error Corrupt test.bug22080_2 check error Corrupt
"1","string" 1,"string"
"2",string" 2,"string"
"3","string" 3,"string"
check table bug22080_3; check table bug22080_3;
Table Op Msg_type Msg_text Table Op Msg_type Msg_text
test.bug22080_3 check error Corrupt test.bug22080_3 check error Corrupt
drop tables bug22080_1,bug22080_2,bug22080_3; drop tables bug22080_1,bug22080_2,bug22080_3;
create table float_test (id float,string varchar(64)) Engine=CSV;
insert into float_test values(1.0,'string');
insert into float_test values(2.23,'serg.g');
insert into float_test values(0.03,'string');
insert into float_test values(0.19,'string');
insert into float_test values(.67,'string');
insert into float_test values(9.67,'string');
select * from float_test;
id string
1 string
2.23 serg.g
0.03 string
0.19 string
0.67 string
9.67 string
drop table float_test;
...@@ -1595,7 +1595,7 @@ insert into bug22080_1 values(2,'string'); ...@@ -1595,7 +1595,7 @@ insert into bug22080_1 values(2,'string');
insert into bug22080_1 values(3,'string'); insert into bug22080_1 values(3,'string');
# Currupt the file as described in the bug report # Currupt the file as described in the bug report
--exec sed -e 's/"2"/2"/' $MYSQLTEST_VARDIR/master-data/test/bug22080_1.CSV > $MYSQLTEST_VARDIR/master-data/test/bug22080_2.CSV --exec sed -e 's/2/2"/' $MYSQLTEST_VARDIR/master-data/test/bug22080_1.CSV > $MYSQLTEST_VARDIR/master-data/test/bug22080_2.CSV
--exec sed -e 's/2","/2",/' $MYSQLTEST_VARDIR/master-data/test/bug22080_1.CSV > $MYSQLTEST_VARDIR/master-data/test/bug22080_3.CSV --exec sed -e 's/2","/2",/' $MYSQLTEST_VARDIR/master-data/test/bug22080_1.CSV > $MYSQLTEST_VARDIR/master-data/test/bug22080_3.CSV
--exec cat $MYSQLTEST_VARDIR/master-data/test/bug22080_2.CSV --exec cat $MYSQLTEST_VARDIR/master-data/test/bug22080_2.CSV
...@@ -1605,3 +1605,17 @@ check table bug22080_2; ...@@ -1605,3 +1605,17 @@ check table bug22080_2;
check table bug22080_3; check table bug22080_3;
drop tables bug22080_1,bug22080_2,bug22080_3; drop tables bug22080_1,bug22080_2,bug22080_3;
#
# Testing float type
#
create table float_test (id float,string varchar(64)) Engine=CSV;
insert into float_test values(1.0,'string');
insert into float_test values(2.23,'serg.g');
insert into float_test values(0.03,'string');
insert into float_test values(0.19,'string');
insert into float_test values(.67,'string');
insert into float_test values(9.67,'string');
select * from float_test;
drop table float_test;
...@@ -462,6 +462,7 @@ int ha_tina::encode_quote(byte *buf) ...@@ -462,6 +462,7 @@ int ha_tina::encode_quote(byte *buf)
my_bitmap_map *org_bitmap= dbug_tmp_use_all_columns(table, table->read_set); my_bitmap_map *org_bitmap= dbug_tmp_use_all_columns(table, table->read_set);
buffer.length(0); buffer.length(0);
for (Field **field=table->field ; *field ; field++) for (Field **field=table->field ; *field ; field++)
{ {
const char *ptr; const char *ptr;
...@@ -478,50 +479,58 @@ int ha_tina::encode_quote(byte *buf) ...@@ -478,50 +479,58 @@ int ha_tina::encode_quote(byte *buf)
buffer.append(STRING_WITH_LEN("\"\",")); buffer.append(STRING_WITH_LEN("\"\","));
continue; continue;
} }
else
(*field)->val_str(&attribute,&attribute);
if ((*field)->str_needs_quotes())
{ {
(*field)->val_str(&attribute,&attribute);
ptr= attribute.ptr(); ptr= attribute.ptr();
end_ptr= attribute.length() + ptr; end_ptr= attribute.length() + ptr;
}
buffer.append('"'); buffer.append('"');
while (ptr < end_ptr) while (ptr < end_ptr)
{
if (*ptr == '"')
{
buffer.append('\\');
buffer.append('"');
*ptr++;
}
else if (*ptr == '\r')
{
buffer.append('\\');
buffer.append('r');
*ptr++;
}
else if (*ptr == '\\')
{ {
buffer.append('\\'); if (*ptr == '"')
buffer.append('\\'); {
*ptr++; buffer.append('\\');
} buffer.append('"');
else if (*ptr == '\n') *ptr++;
{ }
buffer.append('\\'); else if (*ptr == '\r')
buffer.append('n'); {
*ptr++; buffer.append('\\');
buffer.append('r');
*ptr++;
}
else if (*ptr == '\\')
{
buffer.append('\\');
buffer.append('\\');
*ptr++;
}
else if (*ptr == '\n')
{
buffer.append('\\');
buffer.append('n');
*ptr++;
}
else
buffer.append(*ptr++);
} }
else buffer.append('"');
buffer.append(*ptr++); }
else
{
buffer.append(attribute);
} }
buffer.append('"');
buffer.append(','); buffer.append(',');
} }
// Remove the comma, add a line feed // Remove the comma, add a line feed
buffer.length(buffer.length() - 1); buffer.length(buffer.length() - 1);
buffer.append('\n'); buffer.append('\n');
//buffer.replace(buffer.length(), 0, "\n", 1); //buffer.replace(buffer.length(), 0, "\n", 1);
dbug_tmp_restore_column_map(table->read_set, org_bitmap); dbug_tmp_restore_column_map(table->read_set, org_bitmap);
...@@ -601,47 +610,72 @@ int ha_tina::find_current_row(byte *buf) ...@@ -601,47 +610,72 @@ int ha_tina::find_current_row(byte *buf)
buffer.length(0); buffer.length(0);
if (curr_offset < end_offset && if (curr_offset < end_offset &&
file_buff->get_value(curr_offset) == '"') file_buff->get_value(curr_offset) == '"')
curr_offset++; // Incrementpast the first quote
else
goto err;
for(;curr_offset < end_offset; curr_offset++)
{ {
// Need to convert line feeds! curr_offset++; // Incrementpast the first quote
if (file_buff->get_value(curr_offset) == '"' &&
((file_buff->get_value(curr_offset + 1) == ',') || for(;curr_offset < end_offset; curr_offset++)
(curr_offset == end_offset -1 )))
{
curr_offset+= 2; // Move past the , and the "
break;
}
if (file_buff->get_value(curr_offset) == '\\' &&
curr_offset != (end_offset - 1))
{ {
curr_offset++; // Need to convert line feeds!
if (file_buff->get_value(curr_offset) == 'r') if (file_buff->get_value(curr_offset) == '"' &&
buffer.append('\r'); ((file_buff->get_value(curr_offset + 1) == ',') ||
else if (file_buff->get_value(curr_offset) == 'n' ) (curr_offset == end_offset -1 )))
buffer.append('\n');
else if ((file_buff->get_value(curr_offset) == '\\') ||
(file_buff->get_value(curr_offset) == '"'))
buffer.append(file_buff->get_value(curr_offset));
else /* This could only happed with an externally created file */
{ {
buffer.append('\\'); curr_offset+= 2; // Move past the , and the "
break;
}
if (file_buff->get_value(curr_offset) == '\\' &&
curr_offset != (end_offset - 1))
{
curr_offset++;
if (file_buff->get_value(curr_offset) == 'r')
buffer.append('\r');
else if (file_buff->get_value(curr_offset) == 'n' )
buffer.append('\n');
else if ((file_buff->get_value(curr_offset) == '\\') ||
(file_buff->get_value(curr_offset) == '"'))
buffer.append(file_buff->get_value(curr_offset));
else /* This could only happed with an externally created file */
{
buffer.append('\\');
buffer.append(file_buff->get_value(curr_offset));
}
}
else // ordinary symbol
{
/*
We are at final symbol and no last quote was found =>
we are working with a damaged file.
*/
if (curr_offset == end_offset - 1)
goto err;
buffer.append(file_buff->get_value(curr_offset)); buffer.append(file_buff->get_value(curr_offset));
} }
} }
else // ordinary symbol }
else if (my_isdigit(system_charset_info,
file_buff->get_value(curr_offset)))
{
for(;curr_offset < end_offset; curr_offset++)
{ {
/* if (file_buff->get_value(curr_offset) == ',')
We are at final symbol and no last quote was found => {
we are working with a damaged file. curr_offset+= 1; // Move past the ,
*/ break;
if (curr_offset == end_offset - 1) }
if (my_isdigit(system_charset_info, file_buff->get_value(curr_offset)))
buffer.append(file_buff->get_value(curr_offset));
else if (file_buff->get_value(curr_offset) == '.')
buffer.append(file_buff->get_value(curr_offset));
else
goto err; goto err;
buffer.append(file_buff->get_value(curr_offset));
} }
} }
else
{
goto err;
}
if (bitmap_is_set(table->read_set, (*field)->field_index)) if (bitmap_is_set(table->read_set, (*field)->field_index))
(*field)->store(buffer.ptr(), buffer.length(), system_charset_info); (*field)->store(buffer.ptr(), buffer.length(), system_charset_info);
} }
......
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