Przeglądaj źródła

HUE-5337 [editor] Autocompletion of top columns should take tables and databases into account

Johan Ahlen 9 lat temu
rodzic
commit
16bdac1

+ 6 - 0
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -87,6 +87,12 @@ var ApiHelper = (function () {
     });
   }
 
+  ApiHelper.prototype.isDatabase = function (name, sourceType) {
+    var self = this;
+    return typeof self.lastKnownDatabases[sourceType] !== 'undefined'
+        && self.lastKnownDatabases[sourceType].filter(function (knownDb) { return knownDb.toLowerCase() === name.toLowerCase() }).length === 1;
+  };
+
   ApiHelper.prototype.hasExpired = function (timestamp) {
     return (new Date()).getTime() - timestamp > TIME_TO_LIVE_IN_MILLIS;
   };

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

@@ -910,14 +910,14 @@ BasicIdentifierChain_EDIT
      suggestColumns({
        identifierChain: $1
      });
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }] };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }] };
    }
  | BasicIdentifierChain AnyDot PartialBacktickedOrPartialCursor AnyDot BasicIdentifierChain
    {
      suggestColumns({
        identifierChain: $1
      });
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }] };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }] };
    }
  ;
 
@@ -1237,9 +1237,9 @@ SelectStatement_EDIT
      if ($3.cursorAtStart) {
        var keywords = [];
        if ($2) {
-         keywords = [{ value: '*', weight: 1000 }];
+         keywords = [{ value: '*', weight: 1099 }];
        } else {
-         keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+         keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        }
        if (isImpala()) {
          keywords.push('STRAIGHT_JOIN');
@@ -1269,13 +1269,13 @@ SelectStatement_EDIT
    {
      var keywords = [];
      if ($2) {
-       keywords = [{ value: '*', weight: 1000 }];
+       keywords = [{ value: '*', weight: 1099 }];
        if ($2 === 'ALL') {
          suggestAggregateFunctions();
          suggestAnalyticFunctions();
        }
      } else {
-       keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+       keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        suggestAggregateFunctions();
        suggestAnalyticFunctions();
      }
@@ -1300,13 +1300,13 @@ SelectStatement_EDIT
    {
      var keywords = [];
      if ($2) {
-       keywords = [{ value: '*', weight: 1000 }];
+       keywords = [{ value: '*', weight: 1099 }];
        if ($2 === 'ALL') {
          suggestAggregateFunctions();
          suggestAnalyticFunctions();
        }
      } else {
-       keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+       keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        suggestAggregateFunctions();
        suggestAnalyticFunctions();
      }
@@ -2260,21 +2260,21 @@ SelectList_EDIT
    }
  | SelectList ',' AnyCursor
    {
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestTables: true, suggestDatabases: true, suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestTables: true, suggestDatabases: true, suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true };
    }
  | SelectList ',' SelectSpecification_EDIT                 -> $3
  | SelectList ',' AnyCursor SelectList
    {
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
    }
  | SelectList ',' AnyCursor ','
    {
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
    }
  | SelectList ',' SelectSpecification_EDIT ','             -> $3
  | SelectList ',' AnyCursor ',' SelectList
    {
-     $$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
+     $$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
    }
  | SelectList ',' SelectSpecification_EDIT ',' SelectList  -> $3
  ;
@@ -3216,7 +3216,7 @@ CountFunction_EDIT
    {
      valueExpressionSuggest();
      if (!$3) {
-       var keywords = isImpala() ? [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'] : [{ value: '*', weight: 1000 }, 'DISTINCT'];
+       var keywords = isImpala() ? [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'] : [{ value: '*', weight: 1099 }, 'DISTINCT'];
        if (parser.yy.result.suggestKeywords) {
          keywords = parser.yy.result.suggestKeywords.concat(keywords);
        }

+ 11 - 11
desktop/core/src/desktop/static/desktop/js/autocomplete/sql.js

@@ -321,7 +321,7 @@ case 668:
      suggestColumns({
        identifierChain: $$[$0-2]
      });
-     this.$ = { suggestKeywords: [{ value: '*', weight: 1000 }] };
+     this.$ = { suggestKeywords: [{ value: '*', weight: 1099 }] };
    
 break;
 case 669:
@@ -329,7 +329,7 @@ case 669:
      suggestColumns({
        identifierChain: $$[$0-4]
      });
-     this.$ = { suggestKeywords: [{ value: '*', weight: 1000 }] };
+     this.$ = { suggestKeywords: [{ value: '*', weight: 1099 }] };
    
 break;
 case 670: case 1133: case 1453:
@@ -486,9 +486,9 @@ case 764:
      if ($$[$0].cursorAtStart) {
        var keywords = [];
        if ($$[$0-1]) {
-         keywords = [{ value: '*', weight: 1000 }];
+         keywords = [{ value: '*', weight: 1099 }];
        } else {
-         keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+         keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        }
        if (isImpala()) {
          keywords.push('STRAIGHT_JOIN');
@@ -519,13 +519,13 @@ case 765:
 
      var keywords = [];
      if ($$[$0-1]) {
-       keywords = [{ value: '*', weight: 1000 }];
+       keywords = [{ value: '*', weight: 1099 }];
        if ($$[$0-1] === 'ALL') {
          suggestAggregateFunctions();
          suggestAnalyticFunctions();
        }
      } else {
-       keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+       keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        suggestAggregateFunctions();
        suggestAnalyticFunctions();
      }
@@ -551,13 +551,13 @@ case 768:
 
      var keywords = [];
      if ($$[$0-2]) {
-       keywords = [{ value: '*', weight: 1000 }];
+       keywords = [{ value: '*', weight: 1099 }];
        if ($$[$0-2] === 'ALL') {
          suggestAggregateFunctions();
          suggestAnalyticFunctions();
        }
      } else {
-       keywords = [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'];
+       keywords = [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'];
        suggestAggregateFunctions();
        suggestAnalyticFunctions();
      }
@@ -1466,12 +1466,12 @@ case 1136: case 1137: case 1521:
 break;
 case 1141:
 
-     this.$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestTables: true, suggestDatabases: true, suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true };
+     this.$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestTables: true, suggestDatabases: true, suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true };
    
 break;
 case 1143: case 1144: case 1146:
 
-     this.$ = { suggestKeywords: [{ value: '*', weight: 1000 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
+     this.$ = { suggestKeywords: [{ value: '*', weight: 1099 }], suggestFunctions: true, suggestColumns: true, suggestAggregateFunctions: true,  };
    
 break;
 case 1148:
@@ -2017,7 +2017,7 @@ case 1385:
 
      valueExpressionSuggest();
      if (!$$[$0-2]) {
-       var keywords = isImpala() ? [{ value: '*', weight: 1000 }, 'ALL', 'DISTINCT'] : [{ value: '*', weight: 1000 }, 'DISTINCT'];
+       var keywords = isImpala() ? [{ value: '*', weight: 1099 }, 'ALL', 'DISTINCT'] : [{ value: '*', weight: 1099 }, 'DISTINCT'];
        if (parser.yy.result.suggestKeywords) {
          keywords = parser.yy.result.suggestKeywords.concat(keywords);
        }

+ 24 - 11
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter2.js

@@ -274,7 +274,7 @@ var SqlAutocompleter2 = (function () {
           silenceErrors: true,
           tables: parseResult.suggestColumns.tables,
           successCallback: function (data) {
-            var topColumns = {};
+            var topColumns = [];
             var values = [];
             switch (parseResult.suggestColumns.source) {
               case 'select':
@@ -290,26 +290,39 @@ var SqlAutocompleter2 = (function () {
                 values = [];
             }
             values.forEach(function (col) {
-              // TODO: Take care of databases and multiple tables properly
-              topColumns[col.columnName.split('.').pop()] = col;
+              col.path = col.tableName.split('.').concat(col.columnName.split('.').slice(1)).join('.');
             });
 
-            topColumnsDeferral.resolve(topColumns);
+            topColumnsDeferral.resolve(values);
           },
-          errorCallback: topColumnsDeferral.resolve
+          errorCallback: function () {
+            topColumnsDeferral.resolve([]);
+          }
         });
       } else {
-        topColumnsDeferral.resolve({});
+        topColumnsDeferral.resolve([]);
       }
 
       var adjustNavWeights = function (suggestions, topColumns) {
+        if (topColumns.length === 0) {
+          return;
+        }
         suggestions.forEach(function (suggestion) {
-          if (typeof topColumns[suggestion.value] !== 'undefined') {
-            suggestion.weight += Math.min(topColumns[suggestion.value].columnCount, 99);
-            suggestion.meta = suggestion.meta + ' *';
-            suggestion.docHTML = self.createTopHtml(topColumns[suggestion.value])
+          var path = '';
+          if (!self.snippet.getApiHelper().isDatabase(suggestion.table.identifierChain[0].name, self.snippet.type())) {
+            path = database + '.';
           }
-        })
+          path += $.map(suggestion.table.identifierChain, function (identifier) { return identifier.name }).join('.') + '.' + suggestion.value.replace(/[\[\]]/g, '');
+          for (var i = 0; i < topColumns.length; i++) {
+            // TODO: Switch to map once nav opt API is stable
+            if (path.toLowerCase().indexOf(topColumns[i].path.toLowerCase()) !== -1) {
+              suggestion.weight += Math.min(topColumns[i].columnCount, 99);
+              suggestion.meta = suggestion.meta + ' *';
+              suggestion.docHTML = self.createTopHtml(topColumns[i]);
+              break;
+            }
+          }
+        });
       };
 
       var suggestColumnsDeferral = $.Deferred();