#
# BUG#49100: RBR: Unexpected behavior when AI contains no usable data 
#            for slave columns

-- connection server_1

-- let $row_img_set=server_1:MINIMAL:N,server_2:MINIMAL:Y,server_3:MINIMAL:Y
-- source include/rpl_row_img_set.inc

-- echo #### case #1: AI: no values logged

-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
DROP TABLE t1;
-- source include/rpl_sync.inc

-- echo #### case #2: AI: not empty but slave does not have usable data for its columns (INSERT)

-- connection server_3
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine

-- connection server_2
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine
SET SQL_LOG_BIN=1;

-- connection server_1
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100, c2 int, primary key(c2)) Engine=$engine
SET SQL_LOG_BIN=1;

INSERT INTO t1(c2) VALUES (1);
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
DROP TABLE t1;
-- source include/rpl_sync.inc

-- echo #### case #3: BI: usable columns on the slave, AI: no usable columns on the slave

-- connection server_3
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine

-- connection server_2
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine
SET SQL_LOG_BIN=1;

-- connection server_1
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100, c2 int) Engine=$engine
SET SQL_LOG_BIN=1;

INSERT INTO t1 VALUES (1,1);
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
UPDATE t1 SET c2=2 WHERE c1=1 AND c2=1;
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
DROP TABLE t1;
-- source include/rpl_sync.inc

-- echo #### case #4: AI, BI: no usable columns on the slave (NOOP UPDATE).
-- echo ####          

if ($verbose)
{
  ## CLEAN LOGS (to show clean events later)

  --source include/rpl_reset.inc
}

-- connection server_3
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine

-- connection server_2
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100) Engine=$engine
SET SQL_LOG_BIN=1;

-- connection server_1
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 int DEFAULT 100, c2 int, c3 int, primary key(c2)) Engine=$engine
SET SQL_LOG_BIN=1;

INSERT INTO t1 VALUES (1,1,1);
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
UPDATE t1 SET c3=300 WHERE c2=1;
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
DROP TABLE t1;
-- source include/rpl_sync.inc

if ($verbose)
{
  # show that 2nd slave does not log the update (see: BUG#52473)
  -- connection server_1
  -- echo # Master events
  -- source include/show_binlog_events.inc
  -- connection server_2
  -- echo # First slave events
  -- source include/show_binlog_events.inc
  -- connection server_1
}

-- echo #### case #5: BI: no usable columns on the slave, AI: usable columns on the slave (slave must stop).
-- echo #### 


if ($verbose)
{
  ## CLEAN LOGS
  --source include/rpl_reset.inc
}

-- connection server_3
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 INT DEFAULT 100) Engine=$engine

-- connection server_2
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 INT DEFAULT 100) Engine=$engine
SET SQL_LOG_BIN=1;

-- connection server_1
SET SQL_LOG_BIN=0;
-- replace_result $engine ENGINE
-- eval CREATE TABLE t1 (c1 INT DEFAULT 100, c2 INT PRIMARY KEY) Engine=$engine
SET SQL_LOG_BIN=1;

INSERT INTO t1 VALUES (1,1);
SELECT * FROM t1;
-- source include/rpl_sync.inc
-- connection server_2
SELECT * FROM t1;
-- connection server_3
SELECT * FROM t1;

-- connection server_1
UPDATE t1 SET c1=300 WHERE c2=1;
SELECT * FROM t1;
-- connection server_2

-- let $lower_engine= `SELECT LOWER('$engine')`
if ($lower_engine == ndb)
{
  if ($verbose)
  {
    -- echo # scenario #5 skipped in NDB  (see: BUG#52473).
    # show events logged in server_1. There will be two Write_rows events
    # instead of one Write_rows and one Update_rows log events.
    -- connection server_1
    -- source include/show_binlog_events.inc
  }
}

SET SQL_LOG_BIN=0;
call mtr.add_suppression("Slave: Can\'t find record in \'t1\' Error_code: 1032");
call mtr.add_suppression("Slave SQL for channel '': .*Could not execute Update_rows event on table test.t1; Can.t find record in .t1.* Error_code: 1032");
call mtr.add_suppression("The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state");

SET SQL_LOG_BIN=1;

# NOTE: Because of BUG#52473, when using NDB this will make the test
#       wait forever.
# 1032 = ER_KEY_NOT_FOUND
--let $slave_sql_errno= 1032
--let $not_switch_connection= 1
-- source include/wait_for_slave_sql_error_and_skip.inc

## CLEAN UP

-- connection server_1
DROP TABLE t1;
-- source include/rpl_sync.inc

-- let $row_img_set=server_1:FULL:N,server_2:FULL:Y,server_3:FULL:Y
-- source include/rpl_row_img_set.inc
