Browse Source

HUE-9099 [frontend] Add parser support for Hive CREATE MATERIALIZED VIEW

Johan Ahlen 6 years ago
parent
commit
119306aac8

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

@@ -187,6 +187,7 @@
 'DIRECTORY'                          { this.begin('hdfs'); return 'DIRECTORY'; }
 'DISABLE'                            { return 'DISABLE'; }
 'DISTRIBUTE'                         { return 'DISTRIBUTE'; }
+'DISTRIBUTED'                        { return 'DISTRIBUTED'; }
 DOUBLE\s+PRECISION                   { return 'DOUBLE_PRECISION'; }
 'ENABLE'                             { return 'ENABLE'; }
 'ESCAPED'                            { return 'ESCAPED'; }
@@ -218,6 +219,7 @@ DOUBLE\s+PRECISION                   { return 'DOUBLE_PRECISION'; }
 'LOCATION'                           { this.begin('hdfs'); return 'LOCATION'; }
 'LOCK'                               { return 'LOCK'; }
 'LOCKS'                              { return 'LOCKS'; }
+'MATERIALIZED'                       { return 'MATERIALIZED'; }
 'MATCHED'                            { return 'MATCHED'; }
 'MERGE'                              { return 'MERGE'; }
 'METADATA'                           { return 'METADATA'; }
