# Check that non-utf8 characters are ok

--source include/have_optimizer_trace.inc

# --ps-protocol gives different trace where view
# has already been materialized
if (`SELECT $PS_PROTOCOL + $SP_PROTOCOL + $CURSOR_PROTOCOL
            + $VIEW_PROTOCOL > 0`)
{
   --skip Need normal protocol
}

set @@session.end_markers_in_json=on;
set @@session.optimizer_trace="enabled=on";
create table t1(a int);
insert into t1 values(1);

# test non-utf8 chars in the SELECT list

set names latin1;
# The 5 'A's below are, in this file's bytes, written in latin1
# encoding: xC1C2C4C4C5. We send them as latin1; the server interprets
# them as characters using latin1, then encodes those characters in
# utf8 in the trace; sends back the trace but re-encodes it in latin1
# (as this is how we want it, see "set names latin1".
explain extended select 'ÁÂÃÄÅ', _latin1'ÁÂÃÄÅ' from t1 limit 1;
select (@query:=QUERY)+NULL, (@trace:=TRACE)+NULL from information_schema.OPTIMIZER_TRACE;
select @query, @trace;
# The trace above is not valid JSON because the client asked to
# receive it encoded in latin1, whereas JSON wants it in utf8.
# Let's get it in utf8.
set names utf8;
select @query, @trace;

# test non-utf8 chars in a view's definition

set names latin1;
create view v1 as select 'ÁÂÃÄÅ' as col from t1 limit 1;
select * from v1 where v1.col = 'ÁÂÃÄÅ';
select (@query:=QUERY)+NULL, (@trace:=TRACE)+NULL from information_schema.OPTIMIZER_TRACE;
set names utf8;
select @query, @trace;

drop table t1;
drop view v1;

# test non-ASCII chars in a range

set names latin1;
create table t1(c char(4) primary key);
insert into t1 values ('aaa'), ('ÁÂÃÄ');
select * from t1 where c < 'ÁÂÃÄÅ';
select (@query:=QUERY)+NULL, (@trace:=TRACE)+NULL from information_schema.OPTIMIZER_TRACE;
set names utf8;
select @query, @trace;
drop table t1;

# test newline and tabulation chars in an identifier
create table `t
1	a`(`col
1	a` int, index `index
1	a` (`col
1	a`));
insert into `t
1	a` values(2),(3);
create view `v
1	a` as select * from `t
1	a`;
select `col
1	a` from `v
1	a` where `col
1	a` < 6;
select query, trace from information_schema.OPTIMIZER_TRACE;
drop table `t
1	a`;
drop view `v
1	a`;

set optimizer_trace=default;
