--echo #
--echo # Test of various abuses of the plugin.
--echo #

--source suite/query_rewrite_plugins/include/have_plugin_rewriter.inc
SET sql_mode = 'PIPES_AS_CONCAT';
CALL mtr.add_suppression("Plugin rewriter reported: " ||
                         "'Some rules failed to load.'");

CALL mtr.add_suppression("Plugin rewriter reported: " ||
                         "'Wrong column count or names when loading rules.'");


--echo # In theory a user might try to install the UDF without the plugin. In
--echo # this case, we should exit gracefully with an informative error.

--replace_regex /\.so|\.dll/.xxx/
eval CREATE FUNCTION load_rewrite_rules RETURNS STRING SONAME '$REWRITER';

--error ER_CANT_INITIALIZE_UDF
SELECT load_rewrite_rules();
DROP FUNCTION load_rewrite_rules;

CREATE TABLE t1 ( c1 VARCHAR(10), c2 VARCHAR(10) );
INSERT INTO t1 VALUES( 'abc', 'def' ), ( 'ghi', 'klm' ), ( 'nop', 'qrs' );

CREATE TABLE t2( c1 VARCHAR(10) );
INSERT INTO t2 VALUES ( 'tuv' ), ( 'wxy' );

--echo #
--echo # Create a table with insufficient number of columns.
--echo #
CREATE DATABASE query_rewrite;
CREATE TABLE query_rewrite.rewrite_rules ( pattern INT, replacement INT );

--source suite/query_rewrite_plugins/include/install_rewriter.inc

SELECT c1 FROM t1;

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

--source suite/query_rewrite_plugins/include/load_error_log.inc

--echo # Avoid seeing any other error that might be there.
SELECT search_error_log( '[Error]', "%Plugin Rewriter reported: 'Wrong%" );
SHOW STATUS LIKE 'Rewriter_number_reloads';

--source suite/query_rewrite_plugins/include/uninstall_rewriter.inc
--source include/disconnect_connections.inc

--echo # Create another table with insufficient number of columns.
CREATE DATABASE query_rewrite;
CREATE TABLE query_rewrite.rewrite_rules ( enabled CHAR(1) DEFAULT 'Y' );

--source suite/query_rewrite_plugins/include/install_rewriter.inc

--source suite/query_rewrite_plugins/include/load_error_log.inc
--echo # Avoid seeing any other error that might be there.
SELECT search_error_log( '[Error]', "%Plugin Rewriter reported: 'Wrong%" );
SHOW STATUS LIKE 'Rewriter_number_reloads';

SELECT c1 FROM t1;

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

--source suite/query_rewrite_plugins/include/uninstall_rewriter.inc
--source include/disconnect_connections.inc

--source suite/query_rewrite_plugins/include/install_rewriter.inc

--echo #
--echo # We now try 6 rules with various errors in them:
--echo #

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

--echo # 1. Syntax error in the replacement.
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'SELECT c1 FROM test.t1 WHERE c1 = ?', 'SELECT c1 FROM test.t2 WHERE q c1 = ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT c1 FROM test.t1 WHERE c1 = 'abc';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # 2. No error.
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'SELECT c1 FROM test.t1', 'SELECT * FROM test.t1' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # 3. Syntax error in the pattern.
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'SELECT c1 FROM test.t1 WHERE dqdq c1 = ? OR c2 = ?',
         'SELECT * FROM test.t1 WHERE c2 = ? OR c1 = ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # 4 Empty/NULL replacement/pattern
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'SELECT c1 FROM test.t1 WHERE c2 = ?', '' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM query_rewrite.rewrite_rules;

DELETE FROM query_rewrite.rewrite_rules
WHERE pattern = 'SELECT c1 FROM test.t1 WHERE c2 = ?';

ALTER TABLE query_rewrite.rewrite_rules
MODIFY COLUMN replacement VARCHAR(10000) CHARACTER SET utf8 COLLATE utf8_bin;

