Browse Source

HUE-8301 [editor] Identify column types for variables when a column alias is used

Johan Ahlen 7 years ago
parent
commit
4a286bf7da

+ 18 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSpecLocations.js

@@ -953,6 +953,24 @@
 
 
       describe('variable references', function () {
       describe('variable references', function () {
 
 
+        it('should variable location for "select a as b from tbl where b = ${var_name=10}; |"', function() {
+          assertLocations({
+            dialect: 'impala',
+            beforeCursor: 'select a as b from tbl where b = ${var_name=10}; ',
+            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: 14 } },
+              { type: 'column', location: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 }, identifierChain: [{ name: 'a' }], qualified: false, alias: 'b', tables: [{ identifierChain: [{ name: 'tbl' }] }] },
+              { type: 'alias', source: 'column', alias: 'b', location: { first_line: 1, last_line: 1, first_column: 13, last_column: 14 }, parentLocation: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 } },
+              { type: 'table', location: { first_line: 1, last_line: 1, first_column: 20, last_column: 23 }, identifierChain: [{ name: 'tbl' }] },
+              { type: 'whereClause', missing: false, location: { first_line: 1, last_line: 1, first_column: 24, last_column: 48 } },
+              { type: 'alias', location: { first_line: 1, last_line: 1, first_column: 30, last_column: 31 }, source: 'column', alias: 'b', parentLocation: { first_line: 1, last_line: 1, first_column: 8, last_column: 9 } },
+              { type: 'variable', location: { first_line: 1, last_line: 1, first_column: 34, last_column: 48 }, value: '${var_name=10}', colRef: { identifierChain: [{ name: 'a' }], tables: [{ identifierChain: [{ name: 'tbl' }] }] } },
+              { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 48, last_column: 48 } }
+            ]
+          });
+        });
+
         it('should variable location for "select * from tbl where col = ${var_name=10}; |"', function() {
         it('should variable location for "select * from tbl where col = ${var_name=10}; |"', function() {
           assertLocations({
           assertLocations({
             beforeCursor: 'select * from tbl where col = ${var_name=10}; ',
             beforeCursor: 'select * from tbl where col = ${var_name=10}; ',

+ 22 - 2
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -1504,13 +1504,23 @@ var SqlParseSupport = (function () {
     };
     };
 
 
     parser.addColumnAliasLocation = function (location, alias, parentLocation) {
     parser.addColumnAliasLocation = function (location, alias, parentLocation) {
-      parser.yy.locations.push({
+      var aliasLocation = {
         type: 'alias',
         type: 'alias',
         source: 'column',
         source: 'column',
         alias: alias,
         alias: alias,
         location: adjustLocationForCursor(location),
         location: adjustLocationForCursor(location),
         parentLocation: adjustLocationForCursor(parentLocation)
         parentLocation: adjustLocationForCursor(parentLocation)
-      });
+      };
+      if (parser.yy.locations.length && parser.yy.locations[parser.yy.locations.length - 1].type === 'column') {
+        var closestColumn = parser.yy.locations[parser.yy.locations.length - 1];
+        if (closestColumn.location.first_line === aliasLocation.parentLocation.first_line &&
+          closestColumn.location.last_line === aliasLocation.parentLocation.last_line &&
+          closestColumn.location.first_column === aliasLocation.parentLocation.first_column &&
+          closestColumn.location.last_column === aliasLocation.parentLocation.last_column) {
+          parser.yy.locations[parser.yy.locations.length - 1].alias = alias;
+        }
+      }
+      parser.yy.locations.push(aliasLocation);
     };
     };
 
 
     parser.addTableAliasLocation = function (location, alias, identifierChain) {
     parser.addTableAliasLocation = function (location, alias, identifierChain) {
@@ -1601,6 +1611,16 @@ var SqlParseSupport = (function () {
     parser.addColRefToVariableIfExists = function (left, right) {
     parser.addColRefToVariableIfExists = function (left, right) {
       if (left && left.columnReference && left.columnReference.length && right && right.columnReference && right.columnReference.length && parser.yy.locations.length > 1) {
       if (left && left.columnReference && left.columnReference.length && right && right.columnReference && right.columnReference.length && parser.yy.locations.length > 1) {
         var addColRefToVariableLocation = function (variableValue, colRef) {
         var addColRefToVariableLocation = function (variableValue, colRef) {
+          // See if colref is actually an alias
+          if (colRef.length === 1 && colRef[0].name) {
+            parser.yy.locations.some(function (location) {
+              if (location.type === 'column' && location.alias === colRef[0].name) {
+                colRef = location.identifierChain;
+                return true;
+              }
+            });
+          }
+
           for (var i = parser.yy.locations.length - 1; i > 0; i--) {
           for (var i = parser.yy.locations.length - 1; i > 0; i--) {
             var location = parser.yy.locations[i];
             var location = parser.yy.locations[i];
             if (location.type === 'variable' && location.value === variableValue) {
             if (location.type === 'variable' && location.value === variableValue) {