2003-01-06 00:48:59 +01:00
# Initialise
--disable_warnings
drop table if exists t1,t2;
--enable_warnings
--error 1054
2001-03-14 07:07:12 +01:00
set @a := foo;
set @a := connection_id() + 3;
select @a - connection_id();
2003-10-02 16:19:33 +02:00
set @b := 1;
select @b;
2001-03-15 02:55:48 +01:00
# Check using and setting variables with SELECT DISTINCT
CREATE TABLE t1 ( i int not null, v int not null,index (i));
insert into t1 values (1,1),(1,3),(2,1);
create table t2 (i int not null, unique (i));
insert into t2 select distinct i from t1;
select * from t2;
select distinct t2.i,@vv1:=if(sv1.i,1,0),@vv2:=if(sv2.i,1,0),@vv3:=if(sv3.i,1,0), @vv1+@vv2+@vv3 from t2 left join t1 as sv1 on sv1.i=t2.i and sv1.v=1 left join t1 as sv2 on sv2.i=t2.i and sv2.v=2 left join t1 as sv3 on sv3.i=t2.i and sv3.v=3;
2001-03-15 10:55:44 +01:00
explain select * from t1 where i=@vv1;
2004-09-09 05:59:26 +02:00
select @vv1,i,v from t1 where i=@vv1;
2001-03-15 10:55:44 +01:00
explain select * from t1 where @vv1:=@vv1+1 and i=@vv1;
explain select @vv1:=i from t1 where i=@vv1;
explain select * from t1 where i=@vv1;
2001-03-15 02:55:48 +01:00
drop table t1,t2;
2003-06-26 03:11:23 +02:00
# Check types of variables
2003-10-02 23:40:27 +02:00
set @a=0,@b=0;
2003-06-26 03:11:23 +02:00
select @a:=10, @b:=1, @a > @b, @a < @b;
2003-10-02 23:40:27 +02:00
# Note that here a and b will be avaluated as number
2003-06-26 03:11:23 +02:00
select @a:="10", @b:="1", @a > @b, @a < @b;
2003-10-02 23:40:27 +02:00
# Note that here a and b will be avaluated as strings
2003-06-26 03:11:23 +02:00
select @a:=10, @b:=2, @a > @b, @a < @b;
select @a:="10", @b:="2", @a > @b, @a < @b;
2003-09-25 01:30:20 +02:00
# Fixed bug #1194
select @a:=1;
select @a, @a:=1;
2003-10-02 23:40:27 +02:00
create table t1 (id int, d double, c char(10));
insert into t1 values (1,2.0, "test");
2003-09-25 01:30:20 +02:00
select @c:=0;
update t1 SET id=(@c:=@c+1);
select @c;
select @c:=0;
update t1 set id=(@c:=@c+1);
select @c;
select @c:=0;
select @c:=@c+1;
2003-10-02 23:40:27 +02:00
select @d,(@d:=id),@d from t1;
select @e,(@e:=d),@e from t1;
select @f,(@f:=c),@f from t1;
set @g=1;
select @g,(@g:=c),@g from t1;
select @c, @d, @e, @f;
select @d:=id, @e:=id, @f:=id, @g:=@id from t1;
select @c, @d, @e, @f, @g;
2003-09-25 01:30:20 +02:00
drop table t1;
2003-10-02 23:40:27 +02:00
# just for fun :)
select @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b, @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b;
2003-11-18 22:06:47 +01:00
#
# bug#1739
# Item_func_set_user_var sets update_query_id, Item_func_get_user_var checks it
#
create table t1 (i int not null);
insert t1 values (1),(2),(2),(3),(3),(3);
select @a:=0; select @a, @a:=@a+count(*), count(*), @a from t1 group by i;
select @a:=0; select @a+0, @a:=@a+0+count(*), count(*), @a+0 from t1 group by i;
2005-11-01 14:54:30 +01:00
set @a=0;
select @a,@a:="hello",@a,@a:=3,@a,@a:="hello again" from t1 group by i;
select @a,@a:="hello",@a,@a:=3,@a,@a:="hello again" from t1 group by i;
2003-11-18 22:06:47 +01:00
drop table t1;
2003-12-31 16:01:03 +01:00
#
# Bug #2244: User variables didn't copy collation and derivation
# attributes from values they were initialized to.
#
set @a=_latin2'test';
select charset(@a),collation(@a),coercibility(@a);
select @a=_latin2'TEST';
select @a=_latin2'TEST' collate latin2_bin;
set @a=_latin2'test' collate latin2_general_ci;
select charset(@a),collation(@a),coercibility(@a);
select @a=_latin2'TEST';
select @a=_latin2'TEST' collate latin2_bin;
#
# Check the same invoking Item_set_user_var
#
select charset(@a:=_latin2'test');
select collation(@a:=_latin2'test');
select coercibility(@a:=_latin2'test');
select collation(@a:=_latin2'test' collate latin2_bin);
select coercibility(@a:=_latin2'test' collate latin2_bin);
select (@a:=_latin2'test' collate latin2_bin) = _latin2'TEST';
select charset(@a),collation(@a),coercibility(@a);
select (@a:=_latin2'test' collate latin2_bin) = _latin2'TEST' collate latin2_general_ci;
Implementation of WL#1824 "Add replication of character set variables in 4.1",
by binlogging some SET ONE_SHOT CHARACTER_SETetc,
which will be enough until we have it more compact and more complete in 5.0. With the present patch,
replication will work ok between 4.1.3 master and slaves, as long as:
- master and slave have the same GLOBAL.COLLATION_SERVER
- COLLATION_DATABASE and CHARACTER_SET_DATABASE are not used
- application does not use the fact that table is created with charset of the USEd db (BUG#2326).
all of which are not too hard to fulfill.
ONE_SHOT is reserved for internal use of mysqlbinlog|mysql and works only for charsets,
so we give error if used for non-charset vars.
Fix for BUG#3875 "mysqlbinlog produces wrong ouput if query uses
variables containing quotes" and BUG#3943 "Queries with non-ASCII literals are not replicated
properly after SET NAMES".
Detecting that master and slave have different global charsets or server ids.
mysql-test/r/rpl_server_id1.result:
it's normal to not run as I have added a test to compare server ids of master and slave
at startup and stop if equal (unless --replicate-same-server-id)
mysql-test/r/rpl_user_variables.result:
result update (as we now print charset of user var).
mysql-test/r/user_var.result:
result update
mysql-test/t/rpl_server_id1.test:
no need to select as slave is not running
mysql-test/t/user_var.test:
testing if the content of user vars is escaped when mysqlbinlog prints them,
and if the name is backquoted.
sql/lex.h:
new keyword ONE_SHOT
sql/log.cc:
when writing to the binlog, before writing the actual statement, write some SET ONE_SHOT CHARACTER_SET_CLIENT etc
for the slave to know the charset variables (which are important as they affect the inserted data).
sql/log_event.cc:
print charset and collation of user var in mysqlbinlog and SHOW BINLOG EVENTS.
escape the content of the var. Backquote its name.
Will ask Bar to check that using my_charset_bin for escaping is ok.
sql/set_var.cc:
understand SET CHARACTER_SET_CLIENT=10 (don't require a string, accept a number).
Refuse changing of GLOBAL CHARACTER_SET_SERVER/COLLATION_SERVER if binlog or slave,
as it will make the master or slave make wrong assumptions.
A function to catch SET ONE_SHOT on non-charset variables (which is forbidden)
sql/set_var.h:
no_support_one_shot to know if the var supports ONE_SHOT (only charset vars do, soon timezones).
Accept int arg in SET CHARACTER_SET_etc
sql/slave.cc:
when I/O slave thread starts, verify that master's and slave charsets match.
And by the way verify that server ids are different.
Don't fail if UNIX_TIMESTAMP() can't be done on master (very old master), that's
not fatal.
sql/sql_class.cc:
one_shot
sql/sql_class.h:
one_shot
sql/sql_lex.h:
one_shot
sql/sql_parse.cc:
when SET ONE_SHOT is used, verify that it's only used for charset/collation vars;
otherwise refuse.
sql/sql_yacc.yy:
ONE_SHOT keyword in SET
2004-06-03 23:17:18 +02:00
2004-11-05 11:07:12 +01:00
#
# Bug #6321 strange error:
# string function FIELD(<uservariable content NULL>, ...)
#
set @var= NULL ;
select FIELD( @var,'1it','Hit') as my_column;
2005-03-28 11:01:57 +02:00
#
# Bug#9425 A user variable doesn't always have implicit coercibility
#
select @v, coercibility(@v);
set @v1=null, @v2=1, @v3=1.1, @v4=now();
select coercibility(@v1),coercibility(@v2),coercibility(@v3),coercibility(@v4);
2005-04-06 11:13:06 +02:00
#
# Bug #9286 SESSION/GLOBAL should be disallowed for user variables
#
2005-05-18 09:47:45 +02:00
--error 1064
2005-04-06 11:13:06 +02:00
set session @honk=99;
2005-04-30 02:14:42 +02:00
2005-07-25 20:25:28 +02:00
#
# Bug #10724 @@local not preserved in column name of select
#
# The value doesn't actually matter, we just care about the column name
--replace_column 1 #
select @@local.max_allowed_packet;
--replace_column 1 #
select @@session.max_allowed_packet;
--replace_column 1 #
select @@global.max_allowed_packet;
--replace_column 1 #
select @@max_allowed_packet;
--replace_column 1 #
select @@Max_Allowed_Packet;
--replace_column 1 #
select @@version;
--replace_column 1 #
select @@global.version;
2005-08-02 02:10:35 +02:00
2006-10-04 16:49:39 +02:00
--echo End of 4.1 tests
2005-04-30 02:14:42 +02:00
# Bug #6598: problem with cast(NULL as signed integer);
#
set @first_var= NULL;
create table t1 select @first_var;
show create table t1;
drop table t1;
set @first_var= cast(NULL as signed integer);
create table t1 select @first_var;
show create table t1;
drop table t1;
set @first_var= NULL;
create table t1 select @first_var;
show create table t1;
drop table t1;
set @first_var= concat(NULL);
create table t1 select @first_var;
show create table t1;
drop table t1;
set @first_var=1;
set @first_var= cast(NULL as CHAR);
create table t1 select @first_var;
show create table t1;
drop table t1;
2006-06-09 19:35:54 +02:00
#
# Bug #7498 User variable SET saves SIGNED BIGINT as UNSIGNED BIGINT
#
# First part, set user var to large number and select it
set @a=18446744071710965857;
select @a;
# Second part, set user var from large number in table
# then select it
CREATE TABLE `bigfailure` (
`afield` BIGINT UNSIGNED NOT NULL
);
INSERT INTO `bigfailure` VALUES (18446744071710965857);
SELECT * FROM bigfailure;
select * from (SELECT afield FROM bigfailure) as b;
select * from bigfailure where afield = (SELECT afield FROM bigfailure);
select * from bigfailure where afield = 18446744071710965857;
# This is fixed in 5.0, to be uncommented there
#select * from bigfailure where afield = '18446744071710965857';
select * from bigfailure where afield = 18446744071710965856+1;
SET @a := (SELECT afield FROM bigfailure);
SELECT @a;
SET @a := (select afield from (SELECT afield FROM bigfailure) as b);
SELECT @a;
SET @a := (select * from bigfailure where afield = (SELECT afield FROM bigfailure));
SELECT @a;
drop table bigfailure;
2006-08-22 15:37:41 +02:00
#
# Bug#16861: User defined variable can have a wrong value if a tmp table was
# used.
#
create table t1(f1 int, f2 int);
insert into t1 values (1,2),(2,3),(3,1);
select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
select @var;
2007-01-09 21:24:56 +01:00
create table t2 as select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
select * from t2;
select @var;
drop table t1,t2;
2006-09-12 17:35:44 +02:00
2006-10-04 16:49:39 +02:00
#
# Bug#19024 - SHOW COUNT(*) WARNINGS not return Errors
#
--error 1064
insert into city 'blah';
SHOW COUNT(*) WARNINGS;
SHOW COUNT(*) ERRORS;
2007-05-31 23:17:14 +02:00
#
# Bug#28494: Grouping by Item_func_set_user_var produces incorrect result.
#
2007-06-03 12:46:09 +02:00
create table t1(f1 int, f2 varchar(2), f3 float, f4 decimal(2,1));
insert into t1 values
(1, "a", 1.5, 1.6), (1, "a", 1.5, 1.6), (2, "b", 2.5, 2.6),
(3, "c", 3.5, 3.6), (4, "d", 4.5, 4.6), (1, "a", 1.5, 1.6),
(3, "c", 3.5, 3.6), (1, "a", 1.5, 1.6);
2007-06-02 21:17:46 +02:00
select @a:=f1, count(f1) from t1 group by 1 desc;
select @a:=f1, count(f1) from t1 group by 1 asc;
2007-06-03 12:46:09 +02:00
select @a:=f2, count(f2) from t1 group by 1 desc;
select @a:=f3, count(f3) from t1 group by 1 desc;
select @a:=f4, count(f4) from t1 group by 1 desc;
2007-05-31 23:17:14 +02:00
drop table t1;
2007-11-17 08:20:50 +01:00
#
2007-12-07 20:54:47 +01:00
# Bug#32482: Crash for a query with ORDER BY a user variable.
#
create table t1 (f1 int);
insert into t1 values (2), (1);
select @i := f1 as j from t1 order by 1;
drop table t1;
2007-11-17 08:20:50 +01:00
# Bug #32260: User variables in query cause server crash
#
create table t1(a int);
insert into t1 values(5),(4),(4),(3),(2),(2),(2),(1);
set @rownum := 0;
set @rank := 0;
set @prev_score := NULL;
# Disable the result log as we assign a value to a user variable in one part
# of a statement and use the same variable in other part of the same statement,
# so we can get unexpected results.
--disable_result_log
select @rownum := @rownum + 1 as row,
@rank := IF(@prev_score!=a, @rownum, @rank) as rank,
@prev_score := a as score
from t1 order by score desc;
--enable_result_log
drop table t1;
2008-09-18 10:38:44 +02:00
#
# Bug#26020: User-Defined Variables are not consistent with columns data types
#
create table t1(b bigint);
insert into t1 (b) values (10), (30), (10);
set @var := 0;
select if(b=@var, 999, b) , @var := b from t1 order by b;
drop table t1;
create temporary table t1 (id int);
insert into t1 values (2), (3), (3), (4);
set @lastid=-1;
select @lastid != id, @lastid, @lastid := id from t1;
drop table t1;
create temporary table t1 (id bigint);
insert into t1 values (2), (3), (3), (4);
set @lastid=-1;
select @lastid != id, @lastid, @lastid := id from t1;
drop table t1;
2009-02-24 15:47:12 +01:00
#
# Bug#42009: SELECT into variable gives different results to direct SELECT
#
CREATE TABLE t1(a INT, b INT);
INSERT INTO t1 VALUES (0, 0), (2, 1), (2, 3), (1, 1), (30, 20);
SELECT a, b INTO @a, @b FROM t1 WHERE a=2 AND b=3 GROUP BY a, b;
SELECT @a, @b;
SELECT a, b FROM t1 WHERE a=2 AND b=3 GROUP BY a, b;
DROP TABLE t1;
2009-12-22 10:52:23 +01:00
#
# Bug#47371: reference by same column name
#
CREATE TABLE t1 (f1 int(11) default NULL, f2 int(11) default NULL);
CREATE TABLE t2 (f1 int(11) default NULL, f2 int(11) default NULL, foo int(11));
CREATE TABLE t3 (f1 int(11) default NULL, f2 int(11) default NULL);
INSERT INTO t1 VALUES(10, 10);
INSERT INTO t1 VALUES(10, 10);
INSERT INTO t2 VALUES(10, 10, 10);
INSERT INTO t2 VALUES(10, 10, 10);
INSERT INTO t3 VALUES(10, 10);
INSERT INTO t3 VALUES(10, 10);
SELECT MIN(t2.f1),
@bar:= (SELECT MIN(t3.f2) FROM t3 WHERE t3.f2 > foo)
FROM t1,t2 WHERE t1.f1 = t2.f1 ORDER BY t2.f1;
DROP TABLE t1, t2, t3;
2009-02-24 15:47:12 +01:00
--echo End of 5.0 tests
2009-05-10 18:20:35 +02:00
2009-01-23 19:18:02 +01:00
#
# Bug#42188: crash and/or memory corruption with user variables in trigger
#
CREATE TABLE t1 (i INT);
CREATE TRIGGER t_after_insert AFTER INSERT ON t1 FOR EACH ROW SET @bug42188 = 10;
INSERT INTO t1 VALUES (1);
2009-01-24 15:07:10 +01:00
--change_user root,,test
2009-01-23 19:18:02 +01:00
INSERT INTO t1 VALUES (1);
DROP TABLE t1;
2010-08-13 13:20:49 +02:00
#
# Bug #55615: debug assertion after using variable in assignment and
# referred to
# Bug #55564: crash with user variables, assignments, joins...
#
CREATE TABLE t1(a INT);
INSERT INTO t1 VALUES (0),(0);
--echo # BUG#55615 : should not crash
SELECT (@a:=(SELECT @a:=1 FROM t1 LIMIT 1)) AND COUNT(1) FROM t1 GROUP BY @a;
--echo # BUG#55564 : should not crash
SELECT IF(
@v:=LEAST((SELECT 1 FROM t1 t2 LEFT JOIN t1 ON (@v) GROUP BY t1.a), a),
count(*), 1)
FROM t1 GROUP BY a LIMIT 1;
DROP TABLE t1;
2007-11-17 08:20:50 +01:00
--echo End of 5.1 tests
2010-05-05 13:00:59 +02:00
#
# Bug#50511: Sometimes wrong handling of user variables containing NULL.
#
--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings
CREATE TABLE t1(f1 INT AUTO_INCREMENT, PRIMARY KEY(f1));
INSERT INTO t1 SET f1 = NULL ;
SET @aux = NULL ;
INSERT INTO t1 SET f1 = @aux ;
SET @aux1 = 0.123E-1;
SET @aux1 = NULL;
INSERT INTO t1 SET f1 = @aux1 ;
SELECT * FROM t1;
DROP TABLE t1;
CREATE TABLE t1(f1 VARCHAR(257) , f2 INT, PRIMARY KEY(f2));
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW SET @aux = 1;
SET @aux = 1; # INT
SET @aux = NULL;
INSERT INTO test.t1 (f1, f2) VALUES (1, 1), (@aux, 2);
SET @aux = 'text'; # STRING
SET @aux = NULL;
INSERT INTO t1(f1, f2) VALUES (1, 3), (@aux, 4);
SELECT f1, f2 FROM t1 ORDER BY f2;
DROP TRIGGER trg1;
DROP TABLE t1;
--echo End of 5.5 tests