Browse Source

HUE-6529 [editor] Ignore case for databases when showing the SQL context popover

Johan Ahlen 8 years ago
parent
commit
01692f1

+ 10 - 4
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -941,10 +941,16 @@ var ApiHelper = (function () {
         url: AUTOCOMPLETE_API_PREFIX,
         successCallback: function (data) {
           var databases = data.databases || [];
-          // Blacklist of system databases
-          self.lastKnownDatabases[options.sourceType] = $.grep(databases, function (database) {
-            return database !== "_impala_builtins";
+          var cleanDatabases = [];
+          databases.forEach(function (database) {
+            // Blacklist of system databases
+            if (database !== '_impala_builtins') {
+              // Ensure lower case
+              cleanDatabases.push(database.toLowerCase());
+            }
           });
+          self.lastKnownDatabases[options.sourceType] = cleanDatabases;
+
           options.successCallback(self.lastKnownDatabases[options.sourceType]);
         },
         errorCallback: function (response) {
@@ -1223,7 +1229,7 @@ var ApiHelper = (function () {
 
   ApiHelper.prototype.containsDatabase = function (sourceType, databaseName) {
     var self = this;
-    return typeof self.lastKnownDatabases[sourceType] !== 'undefined' && self.lastKnownDatabases[sourceType].indexOf(databaseName) > -1;
+    return typeof self.lastKnownDatabases[sourceType] !== 'undefined' && self.lastKnownDatabases[sourceType].indexOf(databaseName.toLowerCase()) > -1;
   };
 
   ApiHelper.prototype.expandComplexIdentifierChain = function (sourceType, database, identifierChain, successCallback, errorCallback) {

+ 24 - 28
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -16,10 +16,6 @@
 
 var SqlParseSupport = (function () {
 
-  var equalIgnoreCase = function (a, b) {
-    return a && b && a.toLowerCase() === b.toLowerCase();
-  };
-
   var initSqlParser = function (parser) {
 
     var SIMPLE_TABLE_REF_SUGGESTIONS = ['suggestJoinConditions', 'suggestAggregateFunctions', 'suggestFilters', 'suggestGroupBys', 'suggestOrderBys'];
@@ -301,7 +297,7 @@ var SqlParseSupport = (function () {
         // In this testArray would be marked a type table so we need to switch it to column.
         if (location.type === 'table' && typeof location.identifierChain !== 'undefined' && location.identifierChain.length > 1 && parser.yy.latestTablePrimaries) {
           var found = parser.yy.latestTablePrimaries.filter(function (primary) {
-            return equalIgnoreCase(primary.alias, location.identifierChain[0].name);
+            return hueUtils.equalIgnoreCase(primary.alias, location.identifierChain[0].name);
           });
           if (found.length > 0) {
             location.type = 'column';
@@ -310,7 +306,7 @@ var SqlParseSupport = (function () {
 
         if (location.type === 'database' && typeof location.identifierChain !== 'undefined' && location.identifierChain.length > 0 && parser.yy.latestTablePrimaries) {
           var foundAlias = parser.yy.latestTablePrimaries.filter(function (primary) {
-            return equalIgnoreCase(primary.alias, location.identifierChain[0].name);
+            return hueUtils.equalIgnoreCase(primary.alias, location.identifierChain[0].name);
           });
           if (foundAlias.length > 0) {
             // Impala complex reference in FROM clause, i.e. FROM testTable t, t.testMap tm
@@ -322,15 +318,15 @@ var SqlParseSupport = (function () {
         if (location.type === 'unknown') {
           if (typeof location.identifierChain !== 'undefined' && location.identifierChain.length > 0 && location.identifierChain.length <= 2 && parser.yy.latestTablePrimaries) {
             var found = parser.yy.latestTablePrimaries.filter(function (primary) {
-              return equalIgnoreCase(primary.alias, location.identifierChain[0].name) || (primary.identifierChain && equalIgnoreCase(primary.identifierChain[0].name, location.identifierChain[0].name));
+              return hueUtils.equalIgnoreCase(primary.alias, location.identifierChain[0].name) || (primary.identifierChain && hueUtils.equalIgnoreCase(primary.identifierChain[0].name, location.identifierChain[0].name));
             });
             if (found.length > 0) {
-              if (found[0].identifierChain.length > 1 && location.identifierChain.length === 1 && equalIgnoreCase(found[0].identifierChain[0].name, location.identifierChain[0].name)) {
+              if (found[0].identifierChain.length > 1 && location.identifierChain.length === 1 && hueUtils.equalIgnoreCase(found[0].identifierChain[0].name, location.identifierChain[0].name)) {
                 location.type = 'database';
-              } else if (found[0].alias && equalIgnoreCase(location.identifierChain[0].name, found[0].alias) && location.identifierChain.length > 1) {
+              } else if (found[0].alias && hueUtils.equalIgnoreCase(location.identifierChain[0].name, found[0].alias) && location.identifierChain.length > 1) {
                 location.type = 'column';
                 parser.expandIdentifierChain(location, true);
-              } else if (!found[0].alias && found[0].identifierChain && equalIgnoreCase(location.identifierChain[0].name, found[0].identifierChain[found[0].identifierChain.length - 1].name) && location.identifierChain.length > 1) {
+              } else if (!found[0].alias && found[0].identifierChain && hueUtils.equalIgnoreCase(location.identifierChain[0].name, found[0].identifierChain[found[0].identifierChain.length - 1].name) && location.identifierChain.length > 1) {
                 location.type = 'column';
                 parser.expandIdentifierChain(location, true);
               } else {
@@ -340,7 +336,7 @@ var SqlParseSupport = (function () {
             } else {
               if (parser.yy.subQueries) {
                 found = parser.yy.subQueries.filter(function (subQuery) {
-                  return equalIgnoreCase(subQuery.alias, location.identifierChain[0].name);
+                  return hueUtils.equalIgnoreCase(subQuery.alias, location.identifierChain[0].name);
                 });
                 if (found.length > 0) {
                   location.type = 'subQuery';
@@ -534,12 +530,12 @@ var SqlParseSupport = (function () {
       }
       var expand = function (identifier, expandedChain) {
         var foundPrimary = tablePrimaries.filter(function (tablePrimary) {
-          return equalIgnoreCase(tablePrimary.alias, identifier);
+          return hueUtils.equalIgnoreCase(tablePrimary.alias, identifier);
         });
 
         if (foundPrimary.length === 1 && foundPrimary[0].identifierChain) {
           var parentPrimary = tablePrimaries.filter(function (tablePrimary) {
-            return equalIgnoreCase(tablePrimary.alias, foundPrimary[0].identifierChain[0].name);
+            return hueUtils.equalIgnoreCase(tablePrimary.alias, foundPrimary[0].identifierChain[0].name);
           });
           if (parentPrimary.length === 1) {
             var keySet = expandedChain[0].keySet;
@@ -583,13 +579,13 @@ var SqlParseSupport = (function () {
           if (!lateralView.udtf.expression.columnReference) {
             return;
           }
-          if (equalIgnoreCase(firstIdentifier.name, lateralView.tableAlias) && identifierChain.length > 1) {
+          if (hueUtils.equalIgnoreCase(firstIdentifier.name, lateralView.tableAlias) && identifierChain.length > 1) {
             identifierChain.shift();
             firstIdentifier = identifierChain[0];
             if (columnSuggestion) {
               delete parser.yy.result.suggestKeywords;
             }
-          } else if (equalIgnoreCase(firstIdentifier.name, lateralView.tableAlias) && identifierChain.length === 1 && typeof parser.yy.result.suggestColumns !== 'undefined') {
+          } else if (hueUtils.equalIgnoreCase(firstIdentifier.name, lateralView.tableAlias) && identifierChain.length === 1 && typeof parser.yy.result.suggestColumns !== 'undefined') {
             if (columnSuggestion) {
               if (typeof parser.yy.result.suggestIdentifiers === 'undefined') {
                 parser.yy.result.suggestIdentifiers = [];
@@ -603,9 +599,9 @@ var SqlParseSupport = (function () {
             return identifierChain;
           }
           if (lateralView.columnAliases.indexOf(firstIdentifier.name) !== -1) {
-            if (lateralView.columnAliases.length === 2 && lateralView.udtf.function.toLowerCase() === 'explode' && equalIgnoreCase(firstIdentifier.name, lateralView.columnAliases[0])) {
+            if (lateralView.columnAliases.length === 2 && lateralView.udtf.function.toLowerCase() === 'explode' && hueUtils.equalIgnoreCase(firstIdentifier.name, lateralView.columnAliases[0])) {
               identifierChain[0] = {name: 'key'};
-            } else if (lateralView.columnAliases.length === 2 && lateralView.udtf.function.toLowerCase() === 'explode' && equalIgnoreCase(firstIdentifier.name, lateralView.columnAliases[1])) {
+            } else if (lateralView.columnAliases.length === 2 && lateralView.udtf.function.toLowerCase() === 'explode' && hueUtils.equalIgnoreCase(firstIdentifier.name, lateralView.columnAliases[1])) {
               identifierChain[0] = {name: 'value'};
             } else {
               identifierChain[0] = {name: 'item'};
@@ -642,13 +638,13 @@ var SqlParseSupport = (function () {
         var tables = [];
         tablePrimaries.forEach(function (tablePrimary) {
           if (identifierChain.length > 1 && !tablePrimary.subQueryAlias) {
-            if (identifierChain.length === 2 && equalIgnoreCase(tablePrimary.alias, identifierChain[0].name)) {
+            if (identifierChain.length === 2 && hueUtils.equalIgnoreCase(tablePrimary.alias, identifierChain[0].name)) {
               addCleanTablePrimary(tables, tablePrimary);
-            } else if (identifierChain.length === 2 && equalIgnoreCase(tablePrimary.identifierChain[0].name, identifierChain[0].name)) {
+            } else if (identifierChain.length === 2 && hueUtils.equalIgnoreCase(tablePrimary.identifierChain[0].name, identifierChain[0].name)) {
               addCleanTablePrimary(tables, tablePrimary);
             } else if (identifierChain.length === 3 && tablePrimary.identifierChain.length > 1 &&
-              equalIgnoreCase(tablePrimary.identifierChain[0].name, identifierChain[0].name) &&
-              equalIgnoreCase(tablePrimary.identifierChain[1].name, identifierChain[1].name)) {
+              hueUtils.equalIgnoreCase(tablePrimary.identifierChain[0].name, identifierChain[0].name) &&
+              hueUtils.equalIgnoreCase(tablePrimary.identifierChain[1].name, identifierChain[1].name)) {
               addCleanTablePrimary(tables, tablePrimary);
             }
           } else {
@@ -695,24 +691,24 @@ var SqlParseSupport = (function () {
       if (identifierChain.length > 0) {
         for (var i = 0; i < tablePrimaries.length; i++) {
           if (tablePrimaries[i].subQueryAlias) {
-            if (equalIgnoreCase(tablePrimaries[i].subQueryAlias, identifierChain[0].name)) {
+            if (hueUtils.equalIgnoreCase(tablePrimaries[i].subQueryAlias, identifierChain[0].name)) {
               foundPrimary = tablePrimaries[i];
             }
-          } else if (equalIgnoreCase(tablePrimaries[i].alias, identifierChain[0].name)) {
+          } else if (hueUtils.equalIgnoreCase(tablePrimaries[i].alias, identifierChain[0].name)) {
             foundPrimary = tablePrimaries[i];
             aliasMatch = true;
             break;
           } else if (tablePrimaries[i].identifierChain.length > 1 && identifierChain.length > 1 &&
-            equalIgnoreCase(tablePrimaries[i].identifierChain[0].name, identifierChain[0].name) &&
-            equalIgnoreCase(tablePrimaries[i].identifierChain[1].name, identifierChain[1].name)) {
+            hueUtils.equalIgnoreCase(tablePrimaries[i].identifierChain[0].name, identifierChain[0].name) &&
+            hueUtils.equalIgnoreCase(tablePrimaries[i].identifierChain[1].name, identifierChain[1].name)) {
             foundPrimary = tablePrimaries[i];
             doubleMatch = true;
             break;
-          } else if (!foundPrimary && equalIgnoreCase(tablePrimaries[i].identifierChain[0].name, identifierChain[0].name) && identifierChain.length > (isColumnLocation ? 1 : 0)) {
+          } else if (!foundPrimary && hueUtils.equalIgnoreCase(tablePrimaries[i].identifierChain[0].name, identifierChain[0].name) && identifierChain.length > (isColumnLocation ? 1 : 0)) {
             foundPrimary = tablePrimaries[i];
             // No break as first two can still match.
           } else if (!foundPrimary && tablePrimaries[i].identifierChain.length > 1
-            && equalIgnoreCase(tablePrimaries[i].identifierChain[tablePrimaries[i].identifierChain.length - 1].name, identifierChain[0].name)) {
+            && hueUtils.equalIgnoreCase(tablePrimaries[i].identifierChain[tablePrimaries[i].identifierChain.length - 1].name, identifierChain[0].name)) {
             // This is for the case SELECT baa. FROM bla.baa, blo.boo;
             foundPrimary = tablePrimaries[i];
             break;
@@ -1085,7 +1081,7 @@ var SqlParseSupport = (function () {
         return;
       }
       var tableRef = parser.yy.latestTablePrimaries.filter(function (tablePrimary) {
-        return equalIgnoreCase(tablePrimary.alias, identifier);
+        return hueUtils.equalIgnoreCase(tablePrimary.alias, identifier);
       });
       if (tableRef.length > 0) {
         parser.suggestColumns({identifierChain: [{name: identifier}]});

+ 18 - 17
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -178,7 +178,7 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
         document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
       }
     }
-  }
+  };
 
   hueUtils.exitFullScreen = function () {
     if (document.fullscreenElement ||
@@ -193,18 +193,18 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
         document.webkitExitFullscreen();
       }
     }
-  }
+  };
 
   hueUtils.changeURL = function (newURL) {
     if (window.location.hash !== '' && newURL.indexOf('#') === -1){
       newURL = newURL + window.location.hash;
     }
     window.history.pushState(null, null, newURL);
-  }
+  };
 
   hueUtils.replaceURL = function (newURL) {
     window.history.replaceState(null, null, newURL);
-  }
+  };
 
   hueUtils.changeURLParameter = function (param, value) {
     var newSearch = '';
@@ -228,11 +228,11 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
     }
 
     hueUtils.changeURL(window.location.pathname + newSearch);
-  }
+  };
 
   hueUtils.removeURLParameter = function (param) {
     hueUtils.changeURLParameter(param, null);
-  }
+  };
 
   /**
    * @param {string} pseudoJson
@@ -251,14 +251,14 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
       });
     }
     return parsedParams;
-  }
+  };
 
   hueUtils.isOverflowing = function (element) {
     if (element instanceof jQuery) {
       element = element[0];
     }
     return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
-  }
+  };
 
   /**
    * @param {string} selector
@@ -277,7 +277,7 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
         hueUtils.waitForRendered(selector, condition, callback);
       }, timeout || 100)
     }
-  }
+  };
 
   /**
    * @param {Function} observable
@@ -297,7 +297,7 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
         }
       });
     }
-  }
+  };
 
   /**
    * @param {Function} variable
@@ -314,11 +314,8 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
         hueUtils.waitForVariable(variable, callback);
       }, timeout || 100)
     }
-  }
+  };
 
-  /**
-   * @constructor
-   */
   hueUtils.scrollbarWidth = function () {
     var $parent, $children, width;
     $parent = $('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');
@@ -326,7 +323,7 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
     width = $children.innerWidth() - $children.height(99).innerWidth();
     $parent.remove();
     return width;
-  }
+  };
 
   hueUtils.getSearchParameter = function (search, name, returnNull) {
     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
@@ -336,7 +333,7 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
       return null;
     }
     return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
-  }
+  };
 
   hueUtils.logError = function (error) {
     if (typeof window.console !== 'undefined' && typeof window.console.error !== 'undefined') {
@@ -361,10 +358,14 @@ if (!('addRule' in CSSStyleSheet.prototype)) {
       cursorminheight: 20,
       horizrailenabled: true,
       autohidemode: "leave"
-    }
+    };
     return $el.niceScroll($.extend(defaults, options || {}));
   };
 
+  hueUtils.equalIgnoreCase = function (a, b) {
+    return a && b && a.toLowerCase() === b.toLowerCase();
+  };
+
 }(hueUtils = window.hueUtils || {}));
 
 if (!Object.keys) {

+ 6 - 10
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter3.js

@@ -56,10 +56,6 @@ var AutocompleteResults = (function () {
 
   var POPULAR_CATEGORIES = [CATEGORIES.POPULAR_AGGREGATE, CATEGORIES.POPULAR_GROUP_BY, CATEGORIES.POPULAR_ORDER_BY, CATEGORIES.POPULAR_FILTER, CATEGORIES.POPULAR_ACTIVE_JOIN, CATEGORIES.POPULAR_JOIN_CONDITION, CATEGORIES.POPULAR_JOIN];
 
-  var equalIgnoreCase = function (a, b) {
-    return a && b && a.toLowerCase() === b.toLowerCase();
-  };
-
   var adjustWeightsBasedOnPopularity = function(suggestions, totalPopularity) {
     suggestions.forEach(function (suggestion) {
       var relativePopularity = Math.round(100 * suggestion.details.popularity.popularity / totalPopularity);
@@ -84,7 +80,7 @@ var AutocompleteResults = (function () {
       return null;
     }
     var foundSubQueries = subQueries.filter(function (knownSubQuery) {
-      return equalIgnoreCase(knownSubQuery.alias, subQueryName)
+      return hueUtils.equalIgnoreCase(knownSubQuery.alias, subQueryName)
     });
     if (foundSubQueries.length > 0) {
       return foundSubQueries[0];
@@ -595,7 +591,7 @@ var AutocompleteResults = (function () {
       if (self.snippet.type() == 'impala' && self.parseResult.suggestTables.identifierChain && self.parseResult.suggestTables.identifierChain.length === 1) {
         databasesDeferred.done(function (databases) {
           var foundDb = databases.filter(function (db) {
-            return equalIgnoreCase(db, self.parseResult.suggestTables.identifierChain[0].name);
+            return hueUtils.equalIgnoreCase(db, self.parseResult.suggestTables.identifierChain[0].name);
           });
           if (foundDb.length > 0) {
             fetchTables();
@@ -678,7 +674,7 @@ var AutocompleteResults = (function () {
     if (typeof table.identifierChain !== 'undefined' && table.identifierChain.length === 1 && typeof table.identifierChain[0].cte !== 'undefined') {
       if (typeof self.parseResult.commonTableExpressions !== 'undefined' && self.parseResult.commonTableExpressions.length > 0) {
         self.parseResult.commonTableExpressions.every(function (cte) {
-          if (equalIgnoreCase(cte.alias, table.identifierChain[0].cte)) {
+          if (hueUtils.equalIgnoreCase(cte.alias, table.identifierChain[0].cte)) {
             cte.columns.forEach(function (column) {
               var type = typeof column.type !== 'undefined' && column.type !== 'COLREF' ? column.type : 'T';
               if (typeof column.alias !== 'undefined') {
@@ -1615,7 +1611,7 @@ var AutocompleteResults = (function () {
       if (navOptColumn.dbName && (navOptColumn.dbName !== self.activeDatabase || navOptColumn.dbName !== tables[i].identifierChain[0].name)) {
         continue;
       }
-      if (navOptColumn.tableName && equalIgnoreCase(navOptColumn.tableName, tables[i].identifierChain[tables[i].identifierChain.length - 1].name) && tables[i].alias) {
+      if (navOptColumn.tableName && hueUtils.equalIgnoreCase(navOptColumn.tableName, tables[i].identifierChain[tables[i].identifierChain.length - 1].name) && tables[i].alias) {
         return tables[i].alias + '.' + navOptColumn.columnName;
       }
     }
@@ -1634,7 +1630,7 @@ var AutocompleteResults = (function () {
     var aliases = [];
     var tablesHasDefaultDatabase = false;
     tables.forEach(function (table) {
-      tablesHasDefaultDatabase = tablesHasDefaultDatabase || equalIgnoreCase(table.identifierChain[0].name.toLowerCase(), self.activeDatabase.toLowerCase());
+      tablesHasDefaultDatabase = tablesHasDefaultDatabase || hueUtils.equalIgnoreCase(table.identifierChain[0].name.toLowerCase(), self.activeDatabase.toLowerCase());
       if (table.alias) {
         aliases.push({ qualifiedName: $.map(table.identifierChain, function (identifier) { return identifier.name }).join('.').toLowerCase(), alias: table.alias });
       }
@@ -1730,7 +1726,7 @@ var AutocompleteResults = (function () {
           successCallback: function (data) {
             try {
               var foundDb = data.filter(function (db) {
-                return equalIgnoreCase(db, identifierChain[0].name.toLowerCase());
+                return hueUtils.equalIgnoreCase(db, identifierChain[0].name.toLowerCase());
               });
               var databaseName = foundDb.length > 0 ? identifierChain.shift().name : self.activeDatabase;
               var tableName = identifierChain.shift().name;

+ 2 - 2
desktop/core/src/desktop/templates/sql_context_popover.mako

@@ -1143,8 +1143,8 @@ from metadata.conf import has_navigator
         if ((self.isColumn || self.isComplex) && self.data.tables && self.data.tables.length > 0) {
           var identifierChain = self.data.identifierChain;
           var foundTable = $.grep(self.data.tables, function (table) {
-            return (table.alias && table.alias === identifierChain[0].name) ||
-                    (table.identifierChain && table.identifierChain[table.identifierChain.length - 1].name === identifierChain[0].name);
+            return hueUtils.equalIgnoreCase(table.alias, identifierChain[0].name) ||
+                    (table.identifierChain && hueUtils.equalIgnoreCase(table.identifierChain[table.identifierChain.length - 1].name, identifierChain[0].name));
           });
           if (foundTable.length === 1 && foundTable.identifierChain) {
             identifierChain.shift();