Browse Source

HUE-4573 [editor] The autocompleter should support carriage return in statements

This also fixes and issue with the if() UDF
Johan Ahlen 9 years ago
parent
commit
8685080659

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/sql.jisonlex

@@ -19,7 +19,7 @@
 %x hdfs doubleQuotedValue singleQuotedValue backtickedValue
 %x hdfs doubleQuotedValue singleQuotedValue backtickedValue
 %%
 %%
 
 
-[ \t\n]                                    { /* skip whitespace */ }
+\s                                         { /* skip whitespace */ }
 '--'.*                                     { /* skip comments */ }
 '--'.*                                     { /* skip comments */ }
 [/][*][^*]*[*]+([^/*][^*]*[*]+)*[/]        { /* skip comments */ }
 [/][*][^*]*[*]+([^/*][^*]*[*]+)*[/]        { /* skip comments */ }
 
 

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

@@ -1908,6 +1908,7 @@ SelectList
 
 
 SelectList_EDIT
 SelectList_EDIT
  : SelectSubList_EDIT
  : SelectSubList_EDIT
+ | SelectSubList_EDIT ',' SelectList
  | 'CURSOR' SelectList
  | 'CURSOR' SelectList
    {
    {
      suggestFunctions();
      suggestFunctions();
@@ -2442,38 +2443,61 @@ UserDefinedFunction_EDIT
  ;
  ;
 
 
 ArbitraryFunction
 ArbitraryFunction
- : 'REGULAR_IDENTIFIER' '(' ')'
+ : RegularIdentifier ArbitraryFunctionRightPart
    {
    {
      addFunctionLocation(@1, $1);
      addFunctionLocation(@1, $1);
-     $$ = { types: findReturnTypes($1) }
+     if ($2.expression) {
+       $$ = { function: $1, expression: $2.expression, types: findReturnTypes($1) }
+     } else {
+       $$ = { function: $1, types: findReturnTypes($1) }
+     }
    }
    }
- | 'REGULAR_IDENTIFIER' '(' ValueExpressionList ')'
+ | 'IF' ArbitraryFunctionRightPart
    {
    {
      addFunctionLocation(@1, $1);
      addFunctionLocation(@1, $1);
-     $$ = { function: $1, expression: $3, types: findReturnTypes($1) }
+     if ($2.expression) {
+       $$ = { function: $1, expression: $2.expression, types: findReturnTypes($1) }
+     } else {
+       $$ = { function: $1, types: findReturnTypes($1) }
+     }
    }
    }
  ;
  ;
 
 
 ArbitraryFunction_EDIT
 ArbitraryFunction_EDIT
- : 'REGULAR_IDENTIFIER' '(' AnyCursor RightParenthesisOrError
+ : RegularIdentifier ArbitraryFunctionRightPart_EDIT
    {
    {
      addFunctionLocation(@1, $1);
      addFunctionLocation(@1, $1);
-     valueExpressionSuggest();
-     applyArgumentTypesToSuggestions($1, 1);
+     if ($2.position) {
+       applyArgumentTypesToSuggestions($1, $2.position);
+     }
      $$ = { types: findReturnTypes($1) };
      $$ = { types: findReturnTypes($1) };
    }
    }
- | 'REGULAR_IDENTIFIER' '(' ValueExpressionList 'CURSOR' RightParenthesisOrError
+ | 'IF' ArbitraryFunctionRightPart_EDIT
    {
    {
      addFunctionLocation(@1, $1);
      addFunctionLocation(@1, $1);
-     suggestValueExpressionKeywords($3);
+     if ($2.position) {
+       applyArgumentTypesToSuggestions($1, $2.position);
+     }
      $$ = { types: findReturnTypes($1) };
      $$ = { types: findReturnTypes($1) };
    }
    }
- | 'REGULAR_IDENTIFIER' '(' ValueExpressionList_EDIT RightParenthesisOrError
+ ;
+
+ArbitraryFunctionRightPart
+ : '(' ')'
+ | '(' ValueExpressionList ')'  -> { expression: $2 }
+ ;
+
+ArbitraryFunctionRightPart_EDIT
+ : '(' AnyCursor RightParenthesisOrError
+   {
+     valueExpressionSuggest();
+     $$ = { position: 1 }
+   }
+ | '(' ValueExpressionList 'CURSOR' RightParenthesisOrError
    {
    {
-     addFunctionLocation(@1, $1);
-     applyArgumentTypesToSuggestions($1, $3.position);
-     $$ = { types: findReturnTypes($1) };
+     suggestValueExpressionKeywords($3);
    }
    }
+ | '(' ValueExpressionList_EDIT RightParenthesisOrError      -> $2
  ;
  ;
 
 
 AggregateFunction
 AggregateFunction

File diff suppressed because it is too large
+ 0 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/sql.js


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

@@ -58,6 +58,23 @@ define([
       });
       });
     });
     });
 
 
