Kaynağa Gözat

[core] Fix incorrect table alias suggestion in autocomplete

This fixes an issue where parts of a nested select appear in the table alias suggestions. With this fix it will ignore any identifiers that contain a '('.

It's definitely possible to suggest aliases of nested select statements but it's quite complex with the current regex-based parser, so for now I've only added a disabled test for it.
Johan Ahlen 10 yıl önce
ebeveyn
işleme
0de6bfffb2

+ 10 - 8
desktop/core/src/desktop/static/desktop/js/autocomplete.js

@@ -55,17 +55,19 @@ Autocompleter.prototype.getFromReferenceIndex = function (statement) {
     var refs = refsRaw.split(/\s*(?:,|\bJOIN\b)\s*/i);
     refs.sort();
     $.each(refs, function(index, tableRefRaw) {
-      var refMatch = tableRefRaw.match(/ *([^ ]*) ?([^ ]*)? */);
+      if (tableRefRaw.indexOf('(') == -1) {
+        var refMatch = tableRefRaw.match(/ *([^ ]*) ?([^ ]*)? */);
 
-      var refParts = refMatch[1].split('.');
-      if (refMatch[2]) {
-        if (refParts.length == 1) {
-          result.tables[refMatch[2]] = refParts[0];
+        var refParts = refMatch[1].split('.');
+        if (refMatch[2]) {
+          if (refParts.length == 1) {
+            result.tables[refMatch[2]] = refParts[0];
+          } else {
+            result.complex[refMatch[2]] = refParts;
+          }
         } else {
-          result.complex[refMatch[2]] = refParts;
+          result.tables[refMatch[1]] = refMatch[1];
         }
-      } else {
-        result.tables[refMatch[1]] = refMatch[1];
       }
     })
   }

+ 28 - 9
desktop/core/src/desktop/static/desktop/spec/autocompleteSpec.js

@@ -175,6 +175,34 @@ describe("autocomplete.js", function() {
         expectedSuggestions: ["testTable1", "testTable2"]
       });
     });
+
+    it("should suggest aliases", function() {
+      assertAutoComplete({
+        serverResponses: {},
+        beforeCursor: "SELECT ",
+        afterCursor: " FROM testTableA tta, testTableB",
+        expectedSuggestions: ["testTableB.", "tta."]
+      });
+    });
+
+    it("should only suggest table aliases", function() {
+      assertAutoComplete({
+        serverResponses: {},
+        beforeCursor: "SELECT ",
+        afterCursor: " FROM testTableA tta, (SELECT SUM(A*B) total FROM tta.array) ttaSum, testTableB ttb",
+        expectedSuggestions: ["tta.", "ttb."]
+      });
+    });
+
+    // TODO: Fix me...
+    xit("should suggest aliases from nested selects", function() {
+      assertAutoComplete({
+        serverResponses: {},
+        beforeCursor: "SELECT ",
+        afterCursor: " FROM testTableA tta, testTableB ttb, (SELECT SUM(A*B) total FROM tta.array) ttaSum",
+        expectedSuggestions: ["tta.", "ttb.", "ttaSum."]
+      });
+    });
   });
 
   describe("hive-specific stuff", function() {
@@ -714,15 +742,6 @@ describe("autocomplete.js", function() {
       });
     });
 
-    it("should suggest aliases", function() {
-      assertAutoComplete({
-        serverResponses: {},
-        beforeCursor: "SELECT ",
-        afterCursor: " FROM testTableA tta, testTableB",
-        expectedSuggestions: ["testTableB.", "tta."]
-      });
-    });
-
     describe("struct completion", function() {
       it("should suggest fields from columns that are structs", function() {
         assertAutoComplete({