@@ -252,6 +254,7 @@ OVERWRITE\s+DIRECTORY                { this.begin('hdfs'); return 'OVERWRITE_DIR
 'REPLACE'                            { return 'REPLACE'; }
 'REPLICATION'                        { return 'REPLICATION'; }
 'RESTRICT'                           { return 'RESTRICT'; }
+'REWRITE'                            { return 'REWRITE'; }
 'ROLE'                               { return 'ROLE'; }
 'ROLES'                              { return 'ROLES'; }
 'SCHEMA'                             { return 'SCHEMA'; }

+ 150 - 5
desktop/core/src/desktop/js/parse/jison/sql/hive/sql_create.jison

@@ -26,6 +26,7 @@ CreateStatement
  : DatabaseDefinition
  | TableDefinition
  | ViewDefinition
+ | MaterializedViewDefinition
  | RoleDefinition
  | FunctionDefinition
  | IndexDefinition
@@ -36,6 +37,7 @@ CreateStatement_EDIT
  : DatabaseDefinition_EDIT
  | TableDefinition_EDIT
  | ViewDefinition_EDIT
+ | MaterializedViewDefinition_EDIT
  | FunctionDefinition_EDIT
  | IndexDefinition_EDIT
  | MacroDefinition_EDIT
@@ -47,7 +49,7 @@ CreateStatement_EDIT
        if ($2 && !$3) {
          parser.suggestKeywords(['EXTERNAL TABLE', 'FUNCTION', 'MACRO', 'TABLE']);
        } else if (!$2 && !$3) {
-         parser.suggestKeywords(['DATABASE', 'EXTERNAL TABLE', 'FUNCTION', 'INDEX', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY EXTERNAL TABLE', 'TEMPORARY FUNCTION', 'TEMPORARY MACRO', 'TEMPORARY TABLE', 'TRANSACTIONAL TABLE', 'VIEW']);
+         parser.suggestKeywords(['DATABASE', 'EXTERNAL TABLE', 'FUNCTION', 'INDEX', 'MATERIALIZED VIEW', 'ROLE', 'SCHEMA', 'TABLE', 'TEMPORARY EXTERNAL TABLE', 'TEMPORARY FUNCTION', 'TEMPORARY MACRO', 'TEMPORARY TABLE', 'TRANSACTIONAL TABLE', 'VIEW']);
        } else if ($3) {
          parser.suggestKeywords(['TABLE']);
        }
@@ -207,7 +209,7 @@ TableDefinitionRightPart_EDIT
    OptionalWithSerdeproperties HdfsLocation_EDIT OptionalTblproperties OptionalAsSelectStatement
  | TableIdentifierAndOptionalColumnSpecification OptionalComment OptionalPartitionedBy
    OptionalClusteredBy OptionalSkewedBy OptionalRowFormat OptionalStoredAsOrBy
-   OptionalWithSerdeproperties OptionalHdfsLocation OptionalTblproperties OptionalAsSelectStatement_EDIT
+   OptionalWithSerdeproperties OptionalHdfsLocation OptionalTblproperties AsSelectStatement_EDIT
  | TableIdentifierAndOptionalColumnSpecification OptionalComment OptionalPartitionedBy
    OptionalClusteredBy OptionalSkewedBy OptionalRowFormat OptionalStoredAsOrBy
    OptionalWithSerdeproperties OptionalHdfsLocation OptionalTblproperties 'CURSOR'
@@ -737,6 +739,23 @@ PartitionedBy_EDIT
    }
  ;
 
+OptionalPartitionedOn
+ :
+ | PartitionedOn
+ ;
+
+PartitionedOn
+ : 'PARTITIONED' 'ON' ParenthesizedColumnList
+ ;
+
+PartitionedOn_EDIT
+ : 'PARTITIONED' 'CURSOR'
+   {
+     parser.suggestKeywords(['ON']);
+   }
+ | 'PARTITIONED' 'ON' ParenthesizedColumnList_EDIT
+ ;
+
 LessThanOrEqualTo
  : '<'
  | 'COMPARISON_OPERATOR' // This is fine for autocompletion
@@ -774,6 +793,58 @@ ClusteredBy_EDIT
  | 'CLUSTERED' 'BY' ParenthesizedColumnList OptionalSortedBy_EDIT
  ;
 
+OptionalClusteredOrDistributedOn
+ :
+ | ClusteredOn
+ | DistributedOn SortedOn
+ ;
+
+ClusteredOrDistributedOn_EDIT
+ : ClusteredOn_EDIT
+ | DistributedOn_EDIT
+ | DistributedOn 'CURSOR'
+   {
+     parser.suggestKeywords(['SORTED ON']);
+   }
+ | DistributedOn SortedOn_EDIT
+ ;
+
+ClusteredOn
+ : 'CLUSTERED' 'ON' ParenthesizedColumnList
+ ;
+
+ClusteredOn_EDIT
+ : 'CLUSTERED' 'CURSOR'
+   {
+     parser.suggestKeywords(['ON']);
+   }
+ | 'CLUSTERED' 'ON' ParenthesizedColumnList_EDIT
+ ;
+
+DistributedOn
+ : 'DISTRIBUTED' 'ON' ParenthesizedColumnList
+ ;
+
+DistributedOn_EDIT
+ : 'DISTRIBUTED' 'CURSOR'
+   {
+     parser.suggestKeywords(['ON']);
+   }
+ | 'DISTRIBUTED' 'ON' ParenthesizedColumnList_EDIT
+ ;
+
+SortedOn
+ : 'SORTED' 'ON' ParenthesizedColumnList
+ ;
+
+SortedOn_EDIT
+ : 'SORTED' 'CURSOR'
+   {
+     parser.suggestKeywords(['ON']);
+   }
+ | 'SORTED' 'ON' ParenthesizedColumnList_EDIT
+ ;
+
 OptionalSortedBy
  :
  | 'SORTED' 'BY' ParenthesizedSortList
@@ -1090,10 +1161,14 @@ TblProperties
 
 OptionalAsSelectStatement
  :
- | 'AS' CommitLocations QuerySpecification
+ | AsSelectStatement
+ ;
+
+AsSelectStatement
+ : 'AS' CommitLocations QuerySpecification
  ;
 
-OptionalAsSelectStatement_EDIT
+AsSelectStatement_EDIT
  : 'AS' CommitLocations 'CURSOR'
    {
      parser.suggestKeywords(['SELECT']);
@@ -1108,6 +1183,76 @@ CommitLocations
    }
  ;
 
+MaterializedViewDefinition
+ : 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier
+    OptionalDisableRewrite OptionalComment OptionalPartitionedOn OptionalClusteredOrDistributedOn
+    OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation OptionalTblproperties
+    AsSelectStatement
+ ;
+
+MaterializedViewDefinition_EDIT
+ : 'CREATE' 'MATERIALIZED' 'CURSOR'
+   {
+     parser.suggestKeywords(['VIEW']);
+   }
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists 'CURSOR'
+   {
+     if (!$4) {
+       parser.suggestKeywords(['IF NOT EXISTS']);
+     }
+     parser.suggestDatabases({ appendDot: true });
+   }
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties 'CURSOR'
+   {
+     parser.suggestKeywordsForOptionalsLR([undefined, $12, $11, $10, $9, $8, $7, $6, $5], [
+       { value: 'AS SELECT', weight: 1 },
+       { value: 'TBLPROPERTIES', weight: 2 },
+       { value: 'LOCATION', weight: 3 },
+       [{ value: 'ROW FORMAT', weight: 4 }, { value: 'STORED AS', weight: 4 }, { value: 'STORED BY', weight: 4 }],
+       [{ value: 'CLUSTERED ON', weight: 5 }, { value: 'DISTRIBUTED ON', weight: 5 }],
+       { value: 'PARTITIONED ON', weight: 6 },
+       { value: 'COMMENT', weight: 7 },
+       { value: 'DISABLE REWRITE', weight: 8 }
+     ]);
+   }
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier DisableRewrite_EDIT OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   PartitionedOn_EDIT OptionalClusteredOrDistributedOn OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn ClusteredOrDistributedOn_EDIT OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn RowFormat_EDIT OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn OptionalRowFormat StoredAsOrBy_EDIT OptionalHdfsLocation
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn OptionalRowFormat OptionalStoredAsOrBy HdfsLocation_EDIT
+   OptionalTblproperties
+ | 'CREATE' 'MATERIALIZED' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalDisableRewrite OptionalComment
+   OptionalPartitionedOn OptionalClusteredOrDistributedOn OptionalRowFormat OptionalStoredAsOrBy OptionalHdfsLocation
+   OptionalTblproperties AsSelectStatement_EDIT
+ ;
+
+
+OptionalDisableRewrite
+ :
+ | 'DISABLE' 'REWRITE'
+ ;
+
+DisableRewrite_EDIT
+ : 'DISABLE' 'CURSOR'
+   {
+     parser.suggestKeywords(['REWRITE']);
+   }
+ ;
+
 ViewDefinition
  : 'CREATE' 'VIEW' OptionalIfNotExists SchemaQualifiedIdentifier OptionalParenthesizedViewColumnList OptionalComment OptionalTblproperties 'AS' QuerySpecification
  ;
@@ -1263,7 +1408,7 @@ RoleDefinition
 IndexDefinition
  : 'CREATE' 'INDEX' RegularOrBacktickedIdentifier 'ON' 'TABLE' ExistingTable ParenthesizedIndexColumnList
    'AS' IndexType OptionalWithDeferredRebuild OptionalIdxProperties OptionalInTable OptionalRowFormat
-   OptionalStoredAsOrBy  OptionalHdfsLocation OptionalTblproperties OptionalComment
+   OptionalStoredAsOrBy OptionalHdfsLocation OptionalTblproperties OptionalComment
  ;
 
 IndexDefinition_EDIT

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

@@ -156,6 +156,7 @@ NonReservedKeyword
  | 'LOAD'
  | 'LOCATION'
  | 'LOCKS'
+ | 'MATERIALIZED'
  | 'MATCHED'
  | 'MERGE'
  | 'METADATA'
@@ -189,6 +190,7 @@ NonReservedKeyword
  | 'REPLACE'
  | 'REPLICATION'
  | 'RESTRICT'
+ | 'REWRITE'
  | 'ROLE'
  | 'ROLES'
  | 'SCHEMAS'
@@ -216,6 +218,7 @@ NonReservedKeyword
  | 'TRANSACTIONS'
  | 'UNARCHIVE'
  | 'UNIONTYPE'
+ | 'UNIQUE'
  | 'USE'
  | 'USER'
  | 'VIEW'

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


+ 151 - 1
desktop/core/src/desktop/js/parse/sql/hive/test/hiveAutocompleteParser.Create.test.js

@@ -69,6 +69,7 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
           'EXTERNAL TABLE',
           'FUNCTION',
           'INDEX',
+          'MATERIALIZED VIEW',
           'ROLE',
           'SCHEMA',
           'TABLE',
@@ -1759,8 +1760,157 @@ describe('hiveAutocompleteParser.js CREATE statements', () => {
     });
   });
 
