From a0dd18b1f3d294abc68293f2a4a9d4d9be220833 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sun, 4 Feb 2024 01:15:35 +0800 Subject: [PATCH] feat: add more and more test Signed-off-by: Chojan Shang --- src/query/ast/tests/it/parser.rs | 7 + .../ast/tests/it/testdata/statement-error.txt | 15 ++ src/query/ast/tests/it/testdata/statement.txt | 248 ++++++++++++++++++ .../base/05_ddl/05_0010_ddl_create_udf.test | 15 ++ 4 files changed, 285 insertions(+) diff --git a/src/query/ast/tests/it/parser.rs b/src/query/ast/tests/it/parser.rs index cbccf4695f9a..032d50875360 100644 --- a/src/query/ast/tests/it/parser.rs +++ b/src/query/ast/tests/it/parser.rs @@ -556,6 +556,12 @@ fn test_statement() { "GRANT OWNERSHIP ON d20_0014.t TO ROLE 'd20_0015_owner';", "GRANT OWNERSHIP ON STAGE s1 TO ROLE 'd20_0015_owner';", "GRANT OWNERSHIP ON UDF f1 TO ROLE 'd20_0015_owner';", + "CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p));", + "CREATE OR REPLACE FUNCTION isnotempty_test_replace AS(p) -> not(is_null(p)) DESC = 'This is a description';", + "CREATE FUNCTION binary_reverse (BINARY) RETURNS BINARY LANGUAGE python HANDLER = 'binary_reverse' ADDRESS = 'http://0.0.0.0:8815';", + "CREATE OR REPLACE FUNCTION binary_reverse (BINARY) RETURNS BINARY LANGUAGE python HANDLER = 'binary_reverse' ADDRESS = 'http://0.0.0.0:8815';", + "DROP FUNCTION binary_reverse;", + "DROP FUNCTION isnotempty;", ]; for case in cases { @@ -651,6 +657,7 @@ fn test_statement_error() { "REVOKE OWNERSHIP ON d20_0014.* FROM USER A;", "REVOKE OWNERSHIP ON d20_0014.* FROM ROLE A;", "GRANT OWNERSHIP ON *.* TO ROLE 'd20_0015_owner';", + "CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p)", ]; for case in cases { diff --git a/src/query/ast/tests/it/testdata/statement-error.txt b/src/query/ast/tests/it/testdata/statement-error.txt index e96694461f17..2b2660b9d0d7 100644 --- a/src/query/ast/tests/it/testdata/statement-error.txt +++ b/src/query/ast/tests/it/testdata/statement-error.txt @@ -823,3 +823,18 @@ error: | while parsing GRANT OWNERSHIP ON TO ROLE +---------- Input ---------- +CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p) +---------- Output --------- +error: + --> SQL:1:65 + | +1 | CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p) + | ------ -- ---- ^ unexpected end of line, expecting `)`, `OVER`, `(`, `IS`, `NOT`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `REGEXP`, `RLIKE`, `SOUNDS`, , , , , , `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, , , , , , `CAST`, `TRY_CAST`, `DATE_ADD`, `DATE_SUB`, `DATE_TRUNC`, `DATE`, `TIMESTAMP`, or 29 more ... + | | | | | + | | | | while parsing `( [, ...])` + | | | while parsing expression + | | while parsing AS (, ...) -> + | while parsing `CREATE [OR REPLACE] FUNCTION [IF NOT EXISTS] {AS (, ...) -> | (, ...) RETURNS LANGUAGE HANDLER= ADDRESS=} [DESC = ]` + + diff --git a/src/query/ast/tests/it/testdata/statement.txt b/src/query/ast/tests/it/testdata/statement.txt index debf50412ae8..98accc84f0e3 100644 --- a/src/query/ast/tests/it/testdata/statement.txt +++ b/src/query/ast/tests/it/testdata/statement.txt @@ -15109,3 +15109,251 @@ Grant( ) +---------- Input ---------- +CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p)); +---------- Output --------- +CREATE FUNCTION IF NOT EXISTS isnotempty AS (p) -> (NOT is_null(p)) +---------- AST ------------ +CreateUDF( + CreateUDFStmt { + create_option: CreateIfNotExists( + true, + ), + udf_name: Identifier { + name: "isnotempty", + quote: None, + span: Some( + 30..40, + ), + }, + description: None, + definition: LambdaUDF { + parameters: [ + Identifier { + name: "p", + quote: None, + span: Some( + 44..45, + ), + }, + ], + definition: UnaryOp { + span: Some( + 50..53, + ), + op: Not, + expr: FunctionCall { + span: Some( + 54..64, + ), + distinct: false, + name: Identifier { + name: "is_null", + quote: None, + span: Some( + 54..61, + ), + }, + args: [ + ColumnRef { + span: Some( + 62..63, + ), + database: None, + table: None, + column: Name( + Identifier { + name: "p", + quote: None, + span: Some( + 62..63, + ), + }, + ), + }, + ], + params: [], + window: None, + lambda: None, + }, + }, + }, + }, +) + + +---------- Input ---------- +CREATE OR REPLACE FUNCTION isnotempty_test_replace AS(p) -> not(is_null(p)) DESC = 'This is a description'; +---------- Output --------- +CREATE OR REPLACE FUNCTION isnotempty_test_replace AS (p) -> (NOT is_null(p)) DESC = 'This is a description' +---------- AST ------------ +CreateUDF( + CreateUDFStmt { + create_option: CreateOrReplace, + udf_name: Identifier { + name: "isnotempty_test_replace", + quote: None, + span: Some( + 27..50, + ), + }, + description: Some( + "This is a description", + ), + definition: LambdaUDF { + parameters: [ + Identifier { + name: "p", + quote: None, + span: Some( + 54..55, + ), + }, + ], + definition: UnaryOp { + span: Some( + 60..63, + ), + op: Not, + expr: FunctionCall { + span: Some( + 64..74, + ), + distinct: false, + name: Identifier { + name: "is_null", + quote: None, + span: Some( + 64..71, + ), + }, + args: [ + ColumnRef { + span: Some( + 72..73, + ), + database: None, + table: None, + column: Name( + Identifier { + name: "p", + quote: None, + span: Some( + 72..73, + ), + }, + ), + }, + ], + params: [], + window: None, + lambda: None, + }, + }, + }, + }, +) + + +---------- Input ---------- +CREATE FUNCTION binary_reverse (BINARY) RETURNS BINARY LANGUAGE python HANDLER = 'binary_reverse' ADDRESS = 'http://0.0.0.0:8815'; +---------- Output --------- +CREATE FUNCTION binary_reverse (BINARY NULL) RETURNS BINARY NULL LANGUAGE python HANDLER = binary_reverse ADDRESS = http://0.0.0.0:8815 +---------- AST ------------ +CreateUDF( + CreateUDFStmt { + create_option: CreateIfNotExists( + false, + ), + udf_name: Identifier { + name: "binary_reverse", + quote: None, + span: Some( + 16..30, + ), + }, + description: None, + definition: UDFServer { + arg_types: [ + Nullable( + Binary, + ), + ], + return_type: Nullable( + Binary, + ), + address: "http://0.0.0.0:8815", + handler: "binary_reverse", + language: "python", + }, + }, +) + + +---------- Input ---------- +CREATE OR REPLACE FUNCTION binary_reverse (BINARY) RETURNS BINARY LANGUAGE python HANDLER = 'binary_reverse' ADDRESS = 'http://0.0.0.0:8815'; +---------- Output --------- +CREATE OR REPLACE FUNCTION binary_reverse (BINARY NULL) RETURNS BINARY NULL LANGUAGE python HANDLER = binary_reverse ADDRESS = http://0.0.0.0:8815 +---------- AST ------------ +CreateUDF( + CreateUDFStmt { + create_option: CreateOrReplace, + udf_name: Identifier { + name: "binary_reverse", + quote: None, + span: Some( + 27..41, + ), + }, + description: None, + definition: UDFServer { + arg_types: [ + Nullable( + Binary, + ), + ], + return_type: Nullable( + Binary, + ), + address: "http://0.0.0.0:8815", + handler: "binary_reverse", + language: "python", + }, + }, +) + + +---------- Input ---------- +DROP FUNCTION binary_reverse; +---------- Output --------- +DROP FUNCTION binary_reverse +---------- AST ------------ +DropUDF { + if_exists: false, + udf_name: Identifier { + name: "binary_reverse", + quote: None, + span: Some( + 14..28, + ), + }, +} + + +---------- Input ---------- +DROP FUNCTION isnotempty; +---------- Output --------- +DROP FUNCTION isnotempty +---------- AST ------------ +DropUDF { + if_exists: false, + udf_name: Identifier { + name: "isnotempty", + quote: None, + span: Some( + 14..24, + ), + }, +} + + diff --git a/tests/sqllogictests/suites/base/05_ddl/05_0010_ddl_create_udf.test b/tests/sqllogictests/suites/base/05_ddl/05_0010_ddl_create_udf.test index 5591f29f1483..53b98a9ccfdc 100644 --- a/tests/sqllogictests/suites/base/05_ddl/05_0010_ddl_create_udf.test +++ b/tests/sqllogictests/suites/base/05_ddl/05_0010_ddl_create_udf.test @@ -57,3 +57,18 @@ DROP FUNCTION isnotempty statement ok DROP FUNCTION isnotempty_with_desc + +statement ok +DROP FUNCTION IF EXISTS 'isnotempty_test_replace'; + +statement error 1105 +CREATE OR REPLACE FUNCTION IF NOT EXISTS isnotempty_test_replace AS(p) -> not(is_null(p)); + +statement ok +CREATE OR REPLACE FUNCTION isnotempty_test_replace AS(p) -> not(is_null(p)); + +statement ok +CREATE OR REPLACE FUNCTION isnotempty_test_replace AS(p) -> not(is_null(p)) DESC = 'This is a description'; + +statement ok +DROP FUNCTION IF EXISTS isnotempty_test_replace