Sfoglia il codice sorgente

HUE-9099 [frontend] Add new table constraints to Hive ALTER and CREATE statements

Johan Ahlen 6 anni fa
parent
commit
d55d9a1034

+ 2 - 0
desktop/core/src/desktop/js/parse/jison/sql/hive/sql.jisonlex

@@ -160,6 +160,7 @@
 'BUCKETS'                            { return 'BUCKETS'; }
 'CASCADE'                            { return 'CASCADE'; }
 'CHANGE'                             { return 'CHANGE'; }
+'CHECK'                              { return 'CHECK'; }
 'CLUSTER'                            { return 'CLUSTER'; }
 'CLUSTERED'                          { return 'CLUSTERED'; }
 'COLLECTION'                         { return 'COLLECTION'; }
@@ -283,6 +284,7 @@ STORED\s+AS\s+DIRECTORIES            { return 'STORED_AS_DIRECTORIES'; }
 'TRANSACTIONS'                       { return 'TRANSACTIONS'; }
 'UNARCHIVE'                          { return 'UNARCHIVE'; }
 'UNIONTYPE'                          { return 'UNIONTYPE'; }
+'UNIQUE'                             { return 'UNIQUE'; }
 'USE'                                { parser.determineCase(yytext); return 'USE'; }
 'VIEW'                               { return 'VIEW'; }
 'WAIT'                               { return 'WAIT'; }

+ 3 - 2
desktop/core/src/desktop/js/parse/jison/sql/hive/sql_alter.jison

@@ -125,7 +125,7 @@ AlterIndex_EDIT
 AlterTable
  : AlterTableLeftSide 'ADD' OptionalIfNotExists PartitionSpec OptionalHdfsLocation OptionalPartitionSpecs
  | AlterTableLeftSide 'ADD' 'CONSTRAINT' RegularOrBacktickedIdentifier PrimaryKeySpecification
- | AlterTableLeftSide 'ADD' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
+ | AlterTableLeftSide 'ADD' TableConstraint
  | AlterTableLeftSide 'RENAME' 'TO' RegularOrBackTickedSchemaQualifiedName
  | AlterTableLeftSide ClusteredBy
  | AlterTableLeftSide 'SKEWED' 'BY' ParenthesizedColumnList 'ON' ParenthesizedSkewedValueList OptionalStoredAsDirectories
@@ -147,6 +147,7 @@ AlterTable
 AlterTable_EDIT
  : AlterTableLeftSide_EDIT
  | AlterTableLeftSide_EDIT 'ADD' OptionalIfNotExists PartitionSpec OptionalHdfsLocation OptionalPartitionSpecs
+ | AlterTableLeftSide_EDIT TableConstraint
  | AlterTableLeftSide_EDIT 'RENAME' 'TO' RegularOrBackTickedSchemaQualifiedName
  | AlterTableLeftSide_EDIT ClusteredBy
  | AlterTableLeftSide_EDIT 'SKEWED' 'BY' ParenthesizedColumnList 'ON' ParenthesizedSkewedValueList OptionalStoredAsDirectories
@@ -194,7 +195,7 @@ AlterTable_EDIT
  | AlterTableLeftSide 'ADD' 'CONSTRAINT' 'CURSOR'
  | AlterTableLeftSide 'ADD' 'CONSTRAINT' RegularOrBacktickedIdentifier 'CURSOR'
    {
-     parser.suggestKeywords(['FOREIGN KEY', 'PRIMARY KEY']);
+     parser.suggestKeywords(['CHECK', 'FOREIGN KEY', 'PRIMARY KEY', 'UNIQUE']);
    }
  | AlterTableLeftSide 'ADD' 'CONSTRAINT' RegularOrBacktickedIdentifier PrimaryKeySpecification_EDIT
  | AlterTableLeftSide 'ADD' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification_EDIT

+ 52 - 31
desktop/core/src/desktop/js/parse/jison/sql/hive/sql_create.jison

@@ -285,12 +285,12 @@ OptionalColumnSpecificationsOrLike_EDIT
 
 ParenthesizedColumnSpecificationList
  : '(' ColumnSpecificationList ')'                              -> $2
- | '(' ColumnSpecificationList ',' ConstraintSpecification ')'  -> $2
+ | '(' ColumnSpecificationList ',' TableConstraints ')'  -> $2
  ;
 
 ParenthesizedColumnSpecificationList_EDIT
  : '(' ColumnSpecificationList_EDIT RightParenthesisOrError
