Selaa lähdekoodia

HUE-7913 [autocomplete] Add variable locations to the autocomplete parser

Johan Ahlen 7 vuotta sitten
vanhempi
commit
c8bcf48

+ 3 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/sql_main.jison

@@ -2492,7 +2492,9 @@ ColumnOrArbitraryFunctionRef
  : BasicIdentifierChain
    {
      var lastLoc = parser.yy.locations[parser.yy.locations.length - 1];
-     lastLoc.type = 'column';
+     if (lastLoc.type !== 'variable') {
+       lastLoc.type = 'column';
+     }
      // used for function references with db prefix
      var firstLoc = parser.yy.locations[parser.yy.locations.length - $1.length];
      $$ = { chain: $1, firstLoc: firstLoc, lastLoc: lastLoc }

+ 21 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSpecLocations.js

@@ -187,7 +187,7 @@
           { type: 'table', location: { first_line: 1, last_line: 1, first_column: 15, last_column: 18 }, identifierChain: [{ name: 'tbl' }] },
           { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 19, last_column: 45 } },
           { type: 'column', location: { first_line: 1, last_line: 1, first_column: 25, last_column: 28 }, identifierChain: [{ name: 'col' }], tables: [{ identifierChain: [{ name: 'tbl' }] }], qualified: false },
-          { type: 'column', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 45 }, identifierChain: [{ name: '${var_name=10}' }], tables: [{ identifierChain: [{ name: 'tbl' }] }], qualified: false },
+          { type: 'variable', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 45 }, value: '${var_name=10}' },
           { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 45, last_column: 45 } }
         ]
       });
@@ -968,6 +968,26 @@
           ]
         });
       });
+
+      describe('variable references', function () {
+        it('should report variable location for "SELECT * FROM testTable WHERE foo = ${some_var}; |"', function () {
+          assertLocations({
+            dialect: 'impala',
+            beforeCursor: 'SELECT * FROM testTable WHERE foo = ${some_var}; ',
+            afterCursor: '',
+            expectedLocations: [
+              { type: 'statement', location: { first_line: 1, last_line: 1, first_column: 1, last_column: 48 } },
+              { type: 'selectList', missing: false, location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 } },
+              { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 }, tables: [{ identifierChain: [{ name: 'testTable' }] }] },
+              { type: 'table', location: { first_line: 1, last_line: 1, first_column: 15, last_column: 24 }, identifierChain: [{ name: 'testTable' }] },
+              { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 25, last_column: 48 } },
+              { type: 'column', location: { first_line: 1, last_line: 1, first_column: 31, last_column: 34 }, identifierChain: [{ name: 'foo' }], qualified: false, tables: [{ identifierChain: [{ name: 'testTable' }] }] },
+              { type: 'variable', location: { first_line: 1, last_line: 1, first_column: 37, last_column: 48 }, value: '${some_var}' },
+              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 48, last_column: 48 } }
+            ]
+          });
+        });
+      });
     })
   });
 })();

+ 2 - 2
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSpecSelect.js

@@ -1115,7 +1115,7 @@
         });
       });
 
-      it('should suggest tables for "SELECT * FROM testTable WHERE ${some_variable} |"', function() {
+      it('should suggest keywords for "SELECT * FROM testTable WHERE ${some_variable} |"', function() {
         assertAutoComplete({
           beforeCursor: 'SELECT * FROM testTable WHERE ${some_variable} ',
           afterCursor: '',
@@ -1130,7 +1130,7 @@
         });
       });
 
-      it('should suggest tables for "SELECT * FROM testTable WHERE ${some_variable} + 1 = |"', function() {
+      it('should suggest columns for "SELECT * FROM testTable WHERE ${some_variable} + 1 = |"', function() {
         assertAutoComplete({
           beforeCursor: 'SELECT * FROM testTable WHERE ${some_variable} + 1 = ',
           afterCursor: '',

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlAutocompleteParser.js


+ 33 - 14
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -1525,12 +1525,21 @@ var SqlParseSupport = (function () {
     };
 
     parser.addColumnLocation = function (location, identifierChain) {
-      parser.yy.locations.push({
-        type: 'column',
-        location: adjustLocationForCursor(location),
-        identifierChain: identifierChain,
-        qualified: identifierChain.length > 1
-      });
+      var isVariable = identifierChain.length && /\$\{[^}]*\}/.test(identifierChain[identifierChain.length - 1].name);
+      if (isVariable) {
+        parser.yy.locations.push({
+          type: 'variable',
+          location: adjustLocationForCursor(location),
+          value: identifierChain[identifierChain.length - 1].name
+        });
+      } else {
+        parser.yy.locations.push({
+          type: 'column',
+          location: adjustLocationForCursor(location),
+          identifierChain: identifierChain,
+          qualified: identifierChain.length > 1
+        });
+      }
     };
 
     parser.addCteAliasLocation = function (location, alias) {
@@ -1543,14 +1552,24 @@ var SqlParseSupport = (function () {
     };
 
     parser.addUnknownLocation = function (location, identifierChain) {
-      var unknownLoc = {
-        type: 'unknown',
-        location: adjustLocationForCursor(location),
-        identifierChain: identifierChain,
-        qualified: identifierChain.length > 1
-      };
-      parser.yy.locations.push(unknownLoc);
-      return unknownLoc;
+      var isVariable = identifierChain.length && /\$\{[^}]*\}/.test(identifierChain[identifierChain.length - 1].name);
+      var loc;
+      if (isVariable) {
+        loc = {
+          type: 'variable',
+          location: adjustLocationForCursor(location),
+          value: identifierChain[identifierChain.length - 1].name
+        };
+      } else {
+        loc = {
+          type: 'unknown',
+          location: adjustLocationForCursor(location),
+          identifierChain: identifierChain,
+          qualified: identifierChain.length > 1
+        };
+      }
+      parser.yy.locations.push(loc);
+      return loc;
     };
 
     parser.suggestDatabases = function (details) {

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlStatementsParser.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlSyntaxParser.js


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä