فهرست منبع

HUE-5309 [editor] The autocompleter should use table aliases in join suggestions

Johan Ahlen 9 سال پیش
والد
کامیت
35d42f0

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

@@ -2336,7 +2336,7 @@ TablePrimaryOrJoinedTable
       if (!lastTablePrimary.subQueryAlias) {
         $$.suggestJoins = {
           prependJoin: true,
-          tables: [{ identifierChain: lastTablePrimary.identifierChain }]
+          tables: [ lastTablePrimary.alias ? { identifierChain: lastTablePrimary.identifierChain, alias: lastTablePrimary.alias } : { identifierChain: lastTablePrimary.identifierChain }]
         };
       }
    }
@@ -2417,7 +2417,7 @@ Join_EDIT
          suggestJoins({
            prependJoin: false,
            joinType: $1,
-           tables: [{ identifierChain: lastTablePrimary.identifierChain }]
+           tables: [ lastTablePrimary.alias ? { identifierChain: lastTablePrimary.identifierChain, alias: lastTablePrimary.alias } : { identifierChain: lastTablePrimary.identifierChain }]
          })
        }
      }

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

@@ -1486,7 +1486,7 @@ case 1163:
       if (!lastTablePrimary.subQueryAlias) {
         this.$.suggestJoins = {
           prependJoin: true,
-          tables: [{ identifierChain: lastTablePrimary.identifierChain }]
+          tables: [ lastTablePrimary.alias ? { identifierChain: lastTablePrimary.identifierChain, alias: lastTablePrimary.alias } : { identifierChain: lastTablePrimary.identifierChain }]
         };
       }
    
@@ -1540,7 +1540,7 @@ case 1181:
          suggestJoins({
            prependJoin: false,
            joinType: $$[$0-3],
-           tables: [{ identifierChain: lastTablePrimary.identifierChain }]
+           tables: [ lastTablePrimary.alias ? { identifierChain: lastTablePrimary.identifierChain, alias: lastTablePrimary.alias } : { identifierChain: lastTablePrimary.identifierChain }]
          })
        }
      }
