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

CREATE TABLE t1 (
  latin1_f CHAR(32) CHARACTER SET latin1 NOT NULL
);

--error 1252
CREATE TABLE t2 (
  latin1_f CHAR(32) CHARACTER SET latin1 COLLATE koi8r_general_ci NOT NULL
);

--error 1272
CREATE TABLE t2 (
  latin1_f CHAR(32) CHARACTER SET latin1 COLLATE some_non_existing_col NOT NULL
);


INSERT INTO t1 (latin1_f) VALUES (_latin1'A');
INSERT INTO t1 (latin1_f) VALUES (_latin1'a');

INSERT INTO t1 (latin1_f) VALUES (_latin1'AD');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ad');

INSERT INTO t1 (latin1_f) VALUES (_latin1'AE');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ae');

INSERT INTO t1 (latin1_f) VALUES (_latin1'AF');
INSERT INTO t1 (latin1_f) VALUES (_latin1'af');

INSERT INTO t1 (latin1_f) VALUES (_latin1'Ä');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ä');

INSERT INTO t1 (latin1_f) VALUES (_latin1'Å');
INSERT INTO t1 (latin1_f) VALUES (_latin1'å');

INSERT INTO t1 (latin1_f) VALUES (_latin1'B');
INSERT INTO t1 (latin1_f) VALUES (_latin1'b');

INSERT INTO t1 (latin1_f) VALUES (_latin1'U');
INSERT INTO t1 (latin1_f) VALUES (_latin1'u');

INSERT INTO t1 (latin1_f) VALUES (_latin1'UE');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ue');

INSERT INTO t1 (latin1_f) VALUES (_latin1'Ü');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ü');

INSERT INTO t1 (latin1_f) VALUES (_latin1'SS');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ss');
INSERT INTO t1 (latin1_f) VALUES (_latin1'ß');

INSERT INTO t1 (latin1_f) VALUES (_latin1'Y');
INSERT INTO t1 (latin1_f) VALUES (_latin1'y');

INSERT INTO t1 (latin1_f) VALUES (_latin1'Z');
INSERT INTO t1 (latin1_f) VALUES (_latin1'z');


-- ORDER BY

SELECT latin1_f FROM t1 ORDER BY latin1_f;
SELECT latin1_f FROM t1 ORDER BY latin1_f COLLATE latin1_swedish_ci;
SELECT latin1_f FROM t1 ORDER BY latin1_f COLLATE latin1_german2_ci;
SELECT latin1_f FROM t1 ORDER BY latin1_f COLLATE latin1_general_ci;
SELECT latin1_f FROM t1 ORDER BY latin1_f COLLATE latin1_bin;
--error 1252
SELECT latin1_f FROM t1 ORDER BY latin1_f COLLATE koi8r_general_ci;

--SELECT latin1_f COLLATE koi8r FROM t1 ;

-- AS + ORDER BY
SELECT latin1_f COLLATE latin1_swedish_ci AS latin1_f_as FROM t1 ORDER BY latin1_f_as;
SELECT latin1_f COLLATE latin1_german2_ci AS latin1_f_as FROM t1 ORDER BY latin1_f_as;
SELECT latin1_f COLLATE latin1_general_ci AS latin1_f_as FROM t1 ORDER BY latin1_f_as;
SELECT latin1_f COLLATE latin1_bin        AS latin1_f_as FROM t1 ORDER BY latin1_f_as;
--error 1252
SELECT latin1_f COLLATE koi8r_general_ci  AS latin1_f_as FROM t1 ORDER BY latin1_f_as;


-- GROUP BY

SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f;
SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f COLLATE latin1_swedish_ci;
SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f COLLATE latin1_german2_ci;
SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f COLLATE latin1_general_ci;
SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f COLLATE latin1_bin;
--error 1252
SELECT latin1_f,count(*) FROM t1 GROUP BY latin1_f COLLATE koi8r_general_ci;


--  DISTINCT

SELECT DISTINCT latin1_f                           FROM t1;
SELECT DISTINCT latin1_f COLLATE latin1_swedish_ci FROM t1;
SELECT DISTINCT latin1_f COLLATE latin1_german2_ci FROM t1;
SELECT DISTINCT latin1_f COLLATE latin1_general_ci FROM t1;
SELECT DISTINCT latin1_f COLLATE latin1_bin        FROM t1;
--error 1272
SELECT DISTINCT latin1_f COLLATE koi8r             FROM t1;


-- Aggregates
--SELECT MAX(k COLLATE latin1_german2_ci)
--FROM t1


-- WHERE
--SELECT *
--FROM t1 
--WHERE (_latin1'Mu"ller' COLLATE latin1_german2_ci) = k 

--HAVING
--SELECT *
--FROM t1 
--HAVING (_latin1'Mu"ller' COLLATE latin1_german2_ci) = k 


#
# Check that SHOW displays COLLATE clause
#

SHOW CREATE TABLE t1;
SHOW FIELDS FROM  t1;
ALTER TABLE t1 CHANGE latin1_f 
latin1_f CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin;
SHOW CREATE TABLE t1;
SHOW FIELDS FROM  t1;
ALTER TABLE t1 CHARACTER SET latin1 COLLATE latin1_bin;
SHOW CREATE TABLE t1;
SHOW FIELDS FROM  t1;

#
# Check SET CHARACTER SET
#

SET CHARACTER SET 'latin1';
SHOW VARIABLES LIKE 'character_set_client';
SELECT charset('a'),collation('a'),coercibility('a'),'a'='A';
explain extended SELECT charset('a'),collation('a'),coercibility('a'),'a'='A';

SET CHARACTER SET koi8r;
SHOW VARIABLES LIKE 'collation_client';
SELECT charset('a'),collation('a'),coercibility('a'),'a'='A';

--error 1115
SET CHARACTER SET 'DEFAULT';

DROP TABLE t1;

CREATE TABLE t1 
(s1 CHAR(5) COLLATE latin1_german1_ci,
 s2 CHAR(5) COLLATE latin1_swedish_ci);
--error 1266
SELECT * FROM t1 WHERE s1 = s2;
DROP TABLE t1;
