#
# This file contains tests covering the parser
#

#=============================================================================
# LEXICAL PARSER (lex)
#=============================================================================

SET @save_sql_mode=@@sql_mode;

#
# Documenting the current behavior, to detect incompatible changes.
# In each cases:
# - no error is the correct result
# - an error is the expected result with the current implementation,
#   and is a limitation.

set SQL_MODE='';

create table ADDDATE(a int);
drop table ADDDATE;
create table ADDDATE (a int);
drop table ADDDATE;

--error ER_PARSE_ERROR
create table BIT_AND(a int);
create table BIT_AND (a int);
drop table BIT_AND;

--error ER_PARSE_ERROR
create table BIT_OR(a int);
create table BIT_OR (a int);
drop table BIT_OR;

--error ER_PARSE_ERROR
create table BIT_XOR(a int);
create table BIT_XOR (a int);
drop table BIT_XOR;

--error ER_PARSE_ERROR
create table CAST(a int);
create table CAST (a int);
drop table CAST;

--error ER_PARSE_ERROR
create table COUNT(a int);
create table COUNT (a int);
drop table COUNT;

--error ER_PARSE_ERROR
create table CURDATE(a int);
create table CURDATE (a int);
drop table CURDATE;

--error ER_PARSE_ERROR
create table CURTIME(a int);
create table CURTIME (a int);
drop table CURTIME;

--error ER_PARSE_ERROR
create table DATE_ADD(a int);
create table DATE_ADD (a int);
drop table DATE_ADD;

--error ER_PARSE_ERROR
create table DATE_SUB(a int);
create table DATE_SUB (a int);
drop table DATE_SUB;

--error ER_PARSE_ERROR
create table EXTRACT(a int);
create table EXTRACT (a int);
drop table EXTRACT;

--error ER_PARSE_ERROR
create table GROUP_CONCAT(a int);
create table GROUP_CONCAT (a int);
drop table GROUP_CONCAT;

# Limitation removed in 5.1
create table GROUP_UNIQUE_USERS(a int);
drop table GROUP_UNIQUE_USERS;
create table GROUP_UNIQUE_USERS (a int);
drop table GROUP_UNIQUE_USERS;

--error ER_PARSE_ERROR
create table MAX(a int);
create table MAX (a int);
drop table MAX;

--error ER_PARSE_ERROR
create table MID(a int);
create table MID (a int);
drop table MID;

--error ER_PARSE_ERROR
create table MIN(a int);
create table MIN (a int);
drop table MIN;

--error ER_PARSE_ERROR
create table NOW(a int);
create table NOW (a int);
drop table NOW;

--error ER_PARSE_ERROR
create table POSITION(a int);
create table POSITION (a int);
drop table POSITION;

create table SESSION_USER(a int);
drop table SESSION_USER;
create table SESSION_USER (a int);
drop table SESSION_USER;

--error ER_PARSE_ERROR
create table STD(a int);
create table STD (a int);
drop table STD;

--error ER_PARSE_ERROR
create table STDDEV(a int);
create table STDDEV (a int);
drop table STDDEV;

--error ER_PARSE_ERROR
create table STDDEV_POP(a int);
create table STDDEV_POP (a int);
drop table STDDEV_POP;

--error ER_PARSE_ERROR
create table STDDEV_SAMP(a int);
create table STDDEV_SAMP (a int);
drop table STDDEV_SAMP;

create table SUBDATE(a int);
drop table SUBDATE;
create table SUBDATE (a int);
drop table SUBDATE;

--error ER_PARSE_ERROR
create table SUBSTR(a int);
create table SUBSTR (a int);
drop table SUBSTR;

--error ER_PARSE_ERROR
create table SUBSTRING(a int);
create table SUBSTRING (a int);
drop table SUBSTRING;

--error ER_PARSE_ERROR
create table SUM(a int);
create table SUM (a int);
drop table SUM;

--error ER_PARSE_ERROR
create table SYSDATE(a int);
create table SYSDATE (a int);
drop table SYSDATE;

create table SYSTEM_USER(a int);
drop table SYSTEM_USER;
create table SYSTEM_USER (a int);
drop table SYSTEM_USER;

--error ER_PARSE_ERROR
create table TRIM(a int);
create table TRIM (a int);
drop table TRIM;

