fix split bug

This commit is contained in:
HSunboy 2019-09-29 17:38:52 +08:00
parent 966a8d31f4
commit 3b6c224e0e
5 changed files with 91 additions and 18 deletions

View File

@ -25,6 +25,16 @@ function splitSql(sql) {
sql += ';'; sql += ';';
haveEnd = false; haveEnd = false;
} }
function pushSql(parser, sql) {
if (!haveEnd && parser.index == sql.length - 1) {
parser.sqls.push(parser.index - 1);
parser.queue = '';
}
else {
parser.sqls.push(parser.index);
parser.queue = '';
}
}
// 处理引号 // 处理引号
function quoteToken(parser, sql) { function quoteToken(parser, sql) {
let queue = parser.queue; let queue = parser.queue;
@ -81,14 +91,7 @@ function splitSql(sql) {
function splitToken(parser, sql) { function splitToken(parser, sql) {
let queue = parser.queue; let queue = parser.queue;
if (queue.endsWith(';')) { if (queue.endsWith(';')) {
if (!haveEnd && parser.index == sql.length - 1) { pushSql(parser, sql);
parser.sqls.push(parser.index - 1);
queue = '';
}
else {
parser.sqls.push(parser.index);
queue = '';
}
} }
else { else {
return null; return null;
@ -106,6 +109,9 @@ function splitSql(sql) {
for (let i = 0; i < tokenFuncs.length; i++) { for (let i = 0; i < tokenFuncs.length; i++) {
tokenFuncs[i](parser, sql); tokenFuncs[i](parser, sql);
} }
if (parser.index == sql.length - 1 && parser.queue) {
pushSql(parser, sql);
}
} }
return parser.sqls; return parser.sqls;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "dt-sql-parser", "name": "dt-sql-parser",
"version": "2.0.3", "version": "2.0.4",
"description": "sql,hive,parser ", "description": "sql,hive,parser ",
"keywords": [ "keywords": [
"hive", "hive",
@ -13,7 +13,7 @@
"build:parse": "pegjs -o core/astParser.js peg/nquery.pegjs ", "build:parse": "pegjs -o core/astParser.js peg/nquery.pegjs ",
"build:filter": "pegjs -o core/comment.js peg/comment.pegjs ", "build:filter": "pegjs -o core/comment.js peg/comment.pegjs ",
"build:syntax": "sh ./jison/hue-sql-syntax.sh ", "build:syntax": "sh ./jison/hue-sql-syntax.sh ",
"build": "rm -rf lib && tsc && jest", "build": "npm test && rm -rf lib && tsc",
"test": "jest" "test": "jest"
}, },
"author": "xiaokang", "author": "xiaokang",

View File

@ -31,6 +31,15 @@ function splitSql (sql: string) {
queue: string; queue: string;
sqls: number[]; sqls: number[];
} }
function pushSql (parser: splitParser, sql: string) {
if (!haveEnd && parser.index == sql.length - 1) {
parser.sqls.push(parser.index - 1);
parser.queue = '';
} else {
parser.sqls.push(parser.index);
parser.queue = '';
}
}
// 处理引号 // 处理引号
function quoteToken (parser: splitParser, sql: string): string { function quoteToken (parser: splitParser, sql: string): string {
let queue = parser.queue; let queue = parser.queue;
@ -81,13 +90,7 @@ function splitSql (sql: string) {
function splitToken (parser: splitParser, sql: string): string { function splitToken (parser: splitParser, sql: string): string {
let queue = parser.queue; let queue = parser.queue;
if (queue.endsWith(';')) { if (queue.endsWith(';')) {
if (!haveEnd && parser.index == sql.length - 1) { pushSql(parser, sql)
parser.sqls.push(parser.index - 1);
queue = '';
} else {
parser.sqls.push(parser.index);
queue = '';
}
} else { } else {
return null; return null;
@ -105,6 +108,9 @@ function splitSql (sql: string) {
for (let i = 0; i < tokenFuncs.length; i++) { for (let i = 0; i < tokenFuncs.length; i++) {
tokenFuncs[i](parser, sql); tokenFuncs[i](parser, sql);
} }
if (parser.index == sql.length - 1 && parser.queue) {
pushSql(parser, sql)
}
} }
return parser.sqls; return parser.sqls;
} }

View File

@ -91,6 +91,39 @@ describe('syntax test', () => {
}); });
expect(result.token.start).toBe(42); expect(result.token.start).toBe(42);
expect(result.token.stop).toBe(42); expect(result.token.stop).toBe(42);
}) 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();
});
}) })
}) })

View File

@ -16,5 +16,33 @@ describe('utils', () => {
const result = utils.splitSql(sql); const result = utils.splitSql(sql);
expect(result).toEqual([34, 65]) expect(result).toEqual([34, 65])
}); });
test('error sql', () => {
const sql = `CREATE TABLE MyResult(
a double,
b timestamp,
c timestamp
)WITH(
type ='mysql,
url ='jdbc:mysql://1.1.1.1:3306/hi?charset=utf8',
userName ='name',
password ='123',
tableName ='user'
);`;
const result = utils.splitSql(sql);
expect(result).toEqual([337])
const sql2 = `CREATE TABLE MyResult(
a double,
b timestamp,
c timestamp
)WITH(
type ='mysql,
url ='jdbc:mysql://1.1.1.1:3306/hi?charset=utf8',
userName ='name',
password ='123',
tableName ='user'
)`;
const result2 = utils.splitSql(sql2);
expect(result2).toEqual([336])
});
}) })
}) })