@@ -4895,7 +4895,7 @@ var linkTablePrimaries = function () {
     parser.yy.result.suggestJoinConditions.tables = [];
     parser.yy.result.suggestJoinConditions.tablePrimaries.forEach(function (tablePrimary) {
       if (!tablePrimary.subQueryAlias) {
-        parser.yy.result.suggestJoinConditions.tables.push({ identifierChain: tablePrimary.identifierChain.concat() });
+        parser.yy.result.suggestJoinConditions.tables.push(tablePrimary.alias ? { identifierChain: tablePrimary.identifierChain.concat(), alias: tablePrimary.alias } : { identifierChain: tablePrimary.identifierChain.concat() });
       }
     });
     delete parser.yy.result.suggestJoinConditions.tablePrimaries;

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/sql_support.js

@@ -792,7 +792,7 @@ var linkTablePrimaries = function () {
     parser.yy.result.suggestJoinConditions.tables = [];
     parser.yy.result.suggestJoinConditions.tablePrimaries.forEach(function (tablePrimary) {
       if (!tablePrimary.subQueryAlias) {
-        parser.yy.result.suggestJoinConditions.tables.push({ identifierChain: tablePrimary.identifierChain.concat() });
+        parser.yy.result.suggestJoinConditions.tables.push(tablePrimary.alias ? { identifierChain: tablePrimary.identifierChain.concat(), alias: tablePrimary.alias } : { identifierChain: tablePrimary.identifierChain.concat() });
       }
     });
     delete parser.yy.result.suggestJoinConditions.tablePrimaries;

+ 22 - 8
desktop/core/src/desktop/static/desktop/js/sqlAutocompleter2.js

@@ -140,7 +140,8 @@ var SqlAutocompleter2 = (function () {
             value.tables.forEach(function (table) {
               var tableParts = table.split('.');
               if (!existingTables[tableParts[tableParts.length - 1]]) {
-                suggestionString += joinRequired ? (parseResult.lowerCase ? ' join ' : ' JOIN ') + self.convertNavOptQualifiedIdentifier(table, database) : self.convertNavOptQualifiedIdentifier(table, database);
+                var identifier = self.convertNavOptQualifiedIdentifier(table, database, parseResult.suggestJoins.tables, false);
+                suggestionString += joinRequired ? (parseResult.lowerCase ? ' join ' : ' JOIN ') + identifier : identifier;
                 joinRequired = true;
               }
             });
@@ -152,7 +153,7 @@ var SqlAutocompleter2 = (function () {
               if (!first) {
                 suggestionString += parseResult.lowerCase ? ' and ' : ' AND ';
               }
-              suggestionString += self.convertNavOptQualifiedIdentifier(joinColPair.columns[0], database) + ' = ' + self.convertNavOptQualifiedIdentifier(joinColPair.columns[1], database);
+              suggestionString += self.convertNavOptQualifiedIdentifier(joinColPair.columns[0], database, parseResult.suggestJoins.tables, true) + ' = ' + self.convertNavOptQualifiedIdentifier(joinColPair.columns[1], database, parseResult.suggestJoins.tables, true);
               first = false;
             });
             completions.push({
@@ -186,7 +187,7 @@ var SqlAutocompleter2 = (function () {
                 if (!first) {
                   suggestionString += parseResult.lowerCase ? ' and ' : ' AND ';
                 }
-                suggestionString += self.convertNavOptQualifiedIdentifier(joinColPair.columns[0], database) + ' = ' + self.convertNavOptQualifiedIdentifier(joinColPair.columns[1], database);
+                suggestionString += self.convertNavOptQualifiedIdentifier(joinColPair.columns[0], database, parseResult.suggestJoinConditions.tables, true) + ' = ' + self.convertNavOptQualifiedIdentifier(joinColPair.columns[1], database,parseResult.suggestJoinConditions.tables, true);
                 first = false;
               });
               completions.push({
@@ -326,14 +327,27 @@ var SqlAutocompleter2 = (function () {
     });
   };
 
-  SqlAutocompleter2.prototype.convertNavOptQualifiedIdentifier = function (qualifiedIdentifier, defaultDatabase) {
+  SqlAutocompleter2.prototype.convertNavOptQualifiedIdentifier = function (qualifiedIdentifier, defaultDatabase, tables, hasColumn) {
     var self = this;
 
-    if (qualifiedIdentifier.indexOf(defaultDatabase) === 0) {
-      return qualifiedIdentifier.substring(defaultDatabase.length + 1);
+    var aliases = [];
+    var tablesHasDefaultDatabase = false
+    tables.forEach(function (table) {
+      tablesHasDefaultDatabase = tablesHasDefaultDatabase || table.identifierChain[0].name.toLowerCase() === defaultDatabase.toLowerCase();
+      if (table.alias) {
+        aliases.push({ qualifiedName: $.map(table.identifierChain, function (identifier) { return identifier.name }).join('.').toLowerCase(), alias: table.alias });
+      }
+    });
+
+    for (var i = 0; i < aliases.length; i++) {
+      if (qualifiedIdentifier.toLowerCase().indexOf(aliases[i].qualifiedName) === 0) {
+        return aliases[i].alias + qualifiedIdentifier.substring(aliases[i].qualifiedName.length);
+      } else if (qualifiedIdentifier.toLowerCase().indexOf(defaultDatabase.toLowerCase() + '.' + aliases[i].qualifiedName) === 0) {
+        return aliases[i].alias + qualifiedIdentifier.substring((defaultDatabase + '.' + aliases[i].qualifiedName).length);
+      }
     }
-    // TODO: Take care of aliases
-    return qualifiedIdentifier;
+
+    return qualifiedIdentifier.toLowerCase().indexOf(defaultDatabase.toLowerCase()) === 0 && !tablesHasDefaultDatabase ? qualifiedIdentifier.substring(defaultDatabase.length + 1) : qualifiedIdentifier;
   };
 
   SqlAutocompleter2.prototype.mergeColumns = function (columnSuggestions) {

+ 11 - 11
desktop/core/src/desktop/static/desktop/spec/autocomplete/sqlSpecSelect.js

@@ -2505,7 +2505,7 @@
           hasLocations: true,
           expectedResult: {
             lowerCase: false,
-            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'db' }, { name: 'foo' }] }] },
+            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'db' }, { name: 'foo' }], alias: 'f' }] },
             suggestKeywords: ['LATERAL VIEW', 'WHERE', 'GROUP BY', 'HAVING', 'WINDOW', 'ORDER BY', 'CLUSTER BY', 'DISTRIBUTE BY', 'SORT BY', 'LIMIT', 'UNION', 'CROSS JOIN', 'FULL JOIN', 'FULL OUTER JOIN', 'JOIN', 'LEFT JOIN', 'LEFT OUTER JOIN', 'LEFT SEMI JOIN', 'RIGHT JOIN', 'RIGHT OUTER JOIN']
           }
         });
