Browse Source

HUE-7738 [editor] Add keywords suggestions to the Impala UDF reference

Johan Ahlen 5 years ago
parent
commit
a885302aee

+ 21 - 10
desktop/core/src/desktop/js/parse/jison/sql/impala/sql_udf.jison

@@ -397,56 +397,67 @@ ExtractFunction_EDIT
  : 'EXTRACT' '(' AnyCursor FromOrComma ValueExpression RightParenthesisOrError
  : 'EXTRACT' '(' AnyCursor FromOrComma ValueExpression RightParenthesisOrError
    {
    {
      parser.valueExpressionSuggest();
      parser.valueExpressionSuggest();
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 2 : 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' AnyCursor FromOrComma RightParenthesisOrError
  | 'EXTRACT' '(' AnyCursor FromOrComma RightParenthesisOrError
    {
    {
      parser.valueExpressionSuggest();
      parser.valueExpressionSuggest();
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['STRING'] : ['TIMESTAMP'] });
+     var isFrom = $4.toLowerCase() === 'from';
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 2 : 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' AnyCursor RightParenthesisOrError
  | 'EXTRACT' '(' AnyCursor RightParenthesisOrError
    {
    {
      parser.valueExpressionSuggest();
      parser.valueExpressionSuggest();
      parser.applyTypeToSuggestions({ types: ['STRING', 'TIMESTAMP'] });
      parser.applyTypeToSuggestions({ types: ['STRING', 'TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression_EDIT FromOrComma ValueExpression RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression_EDIT FromOrComma ValueExpression RightParenthesisOrError
    {
    {
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom === 'from' ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 2 : 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression_EDIT FromOrComma RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression_EDIT FromOrComma RightParenthesisOrError
    {
    {
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['STRING'] : ['TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 2 : 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression_EDIT RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression_EDIT RightParenthesisOrError
    {
    {
      parser.applyTypeToSuggestions({ types: ['STRING', 'TIMESTAMP'] });
      parser.applyTypeToSuggestions({ types: ['STRING', 'TIMESTAMP'] });
+     parser.applyArgumentTypesToSuggestions($1, 1);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression FromOrComma AnyCursor RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression FromOrComma AnyCursor RightParenthesisOrError
    {
    {
      parser.valueExpressionSuggest();
      parser.valueExpressionSuggest();
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 1 : 2);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' FromOrComma AnyCursor RightParenthesisOrError
  | 'EXTRACT' '(' FromOrComma AnyCursor RightParenthesisOrError
    {
    {
      parser.valueExpressionSuggest();
      parser.valueExpressionSuggest();
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 1 : 2);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression FromOrComma ValueExpression_EDIT RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression FromOrComma ValueExpression_EDIT RightParenthesisOrError
    {
    {
-     parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 1 : 2);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' FromOrComma ValueExpression_EDIT RightParenthesisOrError
  | 'EXTRACT' '(' FromOrComma ValueExpression_EDIT RightParenthesisOrError
    {
    {
-    parser.applyTypeToSuggestions({ types: $4.toLowerCase() === 'from' ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyTypeToSuggestions({ types: $4.isFrom ? ['TIMESTAMP'] : ['STRING'] });
+     parser.applyArgumentTypesToSuggestions($1, $4.isFrom ? 1 : 2);
      $$ = { function: $1, types: ['UDFREF'] };
      $$ = { function: $1, types: ['UDFREF'] };
    }
    }
  | 'EXTRACT' '(' ValueExpression 'CURSOR' ValueExpression RightParenthesisOrError
  | 'EXTRACT' '(' ValueExpression 'CURSOR' ValueExpression RightParenthesisOrError
@@ -470,8 +481,8 @@ ExtractFunction_EDIT
  ;
  ;
 
 
 FromOrComma
 FromOrComma
- : 'FROM'
- | ','
+ : 'FROM' -> { isFrom: true }
+ | ','    -> { isFrom: false }
  ;
  ;
 
 
 SumFunction
 SumFunction

File diff suppressed because it is too large
+ 141 - 141
desktop/core/src/desktop/js/parse/sql/impala/impalaAutocompleteParser.js


File diff suppressed because it is too large
+ 142 - 142
desktop/core/src/desktop/js/parse/sql/impala/impalaSyntaxParser.js


+ 25 - 4
desktop/core/src/desktop/js/parse/sql/impala/test/impalaAutocompleteParser.Select.test.js

@@ -2319,6 +2319,24 @@ describe('impalaAutocompleteParser.js SELECT statements', () => {
       });
       });
     });
     });
 
 
+    it('should suggest keywords for "SELECT extract(|) FROM bar"', () => {
+      assertAutoComplete({
+        beforeCursor: 'SELECT extract(',
+        afterCursor: ') FROM bar;',
+        containsKeywords: ['CASE'],
+        expectedResult: {
+          lowerCase: false,
+          suggestColumns: {
+            types: ['STRING', 'TIMESTAMP'],
+            source: 'select',
+            tables: [{ identifierChain: [{ name: 'bar' }] }]
+          },
+          suggestFunctions: { types: ['STRING', 'TIMESTAMP'] },
+          udfArgument: { name: 'extract', position: 1 }
+        }
+      });
+    });
+
     it('should suggest columns for "SELECT extract(bla ,|  FROM bar;"', () => {
     it('should suggest columns for "SELECT extract(bla ,|  FROM bar;"', () => {
       assertAutoComplete({
       assertAutoComplete({
         beforeCursor: 'SELECT extract(bla ,',
         beforeCursor: 'SELECT extract(bla ,',
@@ -2331,7 +2349,8 @@ describe('impalaAutocompleteParser.js SELECT statements', () => {
             source: 'select',
             source: 'select',
             types: ['STRING'],
             types: ['STRING'],
             tables: [{ identifierChain: [{ name: 'bar' }] }]
             tables: [{ identifierChain: [{ name: 'bar' }] }]
-          }
+          },
+          udfArgument: { name: 'extract', position: 2 }
         }
         }
       });
       });
     });
     });
@@ -2348,12 +2367,13 @@ describe('impalaAutocompleteParser.js SELECT statements', () => {
             source: 'select',
             source: 'select',
             types: ['TIMESTAMP'],
             types: ['TIMESTAMP'],
             tables: [{ identifierChain: [{ name: 'bar' }] }]
             tables: [{ identifierChain: [{ name: 'bar' }] }]
-          }
+          },
+          udfArgument: { name: 'extract', position: 1 }
         }
         }
       });
       });
     });
     });
 
 
-    it('should suggest columns for "SELECT extract(bla ,|)  FROM bar;"', () => {
+    it('should suggest columns for "SELECT extract(bla ,|) FROM bar;"', () => {
       assertAutoComplete({
       assertAutoComplete({
         beforeCursor: 'SELECT extract(bla ,',
         beforeCursor: 'SELECT extract(bla ,',
         afterCursor: ') FROM bar;',
         afterCursor: ') FROM bar;',
@@ -2365,7 +2385,8 @@ describe('impalaAutocompleteParser.js SELECT statements', () => {
             source: 'select',
             source: 'select',
             types: ['STRING'],
             types: ['STRING'],
             tables: [{ identifierChain: [{ name: 'bar' }] }]
             tables: [{ identifierChain: [{ name: 'bar' }] }]
-          }
+          },
+          udfArgument: { name: 'extract', position: 2 }
         }
         }
       });
       });
     });
     });