- | '(' ColumnSpecificationList ',' ConstraintSpecification_EDIT RightParenthesisOrError
+ | '(' ColumnSpecificationList ',' TableConstraints_EDIT RightParenthesisOrError
  | '(' ColumnSpecificationList ',' 'CURSOR' RightParenthesisOrError
    {
      parser.suggestKeywords([{ value: 'PRIMARY KEY', weight: 2 }, { value: 'CONSTRAINT', weight: 1 }]);
@@ -334,6 +334,7 @@ ColumnSpecification
      if (!$4) {
        keywords = keywords.concat([
          { value: 'COMMENT', weight: 1 },
+         { value: 'CHECK', weight: 2 },
          { value: 'PRIMARY KEY', weight: 2 },
          { value: 'UNIQUE', weight: 2 },
          { value: 'NOT NULL', weight: 2 },
@@ -370,12 +371,13 @@ ColumnOptions
  ;
 
 ColumnOption
- : 'PRIMARY' 'KEY' ColumnOptionOptionals        -> $3
- | 'PRIMARY'                                    -> { suggestKeywords: [{ value: 'KEY', weight: 3 }] }
- | 'UNIQUE' ColumnOptionOptionals               -> $2
- | 'NOT' 'NULL' ColumnOptionOptionals           -> $3
- | 'NOT'                                        -> { suggestKeywords: [{ value: 'NULL', weight: 3 }] }
- | 'DEFAULT' DefaultValue ColumnOptionOptionals -> $3
+ : 'PRIMARY' 'KEY' ColumnOptionOptionals                  -> $3
+ | 'PRIMARY'                                              -> { suggestKeywords: [{ value: 'KEY', weight: 3 }] }
+ | 'UNIQUE' ColumnOptionOptionals                         -> $2
+ | 'NOT' 'NULL' ColumnOptionOptionals                     -> $3
+ | 'NOT'                                                  -> { suggestKeywords: [{ value: 'NULL', weight: 3 }] }
+ | 'DEFAULT' DefaultValue ColumnOptionOptionals           -> $3
+ | 'CHECK' '(' ValueExpression ')' ColumnOptionOptionals  -> $5
  | 'DEFAULT'
    {
      $$ = {
@@ -587,35 +589,62 @@ GreaterThanOrError
  | error
  ;
 
-ConstraintSpecification
+TableConstraints
  : PrimaryKeySpecification
- | 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
- | PrimaryKeySpecification ',' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
+ | ConstraintList
+ | PrimaryKeySpecification ',' ConstraintList
  ;
 
-ConstraintSpecification_EDIT
+TableConstraints_EDIT
  : PrimaryKeySpecification_EDIT
  | PrimaryKeySpecification ',' 'CURSOR'
    {
      parser.suggestKeywords(['CONSTRAINT']);
    }
- | PrimaryKeySpecification ',' 'CONSTRAINT' RegularOrBacktickedIdentifier 'CURSOR'
+ | ConstraintList_EDIT
+ | PrimaryKeySpecification ',' ConstraintList_EDIT
+ | PrimaryKeySpecification_EDIT ',' ConstraintList
+ ;
+
+ConstraintList
+ : TableConstraint
+ | ConstraintList ',' TableConstraint
+ ;
+
+ConstraintList_EDIT
+ : TableConstraint_EDIT
+ | ConstraintList ',' TableConstraint_EDIT
+ ;
+
+TableConstraint
+ : TableConstraintLeftPart OptionalDisable OptionalNovalidate OptionalRelyOrNorely
+ ;
+
+TableConstraint_EDIT
+ : TableConstraintLeftPart_EDIT OptionalDisable OptionalNovalidate OptionalRelyOrNorely
+ | TableConstraintLeftPart OptionalDisable OptionalNovalidate OptionalRelyOrNorely 'CURSOR'
    {
-     parser.suggestKeywords(['FOREIGN KEY']);
+     parser.suggestKeywordsForOptionalsLR([$4, $3, $2], [
+       [{ value: 'RELY', weight: 1 }, { value: 'NORELY', weight: 1 }],
+       { value: 'NOVALIDATE', weight: 2 },
+       { value: 'DISABLE', weight: 3 }
+     ]);
    }
- | PrimaryKeySpecification ',' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification_EDIT
- | PrimaryKeySpecification_EDIT ',' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
- | 'CONSTRAINT' RegularOrBacktickedIdentifier 'CURSOR'
+ ;
+
+TableConstraintLeftPart
+ : 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
+ | 'CONSTRAINT' RegularOrBacktickedIdentifier 'CHECK' '(' ValueExpression ')'
+ | 'CONSTRAINT' RegularOrBacktickedIdentifier 'UNIQUE' ParenthesizedColumnList
+ ;
+
+TableConstraintLeftPart_EDIT
+ : 'CONSTRAINT' RegularOrBacktickedIdentifier 'CURSOR'
    {
      parser.suggestKeywords(['CHECK', 'FOREIGN KEY', 'UNIQUE']);
    }
  | 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification_EDIT
- | 'CURSOR' 'CONSTRAINT' RegularOrBacktickedIdentifier ForeignKeySpecification
-   {
-     parser.suggestKeywords(['PRIMARY KEY']);
-   }
  ;
-
 PrimaryKeySpecification
  : PrimaryKey ParenthesizedColumnList OptionalDisable OptionalNovalidate OptionalRelyOrNorely
  ;
@@ -633,7 +662,7 @@ PrimaryKeySpecification_EDIT
  ;
 
 ForeignKeySpecification
- : 'FOREIGN' 'KEY' ParenthesizedColumnList 'REFERENCES' SchemaQualifiedTableIdentifier ParenthesizedColumnList OptionalDisable OptionalNovalidate OptionalRelyOrNorely
+ : 'FOREIGN' 'KEY' ParenthesizedColumnList 'REFERENCES' SchemaQualifiedTableIdentifier ParenthesizedColumnList
    {
      parser.addTablePrimary($5);
    }
@@ -659,14 +688,6 @@ ForeignKeySpecification_EDIT
    {
      parser.addTablePrimary($5);
    }
- | 'FOREIGN' 'KEY' ParenthesizedColumnList 'REFERENCES' SchemaQualifiedTableIdentifier ParenthesizedColumnList OptionalDisable OptionalNovalidate OptionalRelyOrNorely 'CURSOR'
-   {
-     parser.addTablePrimary($5);
-     parser.suggestKeywordsForOptionalsLR([$9, $8, $7], [
-        [{ value: 'RELY', weight: 1 }, { value: 'NORELY', weight: 1 }],
-        { value: 'NOVALIDATE', weight: 2 },
-        { value: 'DISABLE', weight: 1 }]);
-   }
  ;
 
 OptionalRelyOrNorely

+ 1 - 0
desktop/core/src/desktop/js/parse/jison/sql/hive/sql_main.jison

@@ -107,6 +107,7 @@ NonReservedKeyword
  | 'BUCKETS'
  | 'CASCADE'
  | 'CHANGE'
+ | 'CHECK'
  | 'CLUSTERED'
  | 'COLLECTION'
  | 'COLUMNS'

File diff suppressed because it is too large
+ 0 - 0
desktop/core/src/desktop/js/parse/sql/hive/hiveAutocompleteParser.js


File diff suppressed because it is too large
+ 0 - 0
desktop/core/src/desktop/js/parse/sql/hive/hiveSyntaxParser.js


+ 6 - 16
desktop/core/src/desktop/js/parse/sql/hive/test/hiveAutocompleteParser.Alter.test.js

@@ -372,7 +372,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });
@@ -410,18 +410,6 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
       });
     });
 