+    it('should suggest tables and databases for "SELECT *\\r\\n |"', function() {
+      assertAutoComplete({
+        beforeCursor: 'SELECT *\r\n',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestTables:{
+            prependFrom:true
+          },
+          suggestDatabases:{
+            prependFrom:true,
+            appendDot:true
+          }
+        }
+      });
+    });
+
     it('should suggest keywords for "SELECT foo, bar |"', function() {
     it('should suggest keywords for "SELECT foo, bar |"', function() {
       assertAutoComplete({
       assertAutoComplete({
         beforeCursor: 'SELECT foo, bar ',
         beforeCursor: 'SELECT foo, bar ',
@@ -423,6 +440,39 @@ define([
         });
         });
       });
       });
 
 
+      it('should suggest columns "SELECT IF(baa, boo, bee) AS b, | FROM testTable;"', function () {
+        assertAutoComplete({
+          beforeCursor: 'SELECT IF(baa, boo, bee) AS b, ',
+          afterCursor: ' FROM testTable',
+          hasLocations: true,
+          expectedResult: {
+            lowerCase: false,
+            suggestFunctions: {},
+            suggestAnalyticFunctions: true,
+            suggestAggregateFunctions: true,
+            suggestColumns: { table: 'testTable'},
+            suggestKeywords: ['*']
+          }
+        });
+      });
+
+      it('should suggest columns "SELECT IF(baa > 2, boo, bee) AS b, | FROM testTable;"', function () {
+        assertAutoComplete({
+          beforeCursor: 'SELECT IF(baa > 2, boo, bee) AS b, ',
+          afterCursor: ' FROM testTable',
+          hasLocations: true,
+          expectedResult: {
+            lowerCase: false,
+            suggestFunctions: {},
+            suggestAnalyticFunctions: true,
+            suggestAggregateFunctions: true,
+            suggestColumns: { table: 'testTable'},
+            suggestKeywords: ['*']
+          }
+        });
+      });
+
+
       it('should handle "SELECT tmp.bc, ROUND(tmp.r, 2) AS r FROM ( SELECT tstDb1.b1.cat AS bc, SUM(tstDb1.b1.price * tran.qua) AS r FROM tstDb1.b1 JOIN [SHUFFLE] tran ON ( tran.b_id = tstDb1.b1.id AND YEAR(tran.tran_d) BETWEEN 2008 AND 2010) GROUP BY tstDb1.b1.cat) tmp ORDER BY r DESC LIMIT 60; |"', function () {
       it('should handle "SELECT tmp.bc, ROUND(tmp.r, 2) AS r FROM ( SELECT tstDb1.b1.cat AS bc, SUM(tstDb1.b1.price * tran.qua) AS r FROM tstDb1.b1 JOIN [SHUFFLE] tran ON ( tran.b_id = tstDb1.b1.id AND YEAR(tran.tran_d) BETWEEN 2008 AND 2010) GROUP BY tstDb1.b1.cat) tmp ORDER BY r DESC LIMIT 60; |"', function () {
         assertAutoComplete({
         assertAutoComplete({
           beforeCursor: 'SELECT tmp.bc, ROUND(tmp.r, 2) AS r FROM ( SELECT tstDb1.b1.cat AS bc, SUM(tstDb1.b1.price * tran.qua) AS r FROM tstDb1.b1 JOIN [SHUFFLE] tran ON ( tran.b_id = tstDb1.b1.id AND YEAR(tran.tran_d) BETWEEN 2008 AND 2010) GROUP BY tstDb1.b1.cat) tmp ORDER BY r DESC LIMIT 60;',
           beforeCursor: 'SELECT tmp.bc, ROUND(tmp.r, 2) AS r FROM ( SELECT tstDb1.b1.cat AS bc, SUM(tstDb1.b1.price * tran.qua) AS r FROM tstDb1.b1 JOIN [SHUFFLE] tran ON ( tran.b_id = tstDb1.b1.id AND YEAR(tran.tran_d) BETWEEN 2008 AND 2010) GROUP BY tstDb1.b1.cat) tmp ORDER BY r DESC LIMIT 60;',
@@ -5080,6 +5130,19 @@ define([
     });
     });
 
 
     describe('ORDER BY Clause', function () {
     describe('ORDER BY Clause', function () {
+      xit('should suggest keywords for "SELECT * FROM testTable GROUP BY a | LIMIT 10"', function () {
+        assertAutoComplete({
+          beforeCursor: 'SELECT * FROM testTable GROUP BY a ',
+          afterCursor: ' LIMIT 10',
+          hasLocations: true,
+          doesNotContainKeywords: ['LIMIT'],
+          containsKeywords: ['ORDER BY'],
+          expectedResult: {
+            lowerCase: false
+          }
+        });
+      });
+
       it('should suggest keywords for "SELECT * FROM testTable ORDER |"', function () {
       it('should suggest keywords for "SELECT * FROM testTable ORDER |"', function () {
         assertAutoComplete({
         assertAutoComplete({
           beforeCursor: 'SELECT * FROM testTable ORDER ',
           beforeCursor: 'SELECT * FROM testTable ORDER ',

Some files were not shown because too many files changed in this diff