SET sql_mode=ORACLE;
#
# Start of 11.7 tests
#
#
# MDEV-12252 ROW data type for stored function return values
#
CREATE FUNCTION f1 RETURN t1.a%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END;
$$
SHOW CREATE FUNCTION f1;
Function	sql_mode	Create Function	character_set_client	collation_connection	Database Collation
f1	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT	CREATE DEFINER="root"@"localhost" FUNCTION "f1"() RETURN "test"."t1"."a"%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
SELECT f1();
ERROR 42S02: Table 'test.t1' doesn't exist
CREATE TABLE t1 (b INT);
SELECT f1();
ERROR 42S22: Unknown column 'a' in 't1'
DROP TABLE t1;
DROP FUNCTION f1;
CREATE DATABASE db1;
CREATE FUNCTION db1.f1 RETURN db1.t1.a%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END;
$$
SHOW CREATE FUNCTION db1.f1;
Function	sql_mode	Create Function	character_set_client	collation_connection	Database Collation
f1	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT	CREATE DEFINER="root"@"localhost" FUNCTION "f1"() RETURN "db1"."t1"."a"%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
SELECT db1.f1();
ERROR 42S02: Table 'db1.t1' doesn't exist
CREATE TABLE db1.t1 (b TIME);
SELECT db1.f1();
ERROR 42S22: Unknown column 'a' in 't1'
DROP TABLE db1.t1;
CREATE TABLE db1.t1 (a TIME);
SELECT db1.f1();
db1.f1()
NULL
INSERT INTO db1.t1 VALUES ('10:20:30');
SELECT db1.f1();
db1.f1()
10:20:30
DROP TABLE db1.t1;
DROP FUNCTION db1.f1;
DROP DATABASE db1;
CREATE TABLE t1 (a TIME);
CREATE FUNCTION f1 RETURN test.t1.a%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END;
$$
SHOW CREATE FUNCTION f1;
Function	sql_mode	Create Function	character_set_client	collation_connection	Database Collation
f1	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT	CREATE DEFINER="root"@"localhost" FUNCTION "f1"() RETURN "test"."t1"."a"%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
SELECT f1();
f1()
NULL
DROP FUNCTION f1;
DROP TABLE t1;
CREATE TABLE t1 (a TIME);
CREATE FUNCTION f1 RETURN t1.a%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END;
$$
SHOW CREATE FUNCTION f1;
Function	sql_mode	Create Function	character_set_client	collation_connection	Database Collation
f1	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT	CREATE DEFINER="root"@"localhost" FUNCTION "f1"() RETURN "test"."t1"."a"%TYPE
AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
SELECT f1();
f1()
NULL
CREATE TABLE t2 AS SELECT f1();
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "f1()" time DEFAULT NULL
)
SELECT * FROM t2;
f1()
NULL
DROP TABLE t2;
INSERT INTO t1 VALUES ('10:20:30');
SELECT f1();
f1()
10:20:30
CREATE TABLE t2 AS SELECT f1();
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "f1()" time DEFAULT NULL
)
SELECT * FROM t2;
f1()
10:20:30
DROP TABLE t2;
DROP FUNCTION f1;
DROP TABLE t1;
#
# MDEV-35003 Server crashes when reading routines table after creating a function with "returns type of"
#
CREATE FUNCTION f1(a t1.a%TYPE) RETURN t1.a%TYPE AS
BEGIN
RETURN (SELECT min(a) FROM t1);
END;
$$
SELECT ROUTINE_NAME, DATA_TYPE, DTD_IDENTIFIER
FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA='test';
ROUTINE_NAME	f1
DATA_TYPE	TYPE OF
DTD_IDENTIFIER	"test"."t1"."a"%TYPE
SELECT SPECIFIC_NAME, ORDINAL_POSITION, PARAMETER_NAME, DATA_TYPE, DTD_IDENTIFIER
FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_SCHEMA='test';
SPECIFIC_NAME	f1
ORDINAL_POSITION	0
PARAMETER_NAME	NULL
DATA_TYPE	TYPE OF
DTD_IDENTIFIER	"test"."t1"."a"%TYPE
SPECIFIC_NAME	f1
ORDINAL_POSITION	1
PARAMETER_NAME	a
DATA_TYPE	TYPE OF
DTD_IDENTIFIER	"t1"."a"%TYPE
DROP FUNCTION f1;
#
# End of 11.7 tests
#