# Limitation removed in 5.1
create table UNIQUE_USERS(a int);
drop table UNIQUE_USERS;
create table UNIQUE_USERS (a int);
drop table UNIQUE_USERS;

--error ER_PARSE_ERROR
create table VARIANCE(a int);
create table VARIANCE (a int);
drop table VARIANCE;

--error ER_PARSE_ERROR
create table VAR_POP(a int);
create table VAR_POP (a int);
drop table VAR_POP;

--error ER_PARSE_ERROR
create table VAR_SAMP(a int);
create table VAR_SAMP (a int);
drop table VAR_SAMP;

set SQL_MODE='IGNORE_SPACE';

create table ADDDATE(a int);
drop table ADDDATE;
create table ADDDATE (a int);
drop table ADDDATE;

--error ER_PARSE_ERROR
create table BIT_AND(a int);
--error ER_PARSE_ERROR
create table BIT_AND (a int);

--error ER_PARSE_ERROR
create table BIT_OR(a int);
--error ER_PARSE_ERROR
create table BIT_OR (a int);

--error ER_PARSE_ERROR
create table BIT_XOR(a int);
--error ER_PARSE_ERROR
create table BIT_XOR (a int);

--error ER_PARSE_ERROR
create table CAST(a int);
--error ER_PARSE_ERROR
create table CAST (a int);

--error ER_PARSE_ERROR
create table COUNT(a int);
--error ER_PARSE_ERROR
create table COUNT (a int);

--error ER_PARSE_ERROR
create table CURDATE(a int);
--error ER_PARSE_ERROR
create table CURDATE (a int);

--error ER_PARSE_ERROR
create table CURTIME(a int);
--error ER_PARSE_ERROR
create table CURTIME (a int);

--error ER_PARSE_ERROR
create table DATE_ADD(a int);
--error ER_PARSE_ERROR
create table DATE_ADD (a int);

--error ER_PARSE_ERROR
create table DATE_SUB(a int);
--error ER_PARSE_ERROR
create table DATE_SUB (a int);

--error ER_PARSE_ERROR
create table EXTRACT(a int);
--error ER_PARSE_ERROR
create table EXTRACT (a int);

--error ER_PARSE_ERROR
create table GROUP_CONCAT(a int);
--error ER_PARSE_ERROR
create table GROUP_CONCAT (a int);

# Limitation removed in 5.1
create table GROUP_UNIQUE_USERS(a int);
drop table GROUP_UNIQUE_USERS;
create table GROUP_UNIQUE_USERS (a int);
drop table GROUP_UNIQUE_USERS;

--error ER_PARSE_ERROR
create table MAX(a int);
--error ER_PARSE_ERROR
create table MAX (a int);

--error ER_PARSE_ERROR
create table MID(a int);
--error ER_PARSE_ERROR
create table MID (a int);

--error ER_PARSE_ERROR
create table MIN(a int);
--error ER_PARSE_ERROR
create table MIN (a int);

--error ER_PARSE_ERROR
create table NOW(a int);
--error ER_PARSE_ERROR
create table NOW (a int);

--error ER_PARSE_ERROR
create table POSITION(a int);
--error ER_PARSE_ERROR
create table POSITION (a int);

create table SESSION_USER(a int);
drop table SESSION_USER;
create table SESSION_USER (a int);
drop table SESSION_USER;

--error ER_PARSE_ERROR
create table STD(a int);
--error ER_PARSE_ERROR
create table STD (a int);

--error ER_PARSE_ERROR
create table STDDEV(a int);
--error ER_PARSE_ERROR
create table STDDEV (a int);

--error ER_PARSE_ERROR
create table STDDEV_POP(a int);
--error ER_PARSE_ERROR
create table STDDEV_POP (a int);

--error ER_PARSE_ERROR
create table STDDEV_SAMP(a int);
--error ER_PARSE_ERROR
create table STDDEV_SAMP (a int);

create table SUBDATE(a int);
drop table SUBDATE;
create table SUBDATE (a int);
drop table SUBDATE;

--error ER_PARSE_ERROR
create table SUBSTR(a int);
--error ER_PARSE_ERROR
create table SUBSTR (a int);

--error ER_PARSE_ERROR
create table SUBSTRING(a int);
--error ER_PARSE_ERROR
create table SUBSTRING (a int);

--error ER_PARSE_ERROR
create table SUM(a int);
--error ER_PARSE_ERROR
create table SUM (a int);

