浏览代码

HUE-4343 [editor] Improved autocompletion in select list

Johan Ahlen 9 年之前
父节点
当前提交
59828fa

+ 21 - 6
desktop/core/src/desktop/static/desktop/js/autocomplete/sql.jison

@@ -2346,7 +2346,14 @@ Identifier_EDIT
  ;
 
 SelectSubList
- : ValueExpression OptionalCorrelationName  -> $2 // <derived column>
+ : ValueExpression OptionalCorrelationName
+   {
+     if ($2 && $2.suggestKeywords) {
+       $$ = { suggestKeywords: getValueExpressionKeywords($1, $2.suggestKeywords || []) }
+     } else {
+       $$ = $2;
+     }
+   }
  | '*'
  ;
 
@@ -2369,6 +2376,14 @@ SelectList_EDIT
      suggestFunctions();
      $$ = { cursorAtStart : true, suggestAggregateFunctions: true };
    }
+ | SelectList 'CURSOR' SelectList
+ | SelectList ',' AnyCursor SelectList
+   {
+     suggestFunctions();
+     suggestColumns();
+     suggestFunctions();
+     $$ = { suggestAggregateFunctions: true, suggestKeywords: ['*'] };
+   }
  | SelectList ',' SelectListPartTwo_EDIT                 -> $3
  | SelectList ',' SelectListPartTwo_EDIT ','             -> $3
  | SelectList ',' SelectListPartTwo_EDIT ',' SelectList  -> $3
