Эх сурвалжийг харах

HUE-8478 [autocomplete] Fix issue where references to CTE aliases in the select list are incorrectly marked as tables

Johan Ahlen 7 жил өмнө
parent
commit
961f1af

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

@@ -173,7 +173,7 @@
           { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 31, last_column: 31 }, subquery: true },
           { type: 'selectList', missing: false, location: { first_line: 1, last_line: 1, first_column: 40, last_column: 41 } },
           { type: 'asterisk', location: { first_line: 1, last_line: 1, first_column: 40, last_column: 41 }, tables: [{ identifierChain: [{ name: 'boo' }] }] },
-          { type: 'table', location: { first_line: 1, last_line: 1, first_column: 47, last_column: 50 }, identifierChain: [{ name: 'boo' }] },
+          { type: 'alias', target: 'cte', alias: 'boo', location: { first_line: 1, last_line: 1, first_column: 47, last_column: 50 } },
           { type: 'whereClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 } },
           { type: 'limitClause', missing: true, location: { first_line: 1, last_line: 1, first_column: 50, last_column: 50 } }
         ]

+ 15 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -531,6 +531,21 @@ var SqlParseSupport = (function () {
           }
         }
 
+        if (location.type === 'table' && typeof location.identifierChain !== 'undefined' && location.identifierChain.length === 1 && location.identifierChain[0].name) {
+          // Could be a cte reference
+          parser.yy.locations.some(function (otherLocation) {
+            if (otherLocation.type === 'alias' && otherLocation.source === 'cte' && SqlUtils.identifierEquals(otherLocation.alias, location.identifierChain[0].name)) {
+              // TODO: Possibly add the other location if we want to show the link in the future.
+              //       i.e. highlight select definition on hover over alias, also for subquery references.
+              location.type = 'alias';
+              location.target = 'cte';
+              location.alias = location.identifierChain[0].name;
+              delete location.identifierChain;
+              return true;
+            }
+          });
+        }
+
         if (location.type === 'table' && (typeof location.identifierChain === 'undefined' || location.identifierChain.length === 0)) {
           parser.yy.locations.splice(i, 1);
         }