+ 124 - 7
desktop/core/src/desktop/js/sql/reference/impala/udfReference.js

@@ -717,7 +717,25 @@ const DATE_FUNCTIONS = {
   },
   },
   date_part: {
   date_part: {
     returnTypes: ['TIMESTAMP'],
     returnTypes: ['TIMESTAMP'],
-    arguments: [[{ type: 'STRING' }], [{ type: 'TIMESTAMP' }]],
+    arguments: [
+      [
+        {
+          type: 'STRING',
+          keywords: [
+            "'epoch'",
+            "'year'",
+            "'quarter'",
+            "'month'",
+            "'day'",
+            "'hour'",
+            "'minute'",
+            "'second'",
+            "'millisecond'"
+          ]
+        }
+      ],
+      [{ type: 'TIMESTAMP' }]
+    ],
     signature: 'date_part(STRING unit, TIMESTAMP timestamp)',
     signature: 'date_part(STRING unit, TIMESTAMP timestamp)',
     draggable: 'date_part()',
     draggable: 'date_part()',
     description:
     description:
@@ -734,7 +752,28 @@ const DATE_FUNCTIONS = {
   },
   },
   date_trunc: {
   date_trunc: {
     returnTypes: ['TIMESTAMP'],
     returnTypes: ['TIMESTAMP'],
-    arguments: [[{ type: 'STRING' }], [{ type: 'TIMESTAMP' }]],
+    arguments: [
+      [
+        {
+          type: 'STRING',
+          keywords: [
+            "'microseconds'",
+            "'milliseconds'",
+            "'second'",
+            "'minute'",
+            "'hour'",
+            "'day'",
+            "'week'",
+            "'month'",
+            "'year'",
+            "'decade'",
+            "'century'",
+            "'millennium'"
+          ]
+        }
+      ],
+      [{ type: 'TIMESTAMP' }]
+    ],
     signature: 'date_trunc(STRING unit, TIMESTAMP timestamp)',
     signature: 'date_trunc(STRING unit, TIMESTAMP timestamp)',
     draggable: 'date_trunc()',
     draggable: 'date_trunc()',
     description:
     description:
@@ -805,7 +844,25 @@ const DATE_FUNCTIONS = {
   },
   },
   extract: {
   extract: {
     returnTypes: ['INT'],
     returnTypes: ['INT'],
-    arguments: [[{ type: 'TIMESTAMP' }], [{ type: 'STRING' }]],
+    arguments: [
+      [{ type: 'TIMESTAMP' }],
+      [
+        {
+          type: 'STRING',
+          keywords: [
+            "'epoch'",
+            "'year'",
+            "'quarter'",
+            "'month'",
+            "'day'",
+            "'hour'",
+            "'minute'",
+            "'second'",
+            "'millisecond'"
+          ]
+        }
+      ]
+    ],
     signature: 'extract(TIMESTAMP date, STRING unit), extract(STRING unit FROM TIMESTAMP date)',
     signature: 'extract(TIMESTAMP date, STRING unit), extract(STRING unit FROM TIMESTAMP date)',
     draggable: 'extract()',
     draggable: 'extract()',
     description: 'Returns one of the numeric date or time fields from a TIMESTAMP value.'
     description: 'Returns one of the numeric date or time fields from a TIMESTAMP value.'
@@ -981,7 +1038,30 @@ const DATE_FUNCTIONS = {
   },
   },
   next_day: {
   next_day: {
     returnTypes: ['TIMESTAMP'],
     returnTypes: ['TIMESTAMP'],
-    arguments: [[{ type: 'TIMESTAMP' }], [{ type: 'STRING' }]],
+    arguments: [
+      [{ type: 'TIMESTAMP' }],
+      [
+        {
+          type: 'STRING',
+          keywords: [
+            "'Sunday'",
+            "'Sun'",
+            "'Monday'",
+            "'Mon'",
+            "'Tuesday'",
+            "'Tue'",
+            "'Wednesday'",
+            "'Wed'",
+            "'Thursday'",
+            "'Thu'",
+            "'Friday'",
+            "'Fri'",
+            "'Saturday'",
+            "'Sat'"
+          ]
+        }
+      ]
+    ],
     signature: 'next_day(TIMESTAMP date, STRING weekday)',
     signature: 'next_day(TIMESTAMP date, STRING weekday)',
     draggable: 'next_day()',
     draggable: 'next_day()',
     description:
     description:
@@ -1073,7 +1153,40 @@ const DATE_FUNCTIONS = {
   },
   },
   trunc: {
   trunc: {
     returnTypes: ['TIMESTAMP'],
     returnTypes: ['TIMESTAMP'],
-    arguments: [[{ type: 'TIMESTAMP' }], [{ type: 'STRING' }]],
+    arguments: [
+      [{ type: 'TIMESTAMP' }],
+      [
+        {
+          type: 'STRING',
+          keywords: [
+            "'SYYYY'",
+            "'YYYY'",
+            "'YEAR'",
+            "'SYEAR'",
+            "'YYY'",
+            "'YY'",
+            "'Y'",
+            "'Q'",
+            "'MONTH'",
+            "'MON'",
+            "'MM'",
+            "'RM'",
+            "'WW'",
+            "'W'",
+            "'DDD'",
+            "'DD'",
+            "'J'",
+            "'DAY'",
+            "'DY'",
+            "'D'",
+            "'HH'",
+            "'HH12'",
+            "'HH24'",
+            "'MI'"
+          ]
+        }
+      ]
+    ],
     signature: 'trunc(TIMESTAMP date, STRING unit)',
     signature: 'trunc(TIMESTAMP date, STRING unit)',
     draggable: 'trunc()',
     draggable: 'trunc()',
     description:
     description:
@@ -1804,7 +1917,7 @@ const BIT_FUNCTIONS = {
   },
   },
   countset: {
   countset: {
     returnTypes: ['T'],
     returnTypes: ['T'],
-    arguments: [[{ type: 'T' }], [{ type: 'INT', optional: true }]],
+    arguments: [[{ type: 'T' }], [{ type: 'INT', optional: true, keywords: ['0', '1'] }]],
     signature: 'countset(T<integer_type> a [, INT b])',
     signature: 'countset(T<integer_type> a [, INT b])',
     draggable: 'countset()',
     draggable: 'countset()',
     description:
     description:
@@ -1836,7 +1949,11 @@ const BIT_FUNCTIONS = {
   },
   },
   setbit: {
   setbit: {
     returnTypes: ['T'],
     returnTypes: ['T'],
-    arguments: [[{ type: 'T' }], [{ type: 'INT' }], [{ type: 'INT', optional: true }]],
+    arguments: [
+      [{ type: 'T' }],
+      [{ type: 'INT' }],
+      [{ type: 'INT', optional: true, keywords: ['0', '1'] }]
+    ],
     signature: 'setbit(T<integer_type> a, INT b [, INT c])',
     signature: 'setbit(T<integer_type> a, INT b [, INT c])',
     draggable: 'setbit()',
     draggable: 'setbit()',
     description:
     description:

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