mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / t / func_misc.test
blobf47418fa77384ad5a3ec4abafe0b4e066b71a5c8
2 # Testing of misc functions
5 --disable_warnings
6 DROP TABLE IF EXISTS t1, t2;
7 --enable_warnings
9 select format(1.5555,0),format(123.5555,1),format(1234.5555,2),format(12345.55555,3),format(123456.5555,4),format(1234567.5555,5),format("12345.2399",2);
11 select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
12 select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
13 select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
15 select hex(inet_aton('127'));
16 select hex(inet_aton('127.1'));
17 select hex(inet_aton('127.1.1'));
19 select length(uuid()), charset(uuid()), length(unhex(replace(uuid(),_utf8'-',_utf8'')));
21 # As we can assume we are the only user for the mysqld server, the difference
22 # between two calls should be -1
23 set @a= uuid_short();
24 set @b= uuid_short();
25 select cast(@a - @b as signed);
28 # Test for core dump with nan
30 select length(format('nan', 2)) > 0;
33 # Test for bug #628
35 select concat("$",format(2500,2));
37 # Test for BUG#7716
38 create table t1 ( a timestamp );
39 insert into t1 values ( '2004-01-06 12:34' );
40 select a from t1 where left(a+0,6) in ( left(20040106,6) );
41 select a from t1 where left(a+0,6) = ( left(20040106,6) );
43 select a from t1 where right(a+0,6) in ( right(20040106123400,6) );
44 select a from t1 where right(a+0,6) = ( right(20040106123400,6) );
46 select a from t1 where mid(a+0,6,3) in ( mid(20040106123400,6,3) );
47 select a from t1 where mid(a+0,6,3) = ( mid(20040106123400,6,3) );
49 drop table t1;
53 # Bug#16501: IS_USED_LOCK does not appear to work
56 CREATE TABLE t1 (conn CHAR(7), connection_id INT);
57 INSERT INTO t1 VALUES ('default', CONNECTION_ID());
59 SELECT GET_LOCK('bug16501',600);
61 connect (con1,localhost,root,,);
62 INSERT INTO t1 VALUES ('con1', CONNECTION_ID());
63 SELECT IS_USED_LOCK('bug16501') = connection_id
64 FROM t1
65 WHERE conn = 'default';
66 send SELECT GET_LOCK('bug16501',600);
68 connection default;
69 SELECT IS_USED_LOCK('bug16501') = CONNECTION_ID();
70 SELECT RELEASE_LOCK('bug16501');
71 connection con1;
72 reap;
73 connection default;
74 SELECT IS_USED_LOCK('bug16501') = connection_id
75 FROM t1
76 WHERE conn = 'con1';
78 connection con1;
79 SELECT IS_USED_LOCK('bug16501') = CONNECTION_ID();
80 SELECT RELEASE_LOCK('bug16501');
81 SELECT IS_USED_LOCK('bug16501');
83 disconnect con1;
84 connection default;
86 DROP TABLE t1;
89 # Bug #21531: EXPORT_SET() doesn't accept args with coercible character sets
91 select export_set(3, _latin1'foo', _utf8'bar', ',', 4);
93 --echo End of 4.1 tests
97 # Test for BUG#9535
99 --disable_warnings
100 create table t1 as select uuid(), length(uuid());
101 --enable_warnings
102 show create table t1;
103 drop table t1;
106 # Bug#6760: Add SLEEP() function (feature request)
108 #   Logics of original test:
109 #   Reveal that a query with SLEEP does not need less time than estimated.
111 # Bug#12689: SLEEP() gets incorrectly cached/optimized-away
113 #   Description from bug report (slightly modified)
115 #   Bug 1 (happened all time):
116 #      SELECT * FROM t1 WHERE SLEEP(1) will only result in a sleep of 1
117 #      second, regardless of the number of rows in t1.
118 #   Bug 2 (happened all time):
119 #      Such a query will also get cached by the query cache, but should not.
121 # Notes (mleich, 2008-05)
122 # =======================
124 # Experiments around
125 #    Bug#36345 Test 'func_misc' fails on RHAS3 x86_64
126 # showed that the tests for both bugs could produce in case of parallel
127 # artificial system time (like via ntpd)
128 # - decreases false alarm
129 # - increases false success
131 # We try here to circumvent these issues by reimplementation of the tests
132 # and sophisticated scripting, although the cause of the problems is a massive
133 # error within the setup of the testing environment.
134 # Tests relying on or checking derivates of the system time must never meet
135 # parallel manipulations of system time.
137 # Results of experiments with/without manipulation of system time,
138 # information_schema.processlist content, high load on testing box
139 # ----------------------------------------------------------------
140 # Definition: Predicted_cumulative_sleep_time =
141 #                #_of_result_rows * sleep_time_per_result_row
143 # 1. Total (real sleep time) ~= predicted_cumulative_sleep_time !!
144 # 2. The state of a session within the PROCESSLIST changes to 'User sleep'
145 #    if the sessions runs a statement containing the sleep function and the
146 #    processing of the statement is just within the phase where the sleep
147 #    is done. (*)
148 # 3. NOW() and processlist.time behave "synchronous" to system time and
149 #    show also the "jumps" caused by system time manipulations. (*)
150 # 4. processlist.time is unsigned, the "next" value below 0 is ~ 4G (*)
151 # 5. Current processlist.time ~= current real sleep time if the system time
152 #    was not manipulated. (*)
153 # 6. High system load can cause delays of <= 2 seconds.
154 # 7. Thanks to Davi for excellent hints and ideas.
156 #    (*)
157 #    - information_schema.processlist is not available before MySQL 5.1.
158 #    - Observation of processlist content requires a
159 #      - "worker" session sending the query with "send" and pulling results
160 #        with "reap"
161 #      - session observing the processlist parallel to the worker session
162 #      "send" and "reap" do not work in case of an embedded server.
163 #    Conclusion: Tests based on processlist have too many restrictions.
165 # Solutions for subtests based on TIMEDIFF of values filled via NOW()
166 # -------------------------------------------------------------------
167 # Run the following sequence three times
168 #    1. SELECT <start_time>
169 #    2. Query with SLEEP
170 #    3. SELECT <end_time>
171 # If TIMEDIFF(<end_time>,<start_time>) is at least two times within a
172 # reasonable range assume that we did not met errors we were looking for.
174 # It is extreme unlikely that we have two system time changes within the
175 # < 30 seconds runtime. Even if the unlikely happens, there are so
176 # frequent runs of this test on this or another testing box which will
177 # catch the problem.
180 --echo #------------------------------------------------------------------------
181 --echo # Tests for Bug#6760 and Bug#12689
182 # Number of rows within the intended result set.
183 SET @row_count = 4;
184 # Parameter within SLEEP function
185 SET @sleep_time_per_result_row = 1;
186 # Maximum acceptable delay caused by high load on testing box
187 SET @max_acceptable_delay = 2;
188 # TIMEDIFF = time for query with sleep (mostly the time caused by SLEEP)
189 #            + time for delays caused by high load on testing box
190 # Ensure that at least a reasonable fraction of TIMEDIFF belongs to the SLEEP
191 # by appropriate setting of variables.
192 # Ensure that any "judging" has a base of minimum three attempts.
193 # (Test 2 uses all attempts except the first one.)
194 if (!` SELECT (@sleep_time_per_result_row * @row_count - @max_acceptable_delay >
195               @sleep_time_per_result_row) AND (@row_count - 1 >= 3)`)
197    --echo # Have to abort because of error in plausibility check
198    --echo ######################################################
199    --vertical_results
200    SELECT @sleep_time_per_result_row * @row_count - @max_acceptable_delay >
201                @sleep_time_per_result_row AS must_be_1,
202                @row_count - 1 >= 3 AS must_be_also_1,
203                @sleep_time_per_result_row, @row_count, @max_acceptable_delay;
204    exit;
206 SET @@global.query_cache_size = 1024 * 64;
207 --disable_warnings
208 DROP TEMPORARY TABLE IF EXISTS t_history;
209 DROP TABLE IF EXISTS t1;
210 --enable_warnings
211 CREATE TEMPORARY TABLE t_history (attempt SMALLINT,
212 start_ts DATETIME, end_ts DATETIME,
213 start_cached INTEGER, end_cached INTEGER);
214 CREATE TABLE t1 (f1 BIGINT);
215 let $num = `SELECT @row_count`;
216 while ($num)
218    INSERT INTO t1 VALUES (1);
219    dec $num;
222 let $loops = 4;
223 let $num = $loops;
224 while ($num)
226    let $Qcache_queries_in_cache =
227        query_get_value(SHOW STATUS LIKE 'Qcache_queries_in_cache', Value, 1);
228    eval
229    INSERT INTO t_history
230    SET attempt = $loops - $num + 1, start_ts = NOW(),
231        start_cached = $Qcache_queries_in_cache;
232    SELECT *, SLEEP(@sleep_time_per_result_row) FROM t1;
233    #
234    # Do not determine Qcache_queries_in_cache before updating end_ts. The SHOW
235    # might cost too much time on an overloaded box.
236    eval
237    UPDATE t_history SET end_ts = NOW()
238    WHERE attempt = $loops - $num + 1;
239    let $Qcache_queries_in_cache =
240           query_get_value(SHOW STATUS LIKE 'Qcache_queries_in_cache', Value, 1);
241    eval
242    UPDATE t_history SET end_cached = $Qcache_queries_in_cache
243    WHERE attempt = $loops - $num + 1;
244    # DEBUG eval SELECT * FROM t_history WHERE attempt = $loops - $num + 1;
245    dec $num;
248 # 1. The majority of queries with SLEEP must need a reasonable time
249 #    -> SLEEP has an impact on runtime
250 #       = Replacement for original Bug#6760 test
251 #    -> total runtime is clear more needed than for one result row needed
252 #       = Replacement for one of the original Bug#12689 tests
253 --echo # Test 1: Does the query with SLEEP need a reasonable time?
254 eval SELECT COUNT(*) >= $loops - 1 INTO @aux1 FROM t_history
255 WHERE TIMEDIFF(end_ts,start_ts) - @sleep_time_per_result_row * @row_count
256       BETWEEN 0 AND @max_acceptable_delay;
257 SELECT @aux1 AS "Expect 1";
259 # 2. The majority of queries (the first one must be ignored) with SLEEP must
260 #    need a reasonable time
261 #    -> If we assume that the result of a cached query will be sent back
262 #       immediate, without any sleep, than the query with SLEEP cannot be cached
263 #       (current and intended behaviour for queries with SLEEP).
264 #    -> It could be also not excluded that the query was cached but the server
265 #       honoured somehow the SLEEP. Such a behaviour would be also acceptable.
266 #    = Replacement for one of the original Bug#12689 tests
267 --echo # Test 2: Does the query with SLEEP need a reasonable time even in case
268 --echo #         of the non first execution?
269 eval SELECT COUNT(*) >= $loops - 1 - 1 INTO @aux2 FROM t_history
270 WHERE TIMEDIFF(end_ts,start_ts) - @sleep_time_per_result_row * @row_count
271       BETWEEN 0 AND @max_acceptable_delay
272       AND attempt > 1;
273 SELECT @aux2 AS "Expect 1";
275 # 3. The query with SLEEP should be not cached.
276 #    -> SHOW STATUS Qcache_queries_in_cache must be not incremented after
277 #       the execution of the query with SLEEP
278 --echo # Test 3: The query with SLEEP must be not cached.
279 eval SELECT COUNT(*) = $loops INTO @aux3 FROM t_history
280 WHERE end_cached = start_cached;
281 SELECT @aux3 AS "Expect 1";
283 # Dump the content of t_history if one of the tests failed.
284 if (`SELECT @aux1 + @aux2 + @aux3 <> 3`)
286    --echo # Some tests failed, dumping the content of t_history
287    SELECT * FROM t_history;
289 DROP TABLE t1;
290 DROP TEMPORARY TABLE t_history;
291 SET @@global.query_cache_size = default;
294 # Bug #21466: INET_ATON() returns signed, not unsigned
297 create table t1 select INET_ATON('255.255.0.1') as `a`;
298 show create table t1;
299 drop table t1;
302 # Bug#26093 (SELECT BENCHMARK() for SELECT statements does not produce
303 #   valid results)
306 --disable_warnings
307 drop table if exists table_26093;
308 drop function if exists func_26093_a;
309 drop function if exists func_26093_b;
310 --enable_warnings
312 create table table_26093(a int);
313 insert into table_26093 values
314 (1), (2), (3), (4), (5),
315 (6), (7), (8), (9), (10);
317 delimiter //;
319 create function func_26093_a(x int) returns int
320 begin
321   set @invoked := @invoked + 1;
322   return x;
323 end//
325 create function func_26093_b(x int, y int) returns int
326 begin
327   set @invoked := @invoked + 1;
328   return x;
329 end//
331 delimiter ;//
333 select avg(a) from table_26093;
335 select benchmark(100, (select avg(a) from table_26093));
337 set @invoked := 0;
338 select benchmark(100, (select avg(func_26093_a(a)) from table_26093));
339 # Returns only 10, since intermediate results are cached.
340 select @invoked;
342 set @invoked := 0;
343 select benchmark(100, (select avg(func_26093_b(a, rand())) from table_26093));
344 # Returns 1000, due to rand() preventing caching.
345 select @invoked;
347 --error ER_SUBQUERY_NO_1_ROW
348 select benchmark(100, (select (a) from table_26093));
350 --error ER_OPERAND_COLUMNS
351 select benchmark(100, (select 1, 1));
353 drop table table_26093;
354 drop function func_26093_a;
355 drop function func_26093_b;
358 # Bug #30832: Assertion + crash with select name_const('test',now());
360 --error ER_WRONG_ARGUMENTS
361 SELECT NAME_CONST('test', NOW());
362 --error ER_WRONG_ARGUMENTS
363 SELECT NAME_CONST('test', UPPER('test'));
365 SELECT NAME_CONST('test', NULL);
366 SELECT NAME_CONST('test', 1);
367 SELECT NAME_CONST('test', -1);
368 SELECT NAME_CONST('test', 1.0);
369 SELECT NAME_CONST('test', -1.0);
370 SELECT NAME_CONST('test', 'test');
373 # Bug #34749: Server crash when using NAME_CONST() with an aggregate function
376 CREATE TABLE t1 (a INT);
377 INSERT INTO t1 VALUES (1),(2),(3);
378 # NAME_CONST() + aggregate.
379 SELECT NAME_CONST('flag',1)    * MAX(a) FROM t1;
380 SELECT NAME_CONST('flag',1.5)  * MAX(a) FROM t1;
381 # Now, wrap the INT_ITEM in Item_func_neg and watch the pretty explosions
382 SELECT NAME_CONST('flag',-1)   * MAX(a) FROM t1;
383 SELECT NAME_CONST('flag',-1.5) * MAX(a) FROM t1;
384 --error ER_WRONG_ARGUMENTS
385 SELECT NAME_CONST('flag', SQRT(4)) * MAX(a) FROM t1;
386 --error ER_WRONG_ARGUMENTS
387 SELECT NAME_CONST('flag',-SQRT(4)) * MAX(a) FROM t1;
388 DROP TABLE t1;
391 # Bug #27545: erroneous usage of NAME_CONST with a name as the first parameter
392 #             resolved against a column name of a derived table hangs the client
395 CREATE TABLE t1 (a int);
396 INSERT INTO t1 VALUES (5), (2);
398 --error ER_WRONG_ARGUMENTS
399 SELECT NAME_CONST(x,2) FROM (SELECT a x FROM t1) t;
401 DROP TABLE t1;
405 # Bug #32559: connection hangs on query with name_const
407 CREATE TABLE t1(a INT);
408 INSERT INTO t1 VALUES (), (), ();
409 --error ER_WRONG_ARGUMENTS
410 SELECT NAME_CONST(a, '1') FROM t1;
411 --error ER_WRONG_ARGUMENTS
412 SET INSERT_ID= NAME_CONST(a, a);
413 DROP TABLE t1;
416 # Bug #31349: ERROR 1062 (23000): Duplicate entry '' for key 'group_key'
418 create table t1 (a int not null);
419 insert into t1 values (-1), (-2);
420 select min(a) from t1 group by inet_ntoa(a);
421 drop table t1;
424 # BUG#34289 - Incorrect NAME_CONST substitution in stored procedures breaks
425 # replication
427 SELECT NAME_CONST('var', 'value') COLLATE latin1_general_cs;
430 # Bug #35848: UUID() returns UUIDs with the wrong time
432 select @@session.time_zone into @save_tz;
434 # make sure all times are UTC so the DayNr won't differ
435 set @@session.time_zone='UTC';
436 select uuid() into @my_uuid;
437 # if version nibble isn't 1, the following calculations will be rubbish
438 select mid(@my_uuid,15,1);
439 select 24 * 60 * 60 * 1000 * 1000 * 10 into @my_uuid_one_day;
440 select concat('0',mid(@my_uuid,16,3),mid(@my_uuid,10,4),left(@my_uuid,8)) into @my_uuidate;
441 select floor(conv(@my_uuidate,16,10)/@my_uuid_one_day) into @my_uuid_date;
442 select 141427 + datediff(curdate(),'1970-01-01') into @my_uuid_synthetic;
443 # these should be identical; date part of UUID should be current date
444 select @my_uuid_date - @my_uuid_synthetic;
446 set @@session.time_zone=@save_tz;
450 # Bug#42014: Crash, name_const with collate
452 CREATE TABLE t1 (a DATE);
453 SELECT * FROM t1 WHERE a = NAME_CONST('reportDate',
454   _binary'2009-01-09' COLLATE 'binary');
455 DROP TABLE t1;
458 # Bug#35515: Aliases of variables in binary log are ignored with NAME_CONST
460 select NAME_CONST('_id',1234) as id;
462 --echo End of 5.0 tests
465 # Bug #30389: connection_id() always return 0 in embedded server
468 select connection_id() > 0;
470 --echo #
471 --echo # Bug #54461: crash with longblob and union or update with subquery
472 --echo #
474 CREATE TABLE t1 (a INT, b LONGBLOB);
475 INSERT INTO t1 VALUES (1, '2'), (2, '3'), (3, '2');
477 SELECT DISTINCT LEAST(a, (SELECT b FROM t1 LIMIT 1)) FROM t1 UNION SELECT 1;
478 SELECT DISTINCT GREATEST(a, (SELECT b FROM t1 LIMIT 1)) FROM t1 UNION SELECT 1;
480 DROP TABLE t1;
484 # Bug #57283: inet_ntoa() crashes
486 SELECT INET_NTOA(0);
487 SELECT '1' IN ('1', INET_NTOA(0));
490 --echo End of tests