-    it('should suggest keywords for "ALTER TABLE bar ADD CONSTRAINT boo FOREIGN KEY (bla) REFERENCES tbl(col) DISABLE NOVALIDATE |"', () => {
-      assertAutoComplete({
-        beforeCursor:
-          'ALTER TABLE bar ADD CONSTRAINT boo FOREIGN KEY (bla) REFERENCES tbl(col) DISABLE NOVALIDATE ',
-        afterCursor: '',
-        expectedResult: {
-          lowerCase: false,
-          suggestKeywords: ['NORELY', 'RELY']
-        }
-      });
-    });
-
     it('should suggest keywords for "ALTER TABLE bar ADD CONSTRAINT boo PRIMARY KEY (id) |"', () => {
       assertAutoComplete({
         beforeCursor: 'ALTER TABLE bar ADD CONSTRAINT boo PRIMARY KEY (id) ',
@@ -600,6 +588,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         expectedResult: {
           lowerCase: false,
           suggestKeywords: [
+            'CHECK',
             'DEFAULT',
             'NOT NULL',
             'PRIMARY KEY',
@@ -1033,7 +1022,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });
@@ -1090,6 +1079,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         expectedResult: {
           lowerCase: false,
           suggestKeywords: [
+            'CHECK',
             'DEFAULT',
             'NOT NULL',
             'PRIMARY KEY',
@@ -1243,7 +1233,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });
@@ -1342,7 +1332,7 @@ describe('hiveAutocompleteParser.js ALTER statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });

+ 76 - 5
desktop/core/src/desktop/js/parse/sql/hive/test/hiveAutocompleteParser.Create.test.js

@@ -684,6 +684,56 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
       });
     });
 
+    it('should handle "CREATE TABLE foo (id DOUBLE, foo INT, PRIMARY KEY (id), CONSTRAINT boo UNIQUE (foo)); |"', () => {
+      assertAutoComplete({
+        beforeCursor:
+          'CREATE TABLE foo (id DOUBLE, foo INT, PRIMARY KEY (id), CONSTRAINT boo UNIQUE (foo)); ',
+        afterCursor: '',
+        containsKeywords: ['SELECT'],
+        noErrors: true,
+        expectedResult: {
+          lowerCase: false
+        }
+      });
+    });
+
+    it('should handle "CREATE TABLE foo (id DOUBLE, foo INT, CONSTRAINT boo UNIQUE (foo, id) DISABLE NOVALIDATE, CONSTRAINT baa CHECK (id > 0) DISABLE NOVALIDATE); |"', () => {
+      assertAutoComplete({
+        beforeCursor:
+          'CREATE TABLE foo (id DOUBLE, foo INT, CONSTRAINT boo UNIQUE (foo, id) DISABLE NOVALIDATE, CONSTRAINT baa CHECK (id > 0) DISABLE NOVALIDATE); ',
+        afterCursor: '',
+        containsKeywords: ['SELECT'],
+        noErrors: true,
+        expectedResult: {
+          lowerCase: false
+        }
+      });
+    });
+
+    it('should handle "CREATE TABLE foo (id DOUBLE CHECK (id > 0)); |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE TABLE foo (id DOUBLE CHECK (id > 0)); ',
+        afterCursor: '',
+        containsKeywords: ['SELECT'],
+        noErrors: true,
+        expectedResult: {
+          lowerCase: false
+        }
+      });
+    });
+
+    it('should handle "CREATE TABLE foo (id DOUBLE, CONSTRAINT foo CHECK (id > 0)); |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE TABLE foo (id DOUBLE, CONSTRAINT foo CHECK (id > 0)); ',
+        afterCursor: '',
+        containsKeywords: ['SELECT'],
+        noErrors: true,
+        expectedResult: {
+          lowerCase: false
+        }
+      });
+    });
+
     it('should handle "CREATE TABLE foo (id INT);|"', () => {
       assertAutoComplete({
         beforeCursor: 'CREATE TABLE foo (id INT);',
@@ -844,7 +894,7 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });
@@ -911,7 +961,7 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['FOREIGN KEY']
+          suggestKeywords: ['CHECK', 'FOREIGN KEY', 'UNIQUE']
         }
       });
     });
