feat: add ErrorStrategy(#230)
* refactor: rename errorHandler to errorListener * feat: add ErrorStrategy to mark context exceptions * test: errorStrategy unit tests
This commit is contained in:
62
test/parser/flinksql/errorStrategy.test.ts
Normal file
62
test/parser/flinksql/errorStrategy.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import FlinkSQL from '../../../src/parser/flinksql';
|
||||
import { FlinkSqlSplitListener } from '../../../src/parser/flinksql';
|
||||
import { FlinkSqlParserListener } from '../../../src/lib/flinksql/FlinkSqlParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE VALUES;';
|
||||
|
||||
describe('FlinkSQL ErrorStrategy test', () => {
|
||||
const flinkSQL = new FlinkSQL();
|
||||
test('begin inValid', () => {
|
||||
const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = flinkSQL.parse(sql, () => {});
|
||||
const splitListener = new FlinkSqlSplitListener();
|
||||
flinkSQL.listen(splitListener as FlinkSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== statementCount - 2) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = flinkSQL.parse(sql, () => {});
|
||||
const splitListener = new FlinkSqlSplitListener();
|
||||
flinkSQL.listen(splitListener as FlinkSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = flinkSQL.parse(sql, () => {});
|
||||
const splitListener = new FlinkSqlSplitListener();
|
||||
flinkSQL.listen(splitListener as FlinkSqlParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
62
test/parser/hive/errorStrategy.test.ts
Normal file
62
test/parser/hive/errorStrategy.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import HiveSQL from '../../../src/parser/hive';
|
||||
import { HiveSqlSplitListener } from '../../../src/parser/hive';
|
||||
import { HiveSqlParserListener } from '../../../src/lib/hive/HiveSqlParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE VALUES;';
|
||||
|
||||
describe('HiveSQL ErrorStrategy test', () => {
|
||||
const hiveSQL = new HiveSQL();
|
||||
test('begin inValid', () => {
|
||||
const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = hiveSQL.parse(sql, () => {});
|
||||
const splitListener = new HiveSqlSplitListener();
|
||||
hiveSQL.listen(splitListener as HiveSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== statementCount - 2) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = hiveSQL.parse(sql, () => {});
|
||||
const splitListener = new HiveSqlSplitListener();
|
||||
hiveSQL.listen(splitListener as HiveSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = hiveSQL.parse(sql, () => {});
|
||||
const splitListener = new HiveSqlSplitListener();
|
||||
hiveSQL.listen(splitListener as HiveSqlParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
62
test/parser/impala/errorStrategy.test.ts
Normal file
62
test/parser/impala/errorStrategy.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import ImpalaSQL from '../../../src/parser/impala';
|
||||
import { ImpalaSqlSplitListener } from '../../../src/parser/impala';
|
||||
import { ImpalaSqlParserListener } from '../../../src/lib/impala/ImpalaSqlParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE VALUES;';
|
||||
|
||||
describe('ImpalaSQL ErrorStrategy test', () => {
|
||||
const impalaSQL = new ImpalaSQL();
|
||||
test('begin inValid', () => {
|
||||
const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = impalaSQL.parse(sql, () => {});
|
||||
const splitListener = new ImpalaSqlSplitListener();
|
||||
impalaSQL.listen(splitListener as ImpalaSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== statementCount - 2) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = impalaSQL.parse(sql, () => {});
|
||||
const splitListener = new ImpalaSqlSplitListener();
|
||||
impalaSQL.listen(splitListener as ImpalaSqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = impalaSQL.parse(sql, () => {});
|
||||
const splitListener = new ImpalaSqlSplitListener();
|
||||
impalaSQL.listen(splitListener as ImpalaSqlParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
62
test/parser/mysql/errorStrategy.test.ts
Normal file
62
test/parser/mysql/errorStrategy.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import MySQL from '../../../src/parser/mysql';
|
||||
import { MysqlSplitListener } from '../../../src/parser/mysql';
|
||||
import { MySqlParserListener } from '../../../src/lib/mysql/MySqlParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE VALUES;';
|
||||
|
||||
describe('MySQL ErrorStrategy test', () => {
|
||||
const mysql = new MySQL();
|
||||
test('begin inValid', () => {
|
||||
const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = mysql.parse(sql, () => {});
|
||||
const splitListener = new MysqlSplitListener();
|
||||
mysql.listen(splitListener as MySqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== statementCount - 2) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = mysql.parse(sql, () => {});
|
||||
const splitListener = new MysqlSplitListener();
|
||||
mysql.listen(splitListener as MySqlParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = mysql.parse(sql, () => {});
|
||||
const splitListener = new MysqlSplitListener();
|
||||
mysql.listen(splitListener as MySqlParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
64
test/parser/pgsql/errorStrategy.test.ts
Normal file
64
test/parser/pgsql/errorStrategy.test.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import PgSQL from '../../../src/parser/pgsql';
|
||||
import { PgSqlSplitListener } from '../../../src/parser/pgsql';
|
||||
import { PostgreSQLParserListener } from '../../../src/lib/pgsql/PostgreSQLParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE';
|
||||
|
||||
describe('PgSQL ErrorStrategy test', () => {
|
||||
const pgSQL = new PgSQL();
|
||||
|
||||
// TODO: handle unexpected case
|
||||
// test('begin inValid', () => {
|
||||
// const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// // parse with empty errorListener
|
||||
// const parseTree = pgSQL.parse(sql, () => {});
|
||||
// const splitListener = new PgSqlSplitListener();
|
||||
// pgSQL.listen(splitListener as PostgreSQLParserListener, parseTree);
|
||||
|
||||
// const statementCount = splitListener.statementsContext.length;
|
||||
// splitListener.statementsContext.map((item, index) => {
|
||||
// if(index !== statementCount-1 && index !== statementCount - 2) {
|
||||
// expect(item.exception).not.toBe(undefined);
|
||||
// } else {
|
||||
// expect(item.exception).toBe(undefined);
|
||||
// }
|
||||
// })
|
||||
// });
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = pgSQL.parse(sql, () => {});
|
||||
const splitListener = new PgSqlSplitListener();
|
||||
pgSQL.listen(splitListener as PostgreSQLParserListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = pgSQL.parse(sql, () => {});
|
||||
const splitListener = new PgSqlSplitListener();
|
||||
pgSQL.listen(splitListener as PostgreSQLParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
65
test/parser/spark/errorStrategy.test.ts
Normal file
65
test/parser/spark/errorStrategy.test.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import SparkSQL from '../../../src/parser/spark';
|
||||
import { SparkSqlSplitListener } from '../../../src/parser/spark';
|
||||
import { SparkSqlParserListener } from '../../../src/lib/spark/SparkSqlParserListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE;';
|
||||
|
||||
describe('SparkSQL ErrorStrategy test', () => {
|
||||
const sparkSQL = new SparkSQL();
|
||||
|
||||
// TODO: handle unexpected case
|
||||
// test('begin inValid', () => {
|
||||
// const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// // parse with empty errorListener
|
||||
// const parseTree = sparkSQL.parse(sql, () => {});
|
||||
// const splitListener = new SparkSqlSplitListener();
|
||||
// sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
|
||||
|
||||
// const statementCount = splitListener.statementsContext.length;
|
||||
// splitListener.statementsContext.map((item, index) => {
|
||||
// if(index !== statementCount-1 && index !== statementCount - 2) {
|
||||
// expect(item.exception).not.toBe(undefined);
|
||||
// } else {
|
||||
// expect(item.exception).toBe(undefined);
|
||||
// }
|
||||
// })
|
||||
// });
|
||||
|
||||
// TODO: handle unexpected case
|
||||
// test('middle inValid', () => {
|
||||
// const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// // parse with empty errorListener
|
||||
// const parseTree = sparkSQL.parse(sql, () => {});
|
||||
// const splitListener = new SparkSqlSplitListener();
|
||||
// sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
|
||||
|
||||
// const statementCount = splitListener.statementsContext.length;
|
||||
// splitListener.statementsContext.map((item, index) => {
|
||||
// if(index !== statementCount-1 && index !== 0) {
|
||||
// expect(item.exception).not.toBe(undefined);
|
||||
// } else {
|
||||
// expect(item.exception).toBe(undefined);
|
||||
// }
|
||||
// })
|
||||
// });
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = sparkSQL.parse(sql, () => {});
|
||||
const splitListener = new SparkSqlSplitListener();
|
||||
sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
62
test/parser/trinosql/errorStrategy.test.ts
Normal file
62
test/parser/trinosql/errorStrategy.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import TrinoSQL from '../../../src/parser/trinosql';
|
||||
import { TrinoSqlSplitListener } from '../../../src/parser/trinosql';
|
||||
import { TrinoSqlListener } from '../../../src/lib/trinosql/TrinoSqlListener';
|
||||
|
||||
const validSQL1 = `INSERT INTO country_page_view
|
||||
VALUES ('Chinese', 'mumiao', 18),
|
||||
('Amercian', 'georage', 22);`;
|
||||
const validSQL2 = 'SELECT * FROM tb;';
|
||||
const inValidSQL = 'CREATE TABLE VALUES;';
|
||||
|
||||
describe('TrinoSQL ErrorStrategy test', () => {
|
||||
const trinoSQL = new TrinoSQL();
|
||||
test('begin inValid', () => {
|
||||
const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = trinoSQL.parse(sql, () => {});
|
||||
const splitListener = new TrinoSqlSplitListener();
|
||||
trinoSQL.listen(splitListener as TrinoSqlListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== statementCount - 2) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('middle inValid', () => {
|
||||
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = trinoSQL.parse(sql, () => {});
|
||||
const splitListener = new TrinoSqlSplitListener();
|
||||
trinoSQL.listen(splitListener as TrinoSqlListener, parseTree);
|
||||
|
||||
const statementCount = splitListener.statementsContext.length;
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== statementCount - 1 && index !== 0) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('end inValid', () => {
|
||||
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
||||
// parse with empty errorListener
|
||||
const parseTree = trinoSQL.parse(sql, () => {});
|
||||
const splitListener = new TrinoSqlSplitListener();
|
||||
trinoSQL.listen(splitListener as TrinoSqlListener, parseTree);
|
||||
|
||||
splitListener.statementsContext.map((item, index) => {
|
||||
if (index !== 0 && index !== 1) {
|
||||
expect(item.exception).not.toBe(undefined);
|
||||
} else {
|
||||
expect(item.exception).toBe(undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user