# name: test/sql/peg_parser/parser/support_unreserved_keywords.test
# description: Test supporting different categories of keywords in peg parser
# group: [parser]

require autocomplete

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE OR REPLACE TABLE 'everflow_daily' AS SELECT *$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE tmp.t1(id int);$TEST_PEG_PARSER$);

require tpch

statement ok
CREATE SCHEMA abcdefgh;

query II
SELECT suggestion, suggestion_start FROM sql_auto_complete('CREATE TABLE abcd') LIMIT 1;
----
abcdefgh.	13


statement ok
CALL dbgen(sf=0);

query II
SELECT suggestion, suggestion_start FROM sql_auto_complete('INSERT INTO lin') LIMIT 1;
----
lineitem	12

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE database.schema.table(foo integer);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE MACRO foo(a,b) as TABLE select *;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$INSERT INTO database.schema.table VALUES ({'field': 42});$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$FROM database.schema.table;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select * from '{DATA_DIR}/csv/nullbyte.csv';$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE foo(try_cast INTEGER);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT "hello world".i FROM foo AS 'hello world'$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$FROM information_schema.schema$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$FROM information_schema.columns$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$DROP TABLE IF EXISTS map;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE map(v INTEGER);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$RESET VARIABLE foo$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select left(b collate de, 6)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT v.trim('><')$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE bits (b bit);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE issue14384(i INTERVAL);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT cast(1 as INT)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT 1::SMALLINT$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$copy integers to '{TEMP_DIR}/integers.json.gz' (FORMAT JSON, COMPRESSION GZIP)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE OR REPLACE TABLE t(      x VARCHAR USING COMPRESSION Dictionary );$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select distinct on (types) types from (select vector_type(a) from test limit 8192) tbl(types)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$create table data(id integer, value integer);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$create table t2 (id int, v_map struct(foo integer[]));$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$ATTACH '{TEMP_DIR}/encrypted.duckdb' AS encrypted (ENCRYPTION_KEY 'asdf');$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT COUNT(*) FROM glob('{TEMP_DIR}/attach_no_wal.db.wal');$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$prepare p4 as select $name, $other_name$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE testjson AS SELECT JSON '{ "key" : "value" }';$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$COPY FROM DATABASE unencrypted TO encrypted;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$INSERT INTO hero (name, secret_name, age) VALUES (1) ON CONFLICT (name) DO UPDATE SET secret_name = EXCLUDED.secret_name, age = EXCLUDED.age;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT list_reduce(s, lambda curr, next: struct_pack(a:=curr.a + (next.b - curr.b), b:=next.b)) FROM df$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select features[1].name;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT last(value ORDER BY foo IGNORE NULLS)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select UNPACK(a)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select sum(salary) over (order by enroll_date range between '1 year' preceding and '1 year' following)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TEMPORARY MACRO my_seq(start, finish, stride:=3) as TABLE SELECT *;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE history(id TEXT, type TEXT, PRIMARY KEY(id, type));$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$UNPIVOT PivotedCities ON 2000, 2010, 2020 INTO NAME YEAR VALUE Population;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$WITH data AS (     SELECT 1 as a, 2 as b, 3 as c ) SELECT d FROM data d$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$insert into data (id, value) select 1$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT UNNEST({'a': 42, 'b': {'c': 88, 'd': 99}}, recursive := true)$TEST_PEG_PARSER$);

# Reserved keyword as explicit alias
statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2 as end;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$CREATE TABLE big_tbl AS SELECT i, concat('thisisalongstring', i) as str FROM range(10000000) t(i);$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select distinct on (a) b from (select s a, md5(s::text)) limit 10;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$select distinct on (a) b from (select s a, md5(s::double)) limit 10;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT histogram_exact((date '2000-01-01' + interval (n) days))$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT MAP{ 0 : 0 } FROM lineitem$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT l_returnflag,       map {   '0': case when sum(1) filter(l_orderkey <= 0) is null then 0 end,       '1000000': sum(1) filter(l_orderkey > 0 and l_orderkey <= 1000000),         '2000000': sum(1) filter(l_orderkey > 1000000 and l_orderkey <= 2000000),       '3000000': sum(1) filter(l_orderkey > 2000000 and l_orderkey <= 3000000),   '4000000': sum(1) filter(l_orderkey > 3000000 and l_orderkey <= 4000000),       '5000000': sum(1) filter(l_orderkey > 4000000 and l_orderkey <= 5000000),   '6000000': sum(1) filter(l_orderkey > 5000000 and l_orderkey <= 6000000)        } FROM lineitem GROUP BY l_returnflag ORDER BY l_returnflag$TEST_PEG_PARSER$);

# Non keyword as explicit alias
statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2 as foo;$TEST_PEG_PARSER$);

# Non keyword implicit alias
statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2 foo;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT foo: c2;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2 'foo';$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2, c1 FROM (SELECT a)$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT number1, number2 SIMILAR TO '0' as a;$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT number1, number2 SIMILAR TO '0' as 'a';$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT * SIMILAR TO '0' as 'a';$TEST_PEG_PARSER$);

statement ok
CALL check_peg_parser($TEST_PEG_PARSER$SELECT c2, c1 FROM ( SELECT * SIMILAR TO 'number(\d+)' AS 'c\1' FROM (SELECT 1 as number1, 2 as number2, 3 as end) );$TEST_PEG_PARSER$);
