Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[analyser] Fixing coding nits on parsing
  • Loading branch information
romainr committed Feb 4, 2021
1 parent 82d46e2 commit a34cad5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Expand Up @@ -58,20 +58,20 @@ describe('SqlAnalyzer.ts', () => {

describe('checkSelectStar', () => {
it('Should detect a SELECT *', async () => {
const isMissingLimit = await new SqlAnalyzer(connectorA).checkSelectStar(
const isSelectStar = await new SqlAnalyzer(connectorA).checkSelectStar(
'SELECT * FROM employee',
'hive'
);

expect(isMissingLimit).toBeTruthy();
expect(isSelectStar).toBeTruthy();
});
it('Should not warning from a non SELECT *', async () => {
const isMissingLimit = await new SqlAnalyzer(connectorA).checkSelectStar(
const isSelectStar = await new SqlAnalyzer(connectorA).checkSelectStar(
'SELECT name FROM employee',
'hive'
);

expect(isMissingLimit).toBeFalsy();
expect(isSelectStar).toBeFalsy();
});
});
});
26 changes: 18 additions & 8 deletions desktop/core/src/desktop/js/catalog/optimizer/SqlAnalyzer.ts
Expand Up @@ -106,30 +106,40 @@ export default class SqlAnalyzer implements Optimizer {

async checkMissingLimit(statement: string, dialect: string): Promise<boolean> {
const autocompleter = await sqlParserRepository.getAutocompleteParser(dialect);
const parsedStatement = autocompleter.parseSql(statement + ' ', '');
let parsedStatement;
try {
parsedStatement = autocompleter.parseSql(statement + ' ', '');
} catch (err) {
return false;
}

return (
parsedStatement.locations.some(
location => location.type === 'statementType' && location.identifier === 'SELECT'
) &&
parsedStatement.locations.some(location => location.type === 'table') &&
parsedStatement.locations.some(location => {
return location.type === 'limitClause' && location.missing;
})
parsedStatement.locations.some(
location => location.type === 'limitClause' && location.missing
)
);
}

async checkSelectStar(statement: string, dialect: string): Promise<boolean> {
const autocompleter = await sqlParserRepository.getAutocompleteParser(dialect);
const parsedStatement = autocompleter.parseSql(statement + ' ', '');
let parsedStatement;
try {
parsedStatement = autocompleter.parseSql(statement + ' ', '');
} catch (err) {
return false;
}

return (
parsedStatement.locations.some(
location => location.type === 'statementType' && location.identifier === 'SELECT'
) &&
parsedStatement.locations.some(location => {
return location.type === 'selectList' && !location.missing;
}) &&
parsedStatement.locations.some(
location => location.type === 'selectList' && !location.missing
) &&
parsedStatement.locations.some(location => location.type === 'asterisk')
);
}
Expand Down

0 comments on commit a34cad5

Please sign in to comment.