# The include statement below is a temp one for tests that are yet to
#be ported to run with InnoDB,
#but needs to be kept for tests that would need MyISAM in future.
--source include/force_myisam_default.inc

--source include/have_debug.inc
--source include/have_debug_sync.inc
--source include/count_sessions.inc
--source include/not_embedded.inc

let GREP_START=`SELECT DATE_FORMAT(CONVERT_TZ(SYSDATE(6),'SYSTEM','UTC'),'%Y%m%d%H%i%s%f');`;

SET @old_debug= @@session.debug;

--echo #
--echo # Bug#59331 filesort with priority queue: handling of out-of-memory
--echo #

CREATE TABLE t1(f0 int auto_increment primary key, f1 int, f2 varchar(200));
INSERT INTO t1(f1, f2) VALUES 
(0,"0"),(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"),
(6,"6"),(7,"7"),(8,"8"),(9,"9"),(10,"10");

set debug= '+d,bounded_queue_init_fail';
--replace_regex /Needed [0-9]* bytes/Needed NN bytes/
--error 5
SELECT * FROM t1 ORDER BY f1 ASC, f0 LIMIT 1;
SET session debug= @old_debug;

DROP TABLE t1;

--echo #
--echo # Bug#36022 please log more information about "Sort aborted" queries
--echo #

CREATE TABLE t1(f0 int auto_increment primary key, f1 int);
INSERT INTO t1(f1) VALUES (0),(1),(2),(3),(4),(5);

SET session debug= '+d,alloc_sort_buffer_fail';
CALL mtr.add_suppression("Out of sort memory");
--error ER_OUT_OF_SORTMEMORY
SELECT * FROM t1 ORDER BY f1 ASC, f0;
SET session debug= @old_debug;

CREATE FUNCTION f1() RETURNS INT RETURN 1;
--error ER_SP_WRONG_NO_OF_ARGS
DELETE FROM t1 ORDER BY (f1(10)) LIMIT 1;

DROP TABLE t1;
DROP FUNCTION f1;

--echo #
--echo # Bug #11747102
--echo # 30771: LOG MORE INFO ABOUT THREADS KILL'D AND SORT ABORTED MESSAGES
--echo #

connect (con1, localhost, root);
connect (con2, localhost, root);

--echo # connection 1
connection con1;
CREATE TABLE t1(f0 int auto_increment primary key, f1 int);
INSERT INTO t1(f1) VALUES (0),(1),(2),(3),(4),(5);

let $ID= `SELECT @id := CONNECTION_ID()`;

SET DEBUG_SYNC='filesort_start SIGNAL filesort_started WAIT_FOR filesort_killed';
--echo # Sending: (not reaped since connection is killed later)
--send SELECT * FROM t1 ORDER BY f1 ASC, f0

--echo # connection 2
connection con2;
let $ignore= `SELECT @id := $ID`;
SET DEBUG_SYNC='now WAIT_FOR filesort_started';
KILL @id;
SET DEBUG_SYNC='now SIGNAL filesort_killed';

--echo # connection default
connection default;
disconnect con1;
disconnect con2;
--source include/wait_until_count_sessions.inc
SET DEBUG_SYNC= "RESET";
DROP TABLE t1;

--echo #
--echo # Bug#13832772 ASSERTION `THD->IS_ERROR() || KILL_ERRNO'
--echo #              FAILED IN FILESORT/MYSQL_DELETE
--echo #

CREATE TABLE t1 (
  c1 BLOB,
  c2 TEXT,
  c3 TEXT,
  c4 TEXT,
  c5 TEXT,
  c6 TEXT,
  c7 TEXT,
  c8 BLOB,
  c9 TEXT,
  c19 TEXT,
  pk INT,
  c20 TEXT,
  c21 BLOB,
  c22 TEXT,
  c23 TEXT,
  c24 TEXT,
  c25 TEXT,
  c26 BLOB,
  c27 TEXT,
  c28 TEXT,
  primary key (pk)
) ENGINE=InnoDB
;

CALL mtr.add_suppression("Out of sort memory");

--error ER_OUT_OF_SORTMEMORY
DELETE IGNORE FROM t1 ORDER BY c26,c7,c23,c4,c25,c5,c20,
c19,c21,c8,c1,c27,c28,c3,c9,c22,c24,c6,c2,pk LIMIT 2;
SHOW WARNINGS;

--error ER_OUT_OF_SORTMEMORY
DELETE FROM t1 ORDER BY c26,c7,c23,c4,c25,c5,c20,
c19,c21,c8,c1,c27,c28,c3,c9,c22,c24,c6,c2,pk LIMIT 2; 
SHOW WARNINGS;

DROP TABLE t1;

--echo #
--echo # Bug#17372396: PLEASE REMOVE SORT ABORTED ERRORS FROM THE ERROR LOG
--echo #

# Code inspired by some solution found in binlog_stm_unsafe_warning.test.
let $log_error_= `SELECT @@GLOBAL.log_error`;
if($log_error_ == "stderr")
{
  # MySQL Server on windows is started with --console and thus
  # does not know the location of its .err log, use default location
  let $log_error_ = $MYSQLTEST_VARDIR/log/mysqld.1.err;
}
# Assign env variable LOG_ERROR
let GREP_FILE=$log_error_;
let GREP_PATTERN=Sort aborted;

CREATE TABLE t1 (a INT,b INT) ENGINE=myisam;
INSERT INTO t1 VALUES (2,4),(7,8);
--error ER_SUBQUERY_NO_1_ROW
SELECT GROUP_CONCAT(DISTINCT t1.a) a FROM t1 WHERE t1.a =
  (SELECT a FROM t1) GROUP BY b;
DROP TABLE t1;

# be very specific with the regex, we have got a format to prove:
--replace_regex /[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9][0-9][0-9][0-9][-+Z][0-9:]* *[0-9]* *\[/DATE_TIME [/ /thread: [0-9]*,/thread: #,/ /Needed [0-9][0-9]* bytes/Needed N bytes/
perl;
   use strict;
   use File::stat;
   my $file= $ENV{'GREP_FILE'} or die("grep file not set");
   my $pattern= $ENV{'GREP_PATTERN'} or die("pattern is not set");
   my $stime= $ENV{'GREP_START'};

   open(FILE, "$file") or die("Unable to open $file: $!");
   while (<FILE>) {
     my $line = $_;
     if ($line =~ /$pattern/) {
       $line =~ /([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])T([0-9][0-9]):([0-9][0-9]):([0-9][0-9])\.([0-9][0-9][0-9][0-9][0-9][0-9])[-+Z][0-9:]* *[0-9]* *?(\[.*)/;
       my $ts=$1.$2.$3.$4.$5.$6.$7;
       my $tx=$8;
       if ($ts >= $stime) {
         print "$line";
       }
     }
   }
   close(FILE);
EOF

--echo #
--echo # Bug#21611270 ASSERTION IN FILESORT::MAKE_SORTORDER()
--echo #

CREATE TABLE g(b INT NOT NULL, UNIQUE(b)) ENGINE=INNODB;
CREATE TABLE t(a int, c int) ENGINE=INNODB;
INSERT INTO t VALUES(1,1);

SELECT 1
FROM t
GROUP BY a
HAVING (SELECT a
      FROM g
      GROUP BY b, a);

DROP TABLE t, g;

--echo #
--echo # Bug #22200984 ASSERTION IN FILESORT::MAKE_SORTORDER()
--echo #

# group by d below is incompatible with sql_mode=only_full_group_by
SET sql_mode="";

CREATE TABLE t1(a_t1 INT, c INT, d INT) ENGINE=INNODB;
CREATE TABLE t2(a_t2 INT NOT NULL, UNIQUE KEY (a_t2)) ENGINE=INNODB;
INSERT INTO t1 VALUES();
SELECT (SELECT 1 FROM t2 GROUP BY d, a_t2 HAVING c) FROM t1 GROUP BY (1=2);
DROP TABLE t1, t2;

SET sql_mode=default;
