lava-oushudb-dt-sql-parser/test/parser/flinksql/syntax.test.ts

223 lines
7.8 KiB
TypeScript
Raw Normal View History

2020-10-22 19:57:13 +08:00
import SQLParser from '../../../src/parser/flinksql';
2020-12-04 11:14:20 +08:00
import sqlMockData from '../../mock/flinkSql';
2020-10-22 19:57:13 +08:00
describe('FlinkSQL Syntax Tests', () => {
const parser = new SQLParser();
// Create statements
2020-10-22 19:57:13 +08:00
test('Test simple CreateTable Statement', () => {
const sql = `
CREATE TABLE Orders (
2020-10-23 17:52:43 +08:00
user BIGINT
2020-10-22 19:57:13 +08:00
) WITH (
"connector" = "kafka",
"scan.startup.mode" = "earliest-offset"
);
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple CreateDatabase Statement', () => {
const sql = `
CREATE DATABASE IF NOT EXISTS dataApi
WITH (
"owner" = "admin"
);
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple CreateView Statement', () => {
const sql = `
CREATE TEMPORARY VIEW IF NOT EXISTS tempView
AS SELECT product, amount FROM Orders;
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple CreateFunction Statement', () => {
const sql = `
CREATE TEMPORARY FUNCTION IF NOT EXISTS tempFunction AS 'SimpleUdf';
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// Alter statements
test('Test simple AlterTable Statement', () => {
const sql = `ALTER TABLE Orders RENAME TO NewOrders;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple AlterDatabase Statement', () => {
const sql = `ALTER DATABASE tempDB SET ("key1"="value1");`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple AlterFunction Statement', () => {
const sql = `
ALTER TEMPORARY FUNCTION IF EXISTS tempFunction AS 'SimpleUdf';
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// Drop statements
test('Test simple DropTable Statement', () => {
const sql = `DROP TABLE IF EXISTS Orders;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple DropDatabase Statement', () => {
const sql = `DROP DATABASE IF EXISTS Orders RESTRICT;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple DropView Statement', () => {
const sql = `DROP TEMPORARY VIEW IF EXISTS Orders;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test simple DropFunction Statement', () => {
const sql = `DROP TEMPORARY FUNCTION IF EXISTS Orders;`;
const result = parser.validate(sql);
2020-10-22 19:57:13 +08:00
expect(result.length).toBe(0);
});
// insert statements
test('Test one simple Insert Statement', () => {
const sql = `
INSERT INTO students VALUES
('Amy Smith', '123 Park Ave, San Jose', 111111);
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test two simple Insert Statement', () => {
const sql = `
INSERT INTO students PARTITION (student_id = 444444)
SELECT name, address FROM persons WHERE name = "Dora Williams";
`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// query statements
2020-11-11 19:58:14 +08:00
test('Test simple Select Statement', () => {
const sql = `SELECT product, amount FROM Orders;`;
2020-11-11 19:58:14 +08:00
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
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);
});
// describe statements
test('Test simple Describe Statement', () => {
const sql = `DESCRIBE Orders;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// describe statements
test('Test simple Explain Statement', () => {
const sql = `EXPLAIN tempTable FOR SELECT k, SUM(v) FROM oneTable;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// use statements
test('Test simple Use Statement', () => {
const sql = `USE CATALOG orders;`;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
// show statements
test('Test simple Show Statement', () => {
const sql = `SHOW CATALOGS;`;
const result = parser.validate(sql);
2020-11-11 19:58:14 +08:00
expect(result.length).toBe(0);
});
// todo 字段中包含特殊字符 如$符号 内容匹配不准确
// test('Test complex sql Statement one', () => {
// const sql = sqlMockData.sqlStrOne;
// const result = parser.validate(sql);
// expect(result.length).toBe(0);
// });
test('Test complex sql Statement two', () => {
const sql = sqlMockData.sqlStrTwo;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement three', () => {
const sql = sqlMockData.sqlStrThree;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement four', () => {
const sql = sqlMockData.sqlStrFour;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement five', () => {
const sql = sqlMockData.sqlStrFive;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement six', () => {
const sql = sqlMockData.sqlStrSix;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement seven', () => {
const sql = sqlMockData.sqlStrSeven;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
test('Test complex sql Statement eight', () => {
const sql = sqlMockData.sqlStrEight;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
2020-12-04 11:14:20 +08:00
test('Test complex sql Statement nine', () => {
const sql = sqlMockData.allSqlStr;
const result = parser.validate(sql);
expect(result.length).toBe(0);
});
2020-10-22 19:57:13 +08:00
});