INSERT INTO query_rewrite.rewrite_rules ( pattern )
VALUES ( 'SELECT c1 FROM test.t1 WHERE c2 = ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM query_rewrite.rewrite_rules;

DELETE FROM query_rewrite.rewrite_rules
WHERE pattern = 'SELECT c1 FROM test.t1 WHERE c2 = ?';

ALTER TABLE query_rewrite.rewrite_rules
MODIFY COLUMN pattern VARCHAR(10000) CHARACTER SET utf8 COLLATE utf8_bin;

INSERT INTO query_rewrite.rewrite_rules ( replacement )
VALUES ( 'SELECT c1 FROM test.t1 WHERE c2 = ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # 5 More parameters in the replacement than the pattern
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES
( 'SELECT * FROM test.t1 WHERE c1 = ?', 'SELECT * FROM test.t2 WHERE c2 = ? AND c1 = ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT * FROM test.t1 WHERE c1 = 'abc';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # 6 A replacement that does not have syntax errors in itself, but the
--echo # rewritten query does.
INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'SELECT "Fail, please", ?', 'SELECT 1 LIMIT ?' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SELECT * FROM query_rewrite.rewrite_rules;

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SET @@global.rewriter_verbose = 2;

--error ER_PARSE_ERROR
SELECT 'Fail, please', 'a';

--echo # Check that the one rule without errors is still rewritten and executed.
SELECT c1 FROM test.t1;

--echo # Fix rule 1 (error in the replacement.)
UPDATE query_rewrite.rewrite_rules
SET replacement = 'SELECT c1 FROM test.t2 WHERE c1 = ?', enabled = 'YES'
WHERE pattern = 'SELECT c1 FROM test.t1 WHERE c1 = ?';

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SHOW STATUS LIKE 'Rewriter_number_loaded_rules';

SELECT c1 FROM test.t1 WHERE c1 = 'abc';
SELECT c1 FROM test.t2 WHERE c1 = 'tuv';

SELECT * FROM query_rewrite.rewrite_rules;

--echo # Check that the rule without error is still executed and rewritten.
SELECT c1 FROM test.t1;

--echo # Remove all the rules that cause errors.
DELETE FROM query_rewrite.rewrite_rules WHERE pattern <> 'SELECT c1 FROM test.t1';
DELETE FROM query_rewrite.rewrite_rules WHERE pattern IS NULL;
DELETE FROM query_rewrite.rewrite_rules WHERE replacement IS NULL;

SHOW STATUS LIKE 'Rewriter_reload_error';
SHOW STATUS LIKE 'Rewriter_number_loaded_rules';
SHOW STATUS LIKE 'Rewriter_number_reloads';

--echo # Check that a rule that used to have syntax error now works fine.
SELECT c1 FROM t1 WHERE c1 = 'abc';

--echo # Check that the rule without error is still executed and rewritten.
SELECT c1 FROM test.t1;

--echo # A rule with parse error followed by a correct rule should still turn
--echo # Rewriter_reload_error to true.
DELETE FROM query_rewrite.rewrite_rules;

INSERT INTO query_rewrite.rewrite_rules ( pattern, replacement )
VALUES ( 'error', 'error' ),
       ( 'select 1', 'select 2' );

--error ER_SIGNAL_EXCEPTION
CALL query_rewrite.flush_rewrite_rules();

SELECT * FROM query_rewrite.rewrite_rules;

SHOW STATUS LIKE 'Rewriter_reload_error';

--echo #
--echo # Test of what happens if pfs digests are turned off.
--echo #

UPDATE performance_schema.setup_consumers
SET enabled = 'NO'
WHERE NAME = 'statements_digest';

--echo # Rewrites should still work.
SELECT 1;

PREPARE stmt1 FROM 'SELECT 1';
EXECUTE stmt1;

--echo # Restore the setting.
UPDATE performance_schema.setup_consumers
SET enabled = 'YES'
WHERE NAME = 'statements_digest';

--echo # Let's try to uninstall the plugin the wrong way, that is, not using the
--echo # script. The library will not be unloaded so we can still call the UDF
--echo # but it should return an error.
UNINSTALL PLUGIN rewriter;
--source include/disconnect_connections.inc

--error ER_CANT_INITIALIZE_UDF
CALL query_rewrite.flush_rewrite_rules();
--error ER_CANT_INITIALIZE_UDF
SELECT load_rewrite_rules();

--source suite/query_rewrite_plugins/include/uninstall_rewriter.inc
--source suite/query_rewrite_plugins/include/drop_error_log.inc

DROP TABLE t1, t2;
