--echo #
--echo # MDEV-20034 Add support for the pre-defined weak SYS_REFCURSOR
--echo #

DELIMITER /;

CREATE FUNCTION f1() RETURNS SYS_REFCURSOR
BEGIN
  DECLARE c0 SYS_REFCURSOR;
  OPEN c0 FOR SELECT 1;
  RETURN c0;
END;
/
DELIMITER ;/

--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CREATE VIEW v1 AS SELECT f1() AS c1;

--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CREATE VIEW v1 AS SELECT f1() IS NULL AS c1;

--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CREATE VIEW v1 AS SELECT 1 AS c1 WHERE f1() IS NOT NULL;

--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CREATE VIEW v1 AS SELECT f1() AS c1 WHERE f1() IS NOT NULL;

--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
CREATE VIEW v1 AS SELECT ROW(f1(),1)=ROW(f1(),1) AS c1;

DELIMITER /;
CREATE PROCEDURE p1()
BEGIN
  DECLARE c0 SYS_REFCURSOR;
  CREATE VIEW v1 AS SELECT f1() AS c1;
END
/
DELIMITER ;/
--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CALL p1;
DROP PROCEDURE p1;


DELIMITER /;
CREATE PROCEDURE p1(stmt TEXT)
BEGIN
  DECLARE c0 SYS_REFCURSOR;
  EXECUTE IMMEDIATE stmt;
END
/
DELIMITER ;/
--error ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION
CALL p1('CREATE VIEW v1 AS SELECT 1 AS c1 WHERE f1() IS NOT NULL');
DROP PROCEDURE p1;


DROP FUNCTION f1;
