From d750fcd574f28619f686fb6166063565f140f9ea Mon Sep 17 00:00:00 2001 From: Hayden Date: Wed, 7 Jun 2023 09:34:24 +0800 Subject: [PATCH] test: split flink sql syntax.test.ts (#116) --- test/parser/flinksql/syntax.test.ts | 233 ------------------ .../flinksql/syntax/choreStatement.test.ts | 17 ++ .../flinksql/syntax/commentStatement.test.ts | 16 ++ .../flinksql/syntax/describeStatement.test.ts | 16 ++ .../syntax/dtAddFileStatement.test.ts | 16 ++ .../flinksql/syntax/explainStatement.test.ts | 16 ++ .../flinksql/syntax/fixtures/analyze.sql | 0 .../parser/flinksql/syntax/fixtures/chore.sql | 27 ++ .../flinksql/syntax/fixtures/comment.sql | 7 + .../flinksql/syntax/fixtures/describe.sql | 7 + .../flinksql/syntax/fixtures/dtAddFile.sql | 17 ++ .../flinksql/syntax/fixtures/explain.sql | 15 ++ .../flinksql/syntax/fixtures/select.sql | 8 +- test/parser/flinksql/syntax/fixtures/show.sql | 29 +++ test/parser/flinksql/syntax/fixtures/use.sql | 5 + .../flinksql/syntax/showStatement.test.ts | 16 ++ .../flinksql/syntax/useStatement.test.ts | 16 ++ 17 files changed, 227 insertions(+), 234 deletions(-) delete mode 100644 test/parser/flinksql/syntax.test.ts create mode 100644 test/parser/flinksql/syntax/choreStatement.test.ts create mode 100644 test/parser/flinksql/syntax/commentStatement.test.ts create mode 100644 test/parser/flinksql/syntax/describeStatement.test.ts create mode 100644 test/parser/flinksql/syntax/dtAddFileStatement.test.ts create mode 100644 test/parser/flinksql/syntax/explainStatement.test.ts create mode 100644 test/parser/flinksql/syntax/fixtures/analyze.sql create mode 100644 test/parser/flinksql/syntax/fixtures/chore.sql create mode 100644 test/parser/flinksql/syntax/fixtures/comment.sql create mode 100644 test/parser/flinksql/syntax/fixtures/describe.sql create mode 100644 test/parser/flinksql/syntax/fixtures/dtAddFile.sql create mode 100644 test/parser/flinksql/syntax/fixtures/explain.sql create mode 100644 test/parser/flinksql/syntax/fixtures/show.sql create mode 100644 test/parser/flinksql/syntax/fixtures/use.sql create mode 100644 test/parser/flinksql/syntax/showStatement.test.ts create mode 100644 test/parser/flinksql/syntax/useStatement.test.ts diff --git a/test/parser/flinksql/syntax.test.ts b/test/parser/flinksql/syntax.test.ts deleted file mode 100644 index dbd63a3..0000000 --- a/test/parser/flinksql/syntax.test.ts +++ /dev/null @@ -1,233 +0,0 @@ -import FlinkSQL from '../../../src/parser/flinksql'; - -describe('FlinkSQL Syntax Tests', () => { - const parser = new FlinkSQL(); - - // query statements - test('Test With clause', () => { - const sql = ` - WITH orders_with_total AS ( - SELECT order_id, price + tax AS total - FROM Orders - ) - SELECT order_id, SUM(total) - FROM orders_with_total - GROUP BY order_id; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test simple Select Statement', () => { - const sql = `SELECT product, amount FROM Orders;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test simple Error Select Statement', () => { - const sql = `SELECT product, amount FROM;`; - const result = parser.validate(sql); - expect(result.length).toBe(1); - }); - test('Test Select Statement with where clause', () => { - const sql = `SELECT * FROM person WHERE id = 200 OR id = 300;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test Select Statement with group by clause', () => { - const sql = `SELECT id, sum(quantity) FROM dealer GROUP BY id;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test Select Statement with having clause', () => { - const sql = ` - SELECT city, sum(quantity) AS _sum - FROM dealer GROUP BY city HAVING city = 'Fremont'; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test Select Statement with order by clause', () => { - const sql = `SELECT name, age FROM person ORDER BY age;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test Select Statement with limit clause', () => { - const sql = `SELECT name, age FROM person ORDER BY name LIMIT 2;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - test('Test Select Statement with join', () => { - const sql = ` - SELECT id, name, employee.deptno, deptname FROM employee - FULL JOIN department ON employee.deptno = department.deptno; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - // test left outer join - test('Test Select Statement with left outer join', () => { - const sql = ` - SELECT order_id, res - FROM Orders - LEFT OUTER JOIN LATERAL TABLE(table_func(order_id)) t(res) - ON TRUE - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - // test cross join - test('Test Select Statement with cross join', () => { - const sql = ` - SELECT order_id, tag - FROM Orders CROSS JOIN UNNEST(tags) AS t (tag) - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - // test for time temporal join - test('Test Select Statement with time temporal join', () => { - const sql = `SELECT o.order_id, o.total, c.country, c.zip - FROM Orders AS o - JOIN Customers FOR SYSTEM_TIME AS OF o.proc_time AS c - ON o.customer_id = c.id; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - // test for catalog table - test('Test Select Statement with catalog table', () => { - const sql = `SELECT * FROM catalog1.db1.table1;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // describe statements - test('Test simple Describe Statement', () => { - const sql = ` - DESCRIBE Orders; - DESC Orders; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // explain statements - test('Test simple Explain Statement', () => { - const sql = ` - EXPLAIN SELECT * FROM emps; - EXPLAIN PLAN FOR SELECT * FROM emps; - EXPLAIN PLAN FOR insert into emps1 SELECT * FROM emps2; - EXPLAIN CHANGELOG_MODE SELECT * FROM emps; - EXPLAIN ESTIMATED_COST SELECT * FROM emps; - EXPLAIN JSON_EXECUTION_PLAN SELECT * FROM emps; - EXPLAIN CHANGELOG_MODE, JSON_EXECUTION_PLAN, ESTIMATED_COST SELECT * FROM emps; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // explain insert_statement - test('Test Explain Insert Statement', () => { - const sql = `EXPLAIN INSERT INTO EMPS1 SELECT * FROM EMPS2;`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - test('Test simple Explain statement_set', () => { - // 按官网上写的,那应该是 explain execute statement set - begin,但实际执行时,携带 execute 会报错 - // 个人理解,explain 是解析该语法如何执行,而不能在解析的过程中,去执行语句 - const sql = ` - EXPLAIN STATEMENT SET - BEGIN - INSERT INTO t1 SELECT * FROM t2; - INSERT INTO t2 SELECT * FROM t3; - END; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // use statements - test('Test simple Use Statement', () => { - const sql = ` - USE CATALOG cat1; - USE db1; - USE MODULES hive; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // show statements - test('Test simple Show Statement', () => { - const sql = ` - SHOW CATALOGS; - SHOW CURRENT CATALOG; - SHOW DATABASES; - SHOW CURRENT DATABASE; - SHOW TABLES; - SHOW TABLES FROM catalog1.db1 NOT LIKE '%'; - SHOW CREATE TABLE my_table; - SHOW COLUMNS FROM my_table LIKE '%f%'; - SHOW VIEWS; - SHOW CREATE VIEW my_view; - SHOW FUNCTIONS; - SHOW USER FUNCTIONS; - SHOW MODULES; - SHOW FULL MODULES; - SHOW JARS; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - // other statement - test('Test other Statement', () => { - const sql = ` - LOAD MODULE CORE; - LOAD MODULE dummy WITH ('k1' = 'v1', 'k2' = 'v2'); - UNLOAD MODULE CORE; - SET; - SET 'test-key' = 'test-value'; - RESET; - RESET 'test-key'; - ADD JAR '.jar' - REMOVE JAR '.jar' - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - test('DT Add Statement', () => { - const sql = ` - ADD JAR WITH /home/test.jar; - ADD JAR WITH /home/test.jar AS test.jar; - ADD FILE WITH /home/admin/sftp/dttest_cn/DsCenter_1687/krb5.conf AS krb5.conf; - ADD FILE WITH /home/admin/sftp/dttest_cn/DsCenter_1687/krb5.conf RENAME test.conf; - ADD PYTHON_FILES WITH /test.py RENAME test.py; - ADD PYTHON_REQUIREMENTS WITH /requirements.txt RENAME requirements.txt; - ADD PYTHON_JAR WITH /python_jar.jar RENAME py_jar.jar; - ADD PYTHON_PARAMETER /parameter.py; - ADD ENGINE FILE WITH /filePath RENAME theName KEY theKey; - `; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - test('Test invalid Double Line Comment statement', () => { - const sql = `-test comment\n`; - const result = parser.validate(sql); - expect(result.length).toBe(1); - }); - - test('Test valid Double Line Comment statement', () => { - const sql = `----test comment\n`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); - - test('Test valid Hash Sign Line Comment statement', () => { - const sql = `#test comment\n`; - const result = parser.validate(sql); - expect(result.length).toBe(0); - }); -}); diff --git a/test/parser/flinksql/syntax/choreStatement.test.ts b/test/parser/flinksql/syntax/choreStatement.test.ts new file mode 100644 index 0000000..27d8b61 --- /dev/null +++ b/test/parser/flinksql/syntax/choreStatement.test.ts @@ -0,0 +1,17 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +// 综合测试的 sql 不做切割 +const features = { + chores: readSQL(__dirname, 'chore.sql') +}; + +describe('FlinkSQL Chore Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.chores.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +}); diff --git a/test/parser/flinksql/syntax/commentStatement.test.ts b/test/parser/flinksql/syntax/commentStatement.test.ts new file mode 100644 index 0000000..1ec01a1 --- /dev/null +++ b/test/parser/flinksql/syntax/commentStatement.test.ts @@ -0,0 +1,16 @@ +import fs from 'fs'; +import path from 'path'; +import FlinkSQL from "../../../../src/parser/flinksql"; + +// 注释 sql 不做切割 +const features = { + comments: fs.readFileSync(path.join(__dirname, 'fixtures', 'comment.sql'), 'utf-8') +}; + +describe('FlinkSQL Comment Syntax Tests', () => { + const parser = new FlinkSQL(); + + test('Comment SQL Statement', () => { + expect(parser.validate(features.comments).length).toBe(0); + }); +}); diff --git a/test/parser/flinksql/syntax/describeStatement.test.ts b/test/parser/flinksql/syntax/describeStatement.test.ts new file mode 100644 index 0000000..697eb35 --- /dev/null +++ b/test/parser/flinksql/syntax/describeStatement.test.ts @@ -0,0 +1,16 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +const features = { + describes: readSQL(__dirname, 'describe.sql') +}; + +describe('FlinkSQL Describe Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.describes.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +}); diff --git a/test/parser/flinksql/syntax/dtAddFileStatement.test.ts b/test/parser/flinksql/syntax/dtAddFileStatement.test.ts new file mode 100644 index 0000000..6f317c6 --- /dev/null +++ b/test/parser/flinksql/syntax/dtAddFileStatement.test.ts @@ -0,0 +1,16 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +const features = { + dtAddFiles: readSQL(__dirname, 'dtAddFile.sql') +}; + +describe('FlinkSQL DT Add File Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.dtAddFiles.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +}); diff --git a/test/parser/flinksql/syntax/explainStatement.test.ts b/test/parser/flinksql/syntax/explainStatement.test.ts new file mode 100644 index 0000000..569eb9f --- /dev/null +++ b/test/parser/flinksql/syntax/explainStatement.test.ts @@ -0,0 +1,16 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +const features = { + explains: readSQL(__dirname, 'explain.sql') +}; + +describe('FlinkSQL Explain Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.explains.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +}); diff --git a/test/parser/flinksql/syntax/fixtures/analyze.sql b/test/parser/flinksql/syntax/fixtures/analyze.sql new file mode 100644 index 0000000..e69de29 diff --git a/test/parser/flinksql/syntax/fixtures/chore.sql b/test/parser/flinksql/syntax/fixtures/chore.sql new file mode 100644 index 0000000..2526e02 --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/chore.sql @@ -0,0 +1,27 @@ +-- LOAD/UNLOAD +LOAD MODULE CORE; + +LOAD MODULE dummy WITH ('k1' = 'v1', 'k2' = 'v2'); + +UNLOAD MODULE CORE; + +--SET/RESET +SET; + +SET 'test-key' = 'test-value'; + +RESET; + +RESET 'test-key'; + +-- ADD/REMOVE JAR +ADD JAR '.jar' + +REMOVE JAR '.jar' + +-- Complex Arithmetic Expression +INSERT INTO avg_request_size_5m SELECT + window_start, + window_end, + (server_logs_window_5m.a/server_logs_window_5m.b+c)/d*e%f-g AS avg_size +FROM server_logs_window_5m; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/comment.sql b/test/parser/flinksql/syntax/fixtures/comment.sql new file mode 100644 index 0000000..b83394d --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/comment.sql @@ -0,0 +1,7 @@ +----test comment1 + +--test comment2 + +/* + * test comments 3 + */ \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/describe.sql b/test/parser/flinksql/syntax/fixtures/describe.sql new file mode 100644 index 0000000..b59a8a7 --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/describe.sql @@ -0,0 +1,7 @@ +DESCRIBE Orders; + +DESC Orders; + +DESCRIBE catalog1.db1.table1; + +DESC catalog1.db1.table1; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/dtAddFile.sql b/test/parser/flinksql/syntax/fixtures/dtAddFile.sql new file mode 100644 index 0000000..3a7aefa --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/dtAddFile.sql @@ -0,0 +1,17 @@ +ADD JAR WITH /home/test.jar; + +ADD JAR WITH /home/test.jar AS test.jar; + +ADD FILE WITH /home/admin/sftp/dttest_cn/DsCenter_1687/krb5.conf AS krb5.conf; + +ADD FILE WITH /home/admin/sftp/dttest_cn/DsCenter_1687/krb5.conf RENAME test.conf; + +ADD PYTHON_FILES WITH /test.py RENAME test.py; + +ADD PYTHON_REQUIREMENTS WITH /requirements.txt RENAME requirements.txt; + +ADD PYTHON_JAR WITH /python_jar.jar RENAME py_jar.jar; + +ADD PYTHON_PARAMETER /parameter.py; + +ADD ENGINE FILE WITH /filePath RENAME theName KEY theKey; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/explain.sql b/test/parser/flinksql/syntax/fixtures/explain.sql new file mode 100644 index 0000000..50e863b --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/explain.sql @@ -0,0 +1,15 @@ +EXPLAIN SELECT * FROM emps; + +EXPLAIN PLAN FOR SELECT * FROM emps; + +EXPLAIN PLAN FOR insert into emps1 SELECT * FROM emps2; + +EXPLAIN CHANGELOG_MODE SELECT * FROM emps; + +EXPLAIN ESTIMATED_COST SELECT * FROM emps; + +EXPLAIN JSON_EXECUTION_PLAN SELECT * FROM emps; + +EXPLAIN CHANGELOG_MODE, JSON_EXECUTION_PLAN, ESTIMATED_COST SELECT * FROM emps; + +EXPLAIN INSERT INTO EMPS1 SELECT * FROM EMPS2; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/select.sql b/test/parser/flinksql/syntax/fixtures/select.sql index f46dc8b..9705eb8 100644 --- a/test/parser/flinksql/syntax/fixtures/select.sql +++ b/test/parser/flinksql/syntax/fixtures/select.sql @@ -6,8 +6,14 @@ SELECT order_id, price FROM (VALUES (1, 2.0), (2, 3.1)) AS t (order_id, price); SELECT price + tax FROM Orders WHERE id = 10; +SELECT * FROM person WHERE id = 200 OR id = 300; + +SELECT id, sum(quantity) FROM dealer GROUP BY id; + SELECT PRETTY_PRINT(order_id) FROM Orders; SELECT * FROM Orders ORDER BY order_time, order_id; -SELECT * FROM Orders ORDER BY orderTime LIMIT 3; \ No newline at end of file +SELECT * FROM Orders ORDER BY orderTime LIMIT 3; + +SELECT * FROM catalog1.db1.table1; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/show.sql b/test/parser/flinksql/syntax/fixtures/show.sql new file mode 100644 index 0000000..0359f23 --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/show.sql @@ -0,0 +1,29 @@ +SHOW CATALOGS; + +SHOW CURRENT CATALOG; + +SHOW DATABASES; + +SHOW CURRENT DATABASE; + +SHOW TABLES; + +SHOW TABLES FROM catalog1.db1 NOT LIKE '%'; + +SHOW CREATE TABLE my_table; + +SHOW COLUMNS FROM my_table LIKE '%f%'; + +SHOW VIEWS; + +SHOW CREATE VIEW my_view; + +SHOW FUNCTIONS; + +SHOW USER FUNCTIONS; + +SHOW MODULES; + +SHOW FULL MODULES; + +SHOW JARS; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/fixtures/use.sql b/test/parser/flinksql/syntax/fixtures/use.sql new file mode 100644 index 0000000..0f70f55 --- /dev/null +++ b/test/parser/flinksql/syntax/fixtures/use.sql @@ -0,0 +1,5 @@ +USE CATALOG cat1; + +USE db1; + +USE MODULES hive; \ No newline at end of file diff --git a/test/parser/flinksql/syntax/showStatement.test.ts b/test/parser/flinksql/syntax/showStatement.test.ts new file mode 100644 index 0000000..cd6c98b --- /dev/null +++ b/test/parser/flinksql/syntax/showStatement.test.ts @@ -0,0 +1,16 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +const features = { + shows: readSQL(__dirname, 'show.sql') +}; + +describe('FlinkSQL Show Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.shows.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +}); diff --git a/test/parser/flinksql/syntax/useStatement.test.ts b/test/parser/flinksql/syntax/useStatement.test.ts new file mode 100644 index 0000000..20d491d --- /dev/null +++ b/test/parser/flinksql/syntax/useStatement.test.ts @@ -0,0 +1,16 @@ +import FlinkSQL from "../../../../src/parser/flinksql"; +import { readSQL } from "../../../helper"; + +const features = { + uses: readSQL(__dirname, 'use.sql') +}; + +describe('FlinkSQL Use Syntax Tests', () => { + const parser = new FlinkSQL(); + + features.uses.forEach((sql) => { + it(sql, () => { + expect(parser.validate(sql).length).toBe(0); + }); + }); +});