Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[analyser] Add checkMissingLimit() in SQL statement
  • Loading branch information
romainr committed Feb 4, 2021
1 parent 55afcf4 commit 9e829bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Expand Up @@ -50,6 +50,7 @@ export default class SqlExecutable extends Executable {

getStatement(): string {
let statement = this.getRawStatement();

if (
this.parsedStatement.firstToken &&
this.parsedStatement.firstToken.toLowerCase() === 'select' &&
Expand Down
24 changes: 17 additions & 7 deletions desktop/core/src/desktop/js/catalog/optimizer/SqlAnalyzer.ts
Expand Up @@ -59,15 +59,10 @@ export default class SqlAnalyzer implements Optimizer {
apiPromise.cancel();
});

const autocompleter = await sqlParserRepository.getAutocompleteParser(this.connector.dialect);
const snippet = JSON.parse(options.snippetJson);
const sqlParseResult = autocompleter.parseSql(snippet.statement + ' ', '');
const missingLimit = await this.checkMissingLimit(snippet.statement, this.connector.dialect);

const hasLimit = sqlParseResult.locations.some(
location => location.type === 'limitClause' && !location.missing
);

const hints: RiskHint[] = !hasLimit
const hints: RiskHint[] = missingLimit
? [
{
riskTables: [],
Expand Down Expand Up @@ -98,6 +93,21 @@ 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 + ' ', '');

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;
})
);
}

fetchTopJoins(options: PopularityOptions): CancellablePromise<TopJoins> {
const apiPromise = this.apiStrategy.fetchTopJoins(options);

Expand Down
1 change: 1 addition & 0 deletions desktop/core/src/desktop/js/parse/types.ts
Expand Up @@ -51,6 +51,7 @@ export interface SyntaxError {
}

export interface IdentifierLocation {
identifier: string;
type: string;
alias?: string;
source?: string;
Expand Down

0 comments on commit 9e829bd

Please sign in to comment.