--disable_warnings
drop table if exists t1, t2;
--enable_warnings

create table t1 (v varchar(30), c char(3), e enum('abc','def','ghi'), t text);
copy_file $MYSQL_TEST_DIR/std_data/vchar.frm $MYSQLTEST_VARDIR/master-data/test/vchar.frm;
truncate table vchar;
show create table t1;
show create table vchar;
insert into t1 values ('abc', 'de', 'ghi', 'jkl');
insert into t1 values ('abc ', 'de ', 'ghi', 'jkl ');
insert into t1 values ('abc    ', 'd  ', 'ghi', 'jkl    ');
insert into vchar values ('abc', 'de', 'ghi', 'jkl');
insert into vchar values ('abc ', 'de ', 'ghi', 'jkl ');
insert into vchar values ('abc    ', 'd  ', 'ghi', 'jkl    ');
select length(v),length(c),length(e),length(t) from t1;
select length(v),length(c),length(e),length(t) from vchar;
alter table vchar add i int;
show create table vchar;
select length(v),length(c),length(e),length(t) from vchar;
drop table t1, vchar;
create table t1 (v varchar(20));
insert into t1 values('a ');
select v='a' from t1;
select binary v='a' from t1;
select binary v='a ' from t1;
insert into t1 values('a');
--error 1062
alter table t1 add primary key (v);
drop table t1;
create table t1 (v varbinary(20));
insert into t1 values('a');
insert into t1 values('a ');
alter table t1 add primary key (v);
drop table t1;

#
# Test with varchar of lengths 254,255,256,258 & 258 to ensure we don't
# have any problems with varchar with one or two byte length_bytes
#

create table t1 (v varchar(254), index (v));
insert into t1 values ("This is a test ");
insert into t1 values ("Some sample data");
insert into t1 values (" garbage ");
insert into t1 values (" This is a test ");
insert into t1 values ("This is a test");
insert into t1 values ("Hello world");
insert into t1 values ("Foo bar");
insert into t1 values ("This is a test");
insert into t1 values ("MySQL varchar test");
insert into t1 values ("test MySQL varchar");
insert into t1 values ("This is a long string to have some random length data included");
insert into t1 values ("Short string");
insert into t1 values ("VSS");
insert into t1 values ("Some samples");
insert into t1 values ("Bar foo");
insert into t1 values ("Bye");
let $i= 255;
let $j= 5;
while ($j)
{
  select * from t1 where v like 'This is a test' order by v;
  select * from t1 where v='This is a test' order by v;
  select * from t1 where v like 'S%' order by v;
  explain select * from t1 where v like 'This is a test' order by v;
  explain select * from t1 where v='This is a test' order by v;
  explain select * from t1 where v like 'S%' order by v;
  eval alter table t1 change v v varchar($i);
  inc $i;
  dec $j;
}
let $i= 258;
let $j= 6;
while ($j)
{
  select * from t1 where v like 'This is a test' order by v;
  select * from t1 where v='This is a test' order by v;
  select * from t1 where v like 'S%' order by v;
  explain select * from t1 where v like 'This is a test' order by v;
  explain select * from t1 where v='This is a test' order by v;
  explain select * from t1 where v like 'S%' order by v;
  eval alter table t1 change v v varchar($i);
  dec $i;
  dec $j;
}
alter table t1 change v v varchar(254), drop key v;

# Test with length(varchar) > 256 and key < 256 (to ensure things works with
# different kind of packing

alter table t1 change v v varchar(300), add key (v(10));
select * from t1 where v like 'This is a test' order by v;
select * from t1 where v='This is a test' order by v;
select * from t1 where v like 'S%' order by v;
explain select * from t1 where v like 'This is a test' order by v;
explain select * from t1 where v='This is a test' order by v;
explain select * from t1 where v like 'S%' order by v;
drop table t1;

#
# bug#9339 - meaningless Field_varstring::get_key_image
#
create table t1 (pkcol varchar(16), othercol varchar(16), primary key (pkcol));
insert into t1 values ('test', 'something');
update t1 set othercol='somethingelse' where pkcol='test';
select * from t1;
drop table t1;

#
# Bug #9489: problems with key handling
#

create table t1 (a int, b varchar(12));
insert into t1 values (1, 'A'), (22, NULL);
create table t2 (a int);
insert into t2 values (22), (22);
select t1.a, t1.b, min(t1.b) from t1 inner join t2 ON t2.a = t1.a 
  group by t1.b, t1.a;
drop table t1, t2;

#
# Bug #10543: convert varchar with index to text
#
create table t1 (f1 varchar(65500));
create index index1 on t1(f1(10));
show create table t1;
alter table t1 modify f1 varchar(255);
show create table t1;
alter table t1 modify f1 tinytext;
show create table t1;
drop table t1;

#
# BUG#15588: String overrun
#

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1(f1 VARCHAR(100) DEFAULT 'test');
INSERT INTO t1 VALUES(SUBSTR(f1, 1, 3));
DROP TABLE IF EXISTS t1;

CREATE TABLE t1(f1 CHAR(100) DEFAULT 'test');
INSERT INTO t1 VALUES(SUBSTR(f1, 1, 3));
DROP TABLE IF EXISTS t1;

#
# Bug#14897 "ResultSet.getString("table.column") sometimes doesn't find the
# column"
# Test that after upgrading an old 4.1 VARCHAR column to 5.0 VARCHAR we preserve
# the original column metadata.
#
--disable_warnings
drop table if exists t1, t2, t3;
--enable_warnings

create table t3 (
  id int(11),
  en varchar(255) character set utf8,
  cz varchar(255) character set utf8
);
remove_file $MYSQLTEST_VARDIR/master-data/test/t3.frm;
copy_file $MYSQL_TEST_DIR/std_data/14897.frm $MYSQLTEST_VARDIR/master-data/test/t3.frm;
truncate table t3;
insert into t3 (id, en, cz) values 
(1,'en string 1','cz string 1'),
(2,'en string 2','cz string 2'),
(3,'en string 3','cz string 3');

create table t1 ( 
  id int(11),
  name_id int(11)
);
insert into t1 (id, name_id) values (1,1), (2,3), (3,3);

create table t2 (id int(11));
insert into t2 (id) values (1), (2), (3);

# max_length is different for varchar fields in ps-protocol and we can't 
# replace a single metadata column, disable PS protocol
--disable_ps_protocol
--enable_metadata
select t1.*, t2.id, t3.en, t3.cz from t1 left join t2 on t1.id=t2.id
left join t3 on t1.id=t3.id order by t3.id;
--disable_metadata
--enable_ps_protocol
drop table t1, t2, t3;

#
# Bug #11927: Warnings shown for CAST( chr as signed) but not (chr + 0)
#
CREATE TABLE t1 (a CHAR(2));
INSERT INTO t1 VALUES (10), (50), (30), ('1a'), (60), ('t');
SELECT a,(a + 0) FROM t1 ORDER BY a;
SELECT a,(a DIV 2) FROM t1 ORDER BY a;
SELECT a,CAST(a AS SIGNED) FROM t1 ORDER BY a;
DROP TABLE t1;