@@ -960,7 +1010,19 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['NOVALIDATE', 'DISABLE', 'NORELY', 'RELY']
+          suggestKeywords: ['DISABLE', 'NOVALIDATE', 'NORELY', 'RELY']
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE TABLE foo (id int, CONSTRAINT boo FOREIGN KEY (bla) REFERENCES sometbl(boo) DISABLE |"', () => {
+      assertAutoComplete({
+        beforeCursor:
+          'CREATE TABLE foo (id int, CONSTRAINT boo FOREIGN KEY (bla) REFERENCES sometbl(boo) DISABLE ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: ['NOVALIDATE', 'NORELY', 'RELY']
         }
       });
     });
@@ -1387,7 +1449,15 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['PRECISION', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: [
+            'PRECISION',
+            'CHECK',
+            'DEFAULT',
+            'NOT NULL',
+            'PRIMARY KEY',
+            'UNIQUE',
+            'COMMENT'
+          ]
         }
       });
     });
@@ -1404,6 +1474,7 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
             'CURRENT_USER()',
             'LITERAL',
             'NULL',
+            'CHECK',
             'DEFAULT',
             'NOT NULL',
             'PRIMARY KEY',
@@ -1543,7 +1614,7 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
         afterCursor: '',
         expectedResult: {
           lowerCase: false,
-          suggestKeywords: ['DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
+          suggestKeywords: ['CHECK', 'DEFAULT', 'NOT NULL', 'PRIMARY KEY', 'UNIQUE', 'COMMENT']
         }
       });
     });

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