+  describe('CREATE MATERIALIZED VIEW', () => {
+    it(
+      'should handle "CREATE MATERIALIZED VIEW IF NOT EXISTS foo.bar DISABLE REWRITE \n' +
+        "COMMENT 'comment' PARTITIONED ON (a, b) CLUSTERED ON (c) ROW FORMAT DELIMITED \n" +
+        'STORED AS PARQUET LOCATION \'/baa/boo\' TBLPROPERTIES ("bla"=1) AS SELECT * from foo; |',
+      () => {
+        assertAutoComplete({
+          beforeCursor:
+            'CREATE MATERIALIZED VIEW IF NOT EXISTS foo.bar DISABLE REWRITE \n' +
+            "COMMENT 'comment' PARTITIONED ON (a, b) CLUSTERED ON (c) ROW FORMAT DELIMITED \n" +
+            'STORED AS PARQUET LOCATION \'/baa/boo\' TBLPROPERTIES ("bla"=1) AS SELECT * from foo; ',
+          afterCursor: '',
+          hasLocations: true,
+          noErrors: true,
+          containsKeywords: ['SELECT'],
+          expectedResult: {
+            lowerCase: false
+          }
+        });
+      }
+    );
+
+    it(
+      'should handle "CREATE MATERIALIZED VIEW foo.bar DISABLE REWRITE \n' +
+        "COMMENT 'comment' DISTRIBUTED ON (a, b) SORTED ON (c) AS SELECT * from foo; |",
+      () => {
+        assertAutoComplete({
+          beforeCursor:
+            'CREATE MATERIALIZED VIEW IF NOT EXISTS foo.bar DISABLE REWRITE \n' +
+            "COMMENT 'comment' DISTRIBUTED ON (a, b) SORTED ON (c) AS SELECT * from foo; ",
+          afterCursor: '',
+          hasLocations: true,
+          noErrors: true,
+          containsKeywords: ['SELECT'],
+          expectedResult: {
+            lowerCase: false
+          }
+        });
+      }
+    );
+
+    it('should suggest keywords for "CREATE MATERIALIZED |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: ['VIEW']
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW foo |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW foo ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: [
+            'DISABLE REWRITE',
+            'COMMENT',
+            'PARTITIONED ON',
+            'CLUSTERED ON',
+            'DISTRIBUTED ON',
+            'ROW FORMAT',
+            'STORED AS',
+            'STORED BY',
+            'LOCATION',
+            'TBLPROPERTIES',
+            'AS SELECT'
+          ]
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW bar DISABLE |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW bar DISABLE ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: ['REWRITE']
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW foo DISABLE REWRITE |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW foo DISABLE REWRITE ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: [
+            'COMMENT',
+            'PARTITIONED ON',
+            'CLUSTERED ON',
+            'DISTRIBUTED ON',
+            'ROW FORMAT',
+            'STORED AS',
+            'STORED BY',
+            'LOCATION',
+            'TBLPROPERTIES',
+            'AS SELECT'
+          ]
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW foo DISABLE REWRITE CLUSTERED ON (a) |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW foo DISABLE REWRITE CLUSTERED ON (a) ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: [
+            'ROW FORMAT',
+            'STORED AS',
+            'STORED BY',
+            'LOCATION',
+            'TBLPROPERTIES',
+            'AS SELECT'
+          ]
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW foo.bar DISABLE REWRITE DISTRIBUTED ON (a) |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW foo.bar DISABLE REWRITE DISTRIBUTED ON (a) ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: ['SORTED ON']
+        }
+      });
+    });
+
+    it('should suggest keywords for "CREATE MATERIALIZED VIEW foo DISABLE REWRITE CLUSTERED ON (a) |"', () => {
+      assertAutoComplete({
+        beforeCursor: 'CREATE MATERIALIZED VIEW foo DISABLE REWRITE CLUSTERED ON (a) AS ',
+        afterCursor: '',
+        expectedResult: {
+          lowerCase: false,
+          suggestKeywords: ['SELECT']
+        }
+      });
+    });
+  });
+
   describe('CREATE VIEW', () => {
-    it('should handle "CREATE VIEW foo AS SELECT a, | FROM tableOne"', () => {
+    it('should suggest columns "CREATE VIEW foo AS SELECT a, | FROM tableOne"', () => {
       assertAutoComplete({
         beforeCursor: 'CREATE VIEW foo AS SELECT a, ',
         afterCursor: ' FROM tableOne',

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