Преглед изворни кода

[analyser] Fixing coding nits on parsing

Romain Rigaux пре 4 година
родитељ
комит
a34cad5ed6

+ 4 - 4
desktop/core/src/desktop/js/catalog/optimizer/SqlAnalyser.test.ts

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

+ 18 - 8
desktop/core/src/desktop/js/catalog/optimizer/SqlAnalyzer.ts

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