@@ -6193,7 +6193,7 @@
           hasLocations: true,
           expectedResult: {
             lowerCase: false,
-            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] },
+            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
             suggestKeywords: ['FULL', 'FULL OUTER', 'INNER', 'LEFT', 'LEFT OUTER', 'RIGHT', 'RIGHT OUTER']
           }
         });
@@ -6207,7 +6207,7 @@
           hasLocations: true,
           expectedResult: {
             lowerCase: false,
-            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] },
+            suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
             suggestKeywords: ['FULL', 'FULL OUTER', 'INNER', 'LEFT', 'LEFT OUTER', 'RIGHT', 'RIGHT OUTER']
           }
         });
@@ -6222,7 +6222,7 @@
           expectedResult: {
             lowerCase: false,
             suggestKeywords: ['ON', 'FULL', 'FULL OUTER', 'INNER', 'LEFT', 'LEFT OUTER', 'RIGHT', 'RIGHT OUTER'],
-            suggestJoinConditions: { prependOn: true, tables: [{ identifierChain: [{ name: 'table1' }] }, { identifierChain: [{ name: 'table2' }] }] }
+            suggestJoinConditions: { prependOn: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }, { identifierChain: [{ name: 'table2' }], alias: 't2' }] }
           }
         });
       });
@@ -6317,7 +6317,7 @@
             containsKeywords: ['LEFT SEMI JOIN', 'CROSS JOIN'], // Tested in full above
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] }
+              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] }
             }
           });
         });
@@ -6395,7 +6395,7 @@
             hasLocations: true,
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] },
+              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
               suggestKeywords: ['LATERAL VIEW', 'CROSS', 'FULL', 'FULL OUTER', 'LEFT', 'LEFT OUTER', 'LEFT SEMI', 'RIGHT', 'RIGHT OUTER']
             }
           });
@@ -6464,7 +6464,7 @@
             hasLocations: true,
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: false, joinType: 'LEFT OUTER JOIN', tables: [{ identifierChain: [{ name: 'table1' }] }] },
+              suggestJoins: { prependJoin: false, joinType: 'LEFT OUTER JOIN', tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
               suggestTables: {},
               suggestDatabases: { appendDot: true }
             }
@@ -6498,7 +6498,7 @@
             hasLocations: true,
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] }
+              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] }
             }
           });
         });
@@ -6580,7 +6580,7 @@
             containsKeywords: ['FULL', 'FULL OUTER', 'INNER', 'LEFT', 'LEFT OUTER', 'ON', 'RIGHT', 'RIGHT OUTER', 'USING'],
             expectedResult: {
               lowerCase: false,
-              suggestJoinConditions: { prependOn: true, tables: [{ identifierChain: [{ name: 'table1' }] }, { identifierChain: [{ name: 'table2' }] }] }
+              suggestJoinConditions: { prependOn: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }, { identifierChain: [{ name: 'table2' }], alias: 't2' }] }
             }
           });
         });
@@ -6593,7 +6593,7 @@
             hasLocations: true,
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }] }] },
+              suggestJoins: { prependJoin: true, tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
               suggestKeywords: ['FULL', 'FULL OUTER', 'INNER', 'LEFT', 'LEFT ANTI', 'LEFT OUTER', 'LEFT SEMI', 'RIGHT', 'RIGHT ANTI', 'RIGHT OUTER', 'RIGHT SEMI']
             }
           });
@@ -6662,7 +6662,7 @@
             hasLocations: true,
             expectedResult: {
               lowerCase: false,
-              suggestJoins: { prependJoin: false, joinType: 'LEFT OUTER JOIN', tables: [{ identifierChain: [{ name: 'table1' }] }] },
+              suggestJoins: { prependJoin: false, joinType: 'LEFT OUTER JOIN', tables: [{ identifierChain: [{ name: 'table1' }], alias: 't1' }] },
               suggestTables: {},
               suggestDatabases: { appendDot: true },
               suggestKeywords: ['[BROADCAST]', '[SHUFFLE]']