@@ -3845,7 +3860,7 @@ var suggestValueExpressionKeywords = function (valueExpression, extras) {
 }
 
 var getValueExpressionKeywords = function (valueExpression, extras) {
-  var type = valueExpression.lastType ? valueExpression.lastType.types[0] : valueExpression.types[0];
+  var types = valueExpression.lastType ? valueExpression.lastType.types : valueExpression.types;
   // We could have valueExpression.columnReference to suggest based on column type
   var keywords = ['<', '<=', '<>', '=', '>', '>=', 'BETWEEN', 'IN', 'IS NOT NULL', 'IS NULL', 'NOT BETWEEN', 'NOT IN'];
   if (isHive()) {
@@ -3854,13 +3869,13 @@ var getValueExpressionKeywords = function (valueExpression, extras) {
   if (valueExpression.suggestKeywords) {
     keywords = keywords.concat(valueExpression.suggestKeywords);
   }
-  if (type === 'BOOLEAN' || type === 'T') {
+  if (parser.yy.sqlFunctions.matchesType(parser.yy.activeDialect, ['BOOLEAN'], types)) {
     keywords = keywords.concat(['AND', 'OR']);
   }
-  if (type === 'NUMBER' || type === 'T') {
+  if (parser.yy.sqlFunctions.matchesType(parser.yy.activeDialect, ['NUMBER'], types)) {
     keywords = keywords.concat(['+', '-', '*', '/', '%']);
   }
-  if (type === 'STRING' || type === 'T') {
+  if (parser.yy.sqlFunctions.matchesType(parser.yy.activeDialect, ['STRING'], types)) {
     keywords = keywords.concat(['LIKE', 'NOT LIKE', 'REGEX', 'RLIKE']);
   }
   if (extras) {
@@ -3910,7 +3925,7 @@ var findCaseType = function (whenThenList) {
     });
   });
   if (Object.keys(types).length === 1) {
-    return { type: Object.keys(types)[0] };
+    return { types: [Object.keys(types)[0]] };
   }
   return { types: [ 'T' ] };
 }

文件差异内容过多而无法显示
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sql.js


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

@@ -62,6 +62,7 @@ define([
       assertAutoComplete({
         beforeCursor: 'SELECT foo, bar ',
         afterCursor: '',
+        containsKeywords: ['AS', '+'],
         expectedResult: {
           lowerCase: false,
           suggestTables:{
@@ -70,8 +71,7 @@ define([
           suggestDatabases:{
             prependFrom:true,
             appendDot:true
-          },
-          suggestKeywords: ['AS']
+          }
         }
       });
     });
@@ -80,6 +80,7 @@ define([
       assertAutoComplete({
         beforeCursor: 'SELECT foo AS a, bar ',
         afterCursor: '',
+        containsKeywords: ['AS', '+'],
         expectedResult: {
           lowerCase: false,
           suggestTables:{
@@ -88,8 +89,7 @@ define([
           suggestDatabases:{
             prependFrom:true,
             appendDot:true
-          },
-          suggestKeywords: ['AS']
+          }
         }
       });
     });
@@ -542,9 +542,9 @@ define([
         assertAutoComplete({
           beforeCursor: 'SELECT a ',
           afterCursor: ' FROM tableA;',
+          containsKeywords: ['AS', '+'],
           expectedResult: {
-            lowerCase: false,
-            suggestKeywords: ['AS']
+            lowerCase: false
           }
         });
       });
@@ -553,9 +553,9 @@ define([
         assertAutoComplete({
           beforeCursor: 'SELECT a ',
           afterCursor: ', FROM tableA;',
+          containsKeywords: ['AS', '+'],
           expectedResult: {
-            lowerCase: false,
-            suggestKeywords: ['AS']
+            lowerCase: false
           }
         });
       });
@@ -564,9 +564,9 @@ define([
         assertAutoComplete({
           beforeCursor: 'SELECT a, b ',
           afterCursor: ' FROM tableA;',
+          containsKeywords: ['AS', '+'],
           expectedResult: {
-            lowerCase: false,
-            suggestKeywords: ['AS']
+            lowerCase: false
           }
         });
       });
@@ -575,9 +575,9 @@ define([
         assertAutoComplete({
           beforeCursor: 'SELECT a ',
           afterCursor: ', b, c AS foo, d FROM tableA;',
+          containsKeywords: ['AS', '+'],
           expectedResult: {
-            lowerCase: false,
-            suggestKeywords: ['AS']
+            lowerCase: false
           }
         });
       });
@@ -616,6 +616,7 @@ define([
         assertAutoComplete({
           beforeCursor: 'SELECT COUNT(*) ',
           afterCursor: '',
+          containsKeywords: ['AS', '+'],
           expectedResult: {
             lowerCase: false,
             suggestTables: {
@@ -624,8 +625,7 @@ define([
             suggestDatabases: {
               prependFrom: true,
               appendDot: true
-            },
-            suggestKeywords: ['AS']
+            }
           }
         });
       });
@@ -3360,6 +3360,58 @@ define([
         });
       });
 
+      it('should suggest columns for "SELECT a, b, \\nc,\\nd, |\\ng,\\nf\\nFROM testTable WHERE a > 1 AND b = \'b\' ORDER BY c;"', function() {
+        assertAutoComplete({
+          beforeCursor: 'SELECT a, b, \nc,\nd, ',
+          afterCursor: '\ng,\nf\nFROM testTable WHERE a > 1 AND b = \'b\' ORDER BY c;',
+          expectedResult: {
+            lowerCase: false,
+            suggestFunctions: {},
+            suggestAggregateFunctions: true,
+            suggestColumns: { table: 'testTable' },
+            suggestKeywords: ['*']
+          }
+        });
+      });
+
+      it('should suggest columns for "SELECT a, b, | c FROM testTable"', function() {
+        assertAutoComplete({
+          beforeCursor: 'SELECT a,b, ',
+          afterCursor: ' c FROM testTable',
+          expectedResult: {
+            lowerCase: false,
+            suggestFunctions: {},
+            suggestAggregateFunctions: true,
+            suggestColumns: { table: 'testTable' },
+            suggestKeywords: ['*']
+          }
+        });
+      });
+
+      it('should suggest columns for "SELECT | a, b, c FROM testTable"', function() {
+        assertAutoComplete({
+          beforeCursor: 'SELECT ',
+          afterCursor: ' a, b, c FROM testTable',
+          expectedResult: {
+            lowerCase: false,
+            suggestFunctions: {},
+            suggestAggregateFunctions: true,
+            suggestColumns: { table: 'testTable' },
+            suggestKeywords: ['*', 'ALL', 'DISTINCT']
+          }
+        });
+      });
+
+      it('should suggest keywords for "SELECT a |, b, c FROM testTable"', function() {
+        assertAutoComplete({
+          beforeCursor: 'SELECT a ',
+          afterCursor: ', b, c FROM testTable',
+          containsKeywords: ['AS', '>', 'AND'],
+          expectedResult: {
+            lowerCase: false
+          }
+        });
+      });
 
       it('should suggest columns for "SELECT * FROM testTable WHERE | = \'bar\' AND "', function() {
         assertAutoComplete({

部分文件因为文件数量过多而无法显示