build: optimize cli and add eslint
This commit is contained in:
@ -1,223 +0,0 @@
|
||||
import * as dtSqlParser from '../src';
|
||||
import { SyntaxResult } from '../src/core/sqlSyntaxParser';
|
||||
const parser = dtSqlParser.parser;
|
||||
const filter = dtSqlParser.filter;
|
||||
const flinksqlParser = dtSqlParser.flinksqlParser;
|
||||
|
||||
|
||||
describe('complete test', () => {
|
||||
describe('hive', () => {
|
||||
test('complete result', () => {
|
||||
const sql = 'select id,name from `user` ;';
|
||||
const result = parser.parserSql([sql, ''], dtSqlParser.parser.sqlType.Hive);
|
||||
expect(result.locations).toBeInstanceOf(Array);
|
||||
expect(result.suggestKeywords).toBeInstanceOf(Array);
|
||||
});
|
||||
test('empty result', () => {
|
||||
const sql = 'i';
|
||||
const result = parser.parserSql([sql, ''], dtSqlParser.parser.sqlType.Hive);
|
||||
expect(result.locations).toBeInstanceOf(Array);
|
||||
expect(result.locations).toHaveLength(0);
|
||||
expect(result.suggestKeywords).toBeInstanceOf(Array);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('syntax test', () => {
|
||||
describe('impala', () => {
|
||||
test('no error', () => {
|
||||
const sql = 'select id,name from user1 ';
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
test('insert', () => {
|
||||
const sql = `insert into user1 (id, name) values (1 ,'a')`;
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
test('WITH SERDEPROPERTIES', () => {
|
||||
const sql =`CREATE TABLE ih.h_b_py_detail (
|
||||
contract_no STRING,
|
||||
region_code STRING,
|
||||
credit_code STRING
|
||||
)
|
||||
PARTITIONED BY (
|
||||
cdate STRING
|
||||
)
|
||||
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
|
||||
WITH SERDEPROPERTIES ('field.delim'=',', 'line.delim'='\n', 'serialization.format'=',')
|
||||
STORED AS TEXTFILE
|
||||
LOCATION 'hdfs://kudu1'
|
||||
TBLPROPERTIES ('last_modified_by'='anonymous', 'last_modified_time'='1577082098', 'skip.header.line.count'='1')
|
||||
lifecycle 888`;
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
})
|
||||
test('left function', () => {
|
||||
const sql = `select left(a) from sa;`
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
})
|
||||
test('create as select', () => {
|
||||
const sql = `create table partitions_yes partitioned by (year, month)
|
||||
as select s, year, month from partitions_no;`
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
})
|
||||
test('show grant ', () => {
|
||||
const sql = `show grant role 18_112_a;`
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Impala);
|
||||
expect(result).toBe(false);
|
||||
})
|
||||
});
|
||||
describe('hive', () => {
|
||||
test('no error', () => {
|
||||
const sql = 'select id,name from user1 ';
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
test('insert', () => {
|
||||
const sql = `insert into table user1 values (1, 'a'), (2, 'b'), (3, 'b')`;
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
test('select table should not be null', () => {
|
||||
const sql = 'select id,name from ';
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive) as SyntaxResult;
|
||||
expect(result.loc).toEqual({
|
||||
first_line: 1,
|
||||
last_line: 1,
|
||||
first_column: 20,
|
||||
last_column: 20
|
||||
})
|
||||
});
|
||||
test('sql contains the wrong keyword', () => {
|
||||
const sql = `create table if not 1exists ods_order_header (
|
||||
order_header_id string comment 'order id'
|
||||
,order_date bigint comment 'order date'
|
||||
)comment 'order table'
|
||||
PARTITIONED BY (ds string);`;
|
||||
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive) as SyntaxResult;
|
||||
expect(result.text).toBe('1exists');
|
||||
expect(result.loc).toEqual({
|
||||
first_line: 1,
|
||||
last_line: 1,
|
||||
first_column: 20,
|
||||
last_column: 27
|
||||
})
|
||||
});
|
||||
})
|
||||
describe('flinksql', () => {
|
||||
test('no error', () => {
|
||||
const sql = `select id from use1r.id;`;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
test('empty sql', () => {
|
||||
const sql = ``;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
test('sql comment', () => {
|
||||
const sql = `-- name asf
|
||||
-- type FlinkSQL
|
||||
-- author admin@dtstack.com
|
||||
-- create time 2019-06-12 18:11:05
|
||||
-- desc `;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
test('position', () => {
|
||||
const sql = `selec`;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result.token.start).toBe(0);
|
||||
expect(result.token.stop).toBe(4);
|
||||
});
|
||||
test('test lateral table', () => {
|
||||
const sql = `select
|
||||
id
|
||||
FROM
|
||||
userTable, LATERAL table(json(mess)) as t(ord, name_id);`;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
test('syntax error', () => {
|
||||
const sql = 'select id from us1er.id; \nselect id from us*er.id; \nselect id from *u1ser.id;';
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toMatchObject({
|
||||
line: 2,
|
||||
column: 17,
|
||||
});
|
||||
expect(result.token.start).toBe(43);
|
||||
expect(result.token.stop).toBe(43);
|
||||
const sql2 = `CREATE TABLE MyTable(
|
||||
message.after.id int AS id,
|
||||
message.after.userid varchar AS userid,
|
||||
message.after.username varchar AS username,
|
||||
message.after.prodid varchar AS prodid,
|
||||
message.after.price double AS price,
|
||||
message.after.amount int AS amount,
|
||||
message.after.discount double AS discount,
|
||||
message.after.tm timestamp AS tm,
|
||||
WATERMARK FOR tm AS withOffset(tm,1000)
|
||||
)WITH(
|
||||
'type' ='kafka11,
|
||||
topic ='1'
|
||||
);
|
||||
|
||||
CREATE TABLE MyResult(
|
||||
a double,
|
||||
b timestamp,
|
||||
c timestamp
|
||||
)WITH(
|
||||
type ='mysql',
|
||||
url ='jdbc:mysql://1:3306/yanxi?charset=utf8'
|
||||
);
|
||||
|
||||
insert into MyResult
|
||||
select
|
||||
sum(price * amount * discount) as a,
|
||||
TUMBLE_START( ROWTIME, INTERVAL '30' SECOND) as b
|
||||
from MyTable
|
||||
group by
|
||||
TUMBLE( ROWTIME, INTERVAL '30' SECOND);`;
|
||||
const result2 = flinksqlParser(sql2);
|
||||
expect(result2).not.toBeNull();
|
||||
});
|
||||
test('MATCH_RECOGNIZE', () => {
|
||||
const sql = `SELECT *
|
||||
FROM Ticker
|
||||
MATCH_RECOGNIZE (
|
||||
PARTITION BY symbol
|
||||
ORDER BY rowtime
|
||||
MEASURES
|
||||
START_ROW.rowtime AS start_tstamp,
|
||||
LAST(PRICE_DOWN.rowtime) AS bottom_tstamp,
|
||||
LAST(PRICE_UP.rowtime) AS end_tstamp
|
||||
ONE ROW PER MATCH
|
||||
AFTER MATCH SKIP TO LAST PRICE_UP
|
||||
PATTERN (START_ROW PRICE_DOWN+ PRICE_UP)
|
||||
DEFINE
|
||||
PRICE_DOWN AS
|
||||
(LAST(PRICE_DOWN.price, 1) IS NULL AND PRICE_DOWN.price < START_ROW.price) OR
|
||||
PRICE_DOWN.price < LAST(PRICE_DOWN.price, 1),
|
||||
PRICE_UP AS
|
||||
PRICE_UP.price > LAST(PRICE_DOWN.price, 1)
|
||||
) MR;`;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
})
|
||||
test('test primary key', () => {
|
||||
const sql = `create table aa(
|
||||
name.a[1].a varchar as name ,
|
||||
ts int,
|
||||
primary key (id, id[2])
|
||||
) with (
|
||||
type = 'kafka',
|
||||
aa = '12'
|
||||
);`;
|
||||
const result = flinksqlParser(sql);
|
||||
expect(result).toBeNull();
|
||||
})
|
||||
})
|
||||
})
|
16
test/parsers/mysql/lexer.test.ts
Normal file
16
test/parsers/mysql/lexer.test.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import MySQLParser from '../../../src/core/mysql';
|
||||
|
||||
describe('MySQL Lexer tests', () => {
|
||||
const mysqlParser = new MySQLParser();
|
||||
|
||||
const sql = 'select id,name,sex from user1;';
|
||||
const tokens = mysqlParser.getAllTokens(sql);
|
||||
|
||||
test('token counts', () => {
|
||||
expect(tokens.length).toBe(12);
|
||||
});
|
||||
|
||||
test('token counts', () => {
|
||||
expect(tokens.length).toBe(12);
|
||||
});
|
||||
});
|
24
test/parsers/mysql/parser.test.ts
Normal file
24
test/parsers/mysql/parser.test.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import MySQLParser from '../../../src/core/mysql';
|
||||
|
||||
describe('MySQL Parser tests', () => {
|
||||
const mysql = new MySQLParser();
|
||||
|
||||
test('Select * FROM Statement', () => {
|
||||
const sql = 'SELECT * FROM tb;';
|
||||
const result = mysql.parserTreeToString(sql);
|
||||
expect(result).toEqual(`
|
||||
(statement (sqlStatements (
|
||||
sqlStatement (
|
||||
dmlStatement (
|
||||
selectStatement (
|
||||
querySpecification SELECT (selectElements *) (
|
||||
fromClause FROM (
|
||||
tableSources (tableSource (
|
||||
tableSourceItem (tableName (
|
||||
fullId (uid (
|
||||
simpleId TB
|
||||
)
|
||||
))))))))))) (emptyStatement ;)) <EOF>
|
||||
)`);
|
||||
});
|
||||
});
|
11
test/parsers/mysql/syntax.test.ts
Normal file
11
test/parsers/mysql/syntax.test.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import MySQLParser from '../../../src/core/mysql';
|
||||
|
||||
describe('MySQL Syntax tests', () => {
|
||||
const mysql = new MySQLParser();
|
||||
test('Select Statement', () => {
|
||||
const sql = 'select id,name from user1;';
|
||||
const result = mysql.validate(sql);
|
||||
|
||||
expect(result.length).toBe(0);
|
||||
});
|
||||
});
|
36
test/t.js
36
test/t.js
@ -1,36 +0,0 @@
|
||||
const dtSqlParser = require('../lib/index');
|
||||
const flinkParser = require('../lib/lib/flinkParser').default;
|
||||
const parser = dtSqlParser.parser;
|
||||
|
||||
console.log(flinkParser(`selec`))
|
||||
console.time('t')
|
||||
const sql = `INSERT INTO TABLE STUDENT_SCORES VALUES
|
||||
('1','111','68','69','90','CLASS1','DEPARTMENT1'),
|
||||
('2','112','73','80','96','CLASS1','DEPARTMENT1'),
|
||||
('3','113','90','74','75','CLASS1','DEPARTMENT1'),
|
||||
('4','114','89','94','93','CLASS1','DEPARTMENT2'),
|
||||
('5','115','99','93','89','CLASS1','DEPARTMENT1'),
|
||||
('6','121','96','74','79','CLASS2','DEPARTMENT1'),
|
||||
('7','122','89','86','85','CLASS2','DEPARTMENT1'),
|
||||
('8','123','70','78','61','CLASS2','DEPARTMENT1'),
|
||||
('9','124','76','70','76','CLASS2','DEPARTMENT1'),
|
||||
('10','211','89','93','60','CLASS1','DEPARTMENT2'),
|
||||
('11','212','76','83','75','CLASS1','DEPARTMENT2'),
|
||||
('12','213','71','94','90','CLASS2','DEPARTMENT2'),
|
||||
('13','214','94','94','66','CLASS1','DEPARTMENT2'),
|
||||
('14','215','84','82','73','CLASS1','DEPARTMENT2'),
|
||||
('15','216','85','74','93','CLASS1','DEPARTMENT2'),
|
||||
('16','221','77','99','61','CLASS2','DEPARTMENT2'),
|
||||
('17','222','80','78','96','CLASS2','DEPARTMENT2'),
|
||||
('18','223','79','74','96','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('19','224','75','80','78','CLASS2','DEPARTMENT2'),
|
||||
('20','225','82','85','63','CLASS2','DEPARTMENT2')`;
|
||||
const result = parser.parserSql([sql, ''], 'hive');
|
||||
console.timeEnd('t')
|
@ -4,17 +4,17 @@ describe('utils', () => {
|
||||
test('single', () => {
|
||||
let sql = 'select id,name from user';
|
||||
let result = utils.splitSql(sql);
|
||||
expect(result).toEqual([sql.length - 1])
|
||||
expect(result).toEqual([sql.length - 1]);
|
||||
sql += ';';
|
||||
result = utils.splitSql(sql);
|
||||
expect(result).toEqual([sql.length - 1])
|
||||
expect(result).toEqual([sql.length - 1]);
|
||||
});
|
||||
test('multiple', () => {
|
||||
const sql = `-- a ;
|
||||
select * from a;
|
||||
select user from b`;
|
||||
const result = utils.splitSql(sql);
|
||||
expect(result).toEqual([34, 65])
|
||||
expect(result).toEqual([34, 65]);
|
||||
});
|
||||
test('error sql', () => {
|
||||
const sql = `CREATE TABLE MyResult(
|
||||
@ -29,7 +29,7 @@ describe('utils', () => {
|
||||
tableName ='user'
|
||||
);`;
|
||||
const result = utils.splitSql(sql);
|
||||
expect(result).toEqual([337])
|
||||
expect(result).toEqual([337]);
|
||||
const sql2 = `CREATE TABLE MyResult(
|
||||
a double,
|
||||
b timestamp,
|
||||
@ -42,7 +42,7 @@ describe('utils', () => {
|
||||
tableName ='user'
|
||||
)`;
|
||||
const result2 = utils.splitSql(sql2);
|
||||
expect(result2).toEqual([336])
|
||||
expect(result2).toEqual([336]);
|
||||
});
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user