--error ER_PARSE_ERROR
create table SYSDATE(a int);
--error ER_PARSE_ERROR
create table SYSDATE (a int);

create table SYSTEM_USER(a int);
drop table SYSTEM_USER;
create table SYSTEM_USER (a int);
drop table SYSTEM_USER;

--error ER_PARSE_ERROR
create table TRIM(a int);
--error ER_PARSE_ERROR
create table TRIM (a int);

# Limitation removed in 5.1
create table UNIQUE_USERS(a int);
drop table UNIQUE_USERS;
create table UNIQUE_USERS (a int);
drop table UNIQUE_USERS;

--error ER_PARSE_ERROR
create table VARIANCE(a int);
--error ER_PARSE_ERROR
create table VARIANCE (a int);

--error ER_PARSE_ERROR
create table VAR_POP(a int);
--error ER_PARSE_ERROR
create table VAR_POP (a int);

--error ER_PARSE_ERROR
create table VAR_SAMP(a int);
--error ER_PARSE_ERROR
create table VAR_SAMP (a int);

SET @@sql_mode=@save_sql_mode;

#=============================================================================
# SYNTACTIC PARSER (bison)
#=============================================================================

#
#
# Bug#21114 (Foreign key creation fails to table with name format)
# 

# Test coverage with edge conditions

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select pi(3.14);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select tan();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select tan(1, 2);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select makedate(1);
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select makedate(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select maketime();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select maketime(1);
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select maketime(1, 2);
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select maketime(1, 2, 3, 4);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select atan();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select atan2(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select concat();
select concat("foo");

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select concat_ws();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select concat_ws("foo");

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select encrypt();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select encrypt(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select des_encrypt("p1", "p2", "not expected");
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select des_decrypt("p1", "p2", "not expected");

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select elt();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select elt(1);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select export_set();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select export_set("p1");
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select export_set("p1", "p2");
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select export_set("p1", "p2", "p3", "p4", "p5", "p6");

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select field();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select field("p1");

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select from_unixtime();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select from_unixtime(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select unix_timestamp(1, 2);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select greatest();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select greatest(12);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select last_insert_id(1, 2);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select least();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select least(12);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select locate();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select locate(1);
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select locate(1, 2, 3, 4);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select log();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select log(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select make_set();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select make_set(1);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select master_pos_wait();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select master_pos_wait(1);
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select master_pos_wait(1, 2, 3, 4);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select rand(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select round(1, 2, 3);

-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select yearweek();
-- error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
select yearweek(1, 2, 3);

#
# Bug#24736: UDF functions parsed as Stored Functions
#

# Verify that the syntax for calling UDF : foo(expr AS param, ...)
# can not be used when calling native functions

# Native function with 1 argument

select abs(3);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select abs(3 AS three);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select abs(3 three);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select abs(3 AS "three");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select abs(3 "three");

# Native function with 2 arguments

set @bar="bar";
set @foobar="foobar";

select instr("foobar", "bar");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar" AS p1, "bar");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar" p1, "bar");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar" AS "p1", "bar");
## String concatenation, valid syntax
select instr("foobar" "p1", "bar");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr(@foobar "p1", "bar");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar", "bar" AS p2);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar", "bar" p2);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar", "bar" AS "p2");
## String concatenation, valid syntax
select instr("foobar", "bar" "p2");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar", @bar "p2");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select instr("foobar" AS p1, "bar" AS p2);

# Native function with 3 arguments

select conv(255, 10, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255 AS p1, 10, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255 p1, 10, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255 AS "p1", 10, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255 "p1", 10, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10 AS p2, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10 p2, 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10 AS "p2", 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10 "p2", 16);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10, 16 AS p3);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10, 16 p3);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10, 16 AS "p3");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255, 10, 16 "p3");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select conv(255 AS p1, 10 AS p2, 16 AS p3);

# Native function with a variable number of arguments

select atan(10);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 AS p1);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 p1);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 AS "p1");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 "p1");

select atan(10, 20);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 AS p1, 20);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 p1, 20);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 AS "p1", 20);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 "p1", 20);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10, 20 AS p2);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10, 20 p2);
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10, 20 AS "p2");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10, 20 "p2");
-- error ER_WRONG_PARAMETERS_TO_NATIVE_FCT
select atan(10 AS p1, 20 AS p2);

