diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java index 04abba2..1d2619e 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java @@ -3830,6 +3830,12 @@ public class DDLTask extends Task implements Serializable { */ private int createView(Hive db, CreateViewDesc crtView) throws HiveException { Table oldview = db.getTable(crtView.getViewName(), false); + // alter view as select + // requires the view must exist + if (crtView.getIsAlterViewAs() && oldview == null) { + throw new HiveException("Cannot ALTER VIEW AS SELECT if view currently does not exist"); + } + if (crtView.getOrReplace() && oldview != null) { // replace existing view if (!oldview.getTableType().equals(TableType.VIRTUAL_VIEW)) { diff --git ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g index 745a185..a920613 100644 --- ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g +++ ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g @@ -198,6 +198,7 @@ TOK_CREATEFUNCTION; TOK_DROPFUNCTION; TOK_CREATEVIEW; TOK_DROPVIEW; +TOK_ALTERVIEW_AS; TOK_ALTERVIEW_PROPERTIES; TOK_ALTERVIEW_ADDPARTS; TOK_ALTERVIEW_DROPPARTS; @@ -627,6 +628,8 @@ alterViewStatementSuffix -> ^(TOK_ALTERVIEW_ADDPARTS alterStatementSuffixAddPartitions) | alterStatementSuffixDropPartitions -> ^(TOK_ALTERVIEW_DROPPARTS alterStatementSuffixDropPartitions) + | name=tableName KW_AS selectStatement + -> ^(TOK_ALTERVIEW_AS $name selectStatement) ; alterIndexStatementSuffix diff --git ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index f65ee96..690084f 100644 --- ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -8211,7 +8211,8 @@ public class SemanticAnalyzer extends BaseSemanticAnalyzer { } // analyze create view command - if (ast.getToken().getType() == HiveParser.TOK_CREATEVIEW) { + if (ast.getToken().getType() == HiveParser.TOK_CREATEVIEW || + ast.getToken().getType() == HiveParser.TOK_ALTERVIEW_AS) { child = analyzeCreateView(ast, qb); SessionState.get().setCommandType(HiveOperation.CREATEVIEW); if (child == null) { @@ -8918,6 +8919,7 @@ public class SemanticAnalyzer extends BaseSemanticAnalyzer { List cols = null; boolean ifNotExists = false; boolean orReplace = false; + boolean isAlterViewAs = false; String comment = null; ASTNode selectStmt = null; Map tblProps = null; @@ -8959,8 +8961,14 @@ public class SemanticAnalyzer extends BaseSemanticAnalyzer { throw new SemanticException("Can't combine IF NOT EXISTS and OR REPLACE."); } + if (ast.getToken().getType() == HiveParser.TOK_ALTERVIEW_AS) { + isAlterViewAs = true; + orReplace = true; + } + createVwDesc = new CreateViewDesc( - tableName, cols, comment, tblProps, partColNames, ifNotExists, orReplace); + tableName, cols, comment, tblProps, partColNames, + ifNotExists, orReplace, isAlterViewAs); unparseTranslator.enable(); rootTasks.add(TaskFactory.get(new DDLWork(getInputs(), getOutputs(), createVwDesc), conf)); diff --git ql/src/java/org/apache/hadoop/hive/ql/plan/CreateViewDesc.java ql/src/java/org/apache/hadoop/hive/ql/plan/CreateViewDesc.java index cf398dd..30549e7 100644 --- ql/src/java/org/apache/hadoop/hive/ql/plan/CreateViewDesc.java +++ ql/src/java/org/apache/hadoop/hive/ql/plan/CreateViewDesc.java @@ -43,6 +43,7 @@ public class CreateViewDesc extends DDLDesc implements Serializable { private String comment; private boolean ifNotExists; private boolean orReplace; + private boolean isAlterViewAs; /** * For serialization only. @@ -52,7 +53,8 @@ public class CreateViewDesc extends DDLDesc implements Serializable { public CreateViewDesc(String viewName, List schema, String comment, Map tblProps, - List partColNames, boolean ifNotExists, boolean orReplace) { + List partColNames, boolean ifNotExists, + boolean orReplace, boolean isAlterViewAs) { this.viewName = viewName; this.schema = schema; this.comment = comment; @@ -60,6 +62,7 @@ public class CreateViewDesc extends DDLDesc implements Serializable { this.partColNames = partColNames; this.ifNotExists = ifNotExists; this.orReplace = orReplace; + this.isAlterViewAs = isAlterViewAs; } @Explain(displayName = "name") @@ -158,4 +161,13 @@ public class CreateViewDesc extends DDLDesc implements Serializable { public void setOrReplace(boolean orReplace) { this.orReplace = orReplace; } + + @Explain(displayName = "is alter view as select") + public boolean getIsAlterViewAs() { + return isAlterViewAs; + } + + public void setIsAlterViewAs(boolean isAlterViewAs) { + this.isAlterViewAs = isAlterViewAs; + } } diff --git ql/src/test/queries/clientnegative/alter_view_as_select1.q ql/src/test/queries/clientnegative/alter_view_as_select1.q new file mode 100644 index 0000000..a968482 --- /dev/null +++ ql/src/test/queries/clientnegative/alter_view_as_select1.q @@ -0,0 +1,16 @@ +-- Cannot add or drop partition columns with ALTER VIEW AS SELECT +-- if partitions currently exist + +CREATE VIEW testViewPart +PARTITIONED ON (value) +AS +SELECT key, value +FROM src +WHERE key=86; +DESCRIBE FORMATTED testViewPart; + +ALTER VIEW testViewPart +ADD PARTITION (value='val_86') PARTITION (value='val_xyz'); +DESCRIBE FORMATTED testViewPart; + +ALTER VIEW testViewPart as SELECT * FROM srcpart; diff --git ql/src/test/queries/clientnegative/alter_view_as_select2.q ql/src/test/queries/clientnegative/alter_view_as_select2.q new file mode 100644 index 0000000..fbdbbcf --- /dev/null +++ ql/src/test/queries/clientnegative/alter_view_as_select2.q @@ -0,0 +1,2 @@ +DROP VIEW testView; +ALTER VIEW testView AS SELECT * FROM srcpart; diff --git ql/src/test/queries/clientpositive/alter_view_as_select.q ql/src/test/queries/clientpositive/alter_view_as_select.q new file mode 100644 index 0000000..dcab3ca --- /dev/null +++ ql/src/test/queries/clientpositive/alter_view_as_select.q @@ -0,0 +1,13 @@ +DROP VIEW testView; +CREATE VIEW testView as SELECT * FROM srcpart; +DESCRIBE FORMATTED testView; + +ALTER VIEW testView AS SELECT value FROM src WHERE key=86; +DESCRIBE FORMATTED testView; + +ALTER VIEW testView AS +SELECT * FROM src +WHERE key > 80 AND key < 100 +ORDER BY key, value +LIMIT 10; +DESCRIBE FORMATTED testView; diff --git ql/src/test/results/clientnegative/alter_view_as_select1.q.out ql/src/test/results/clientnegative/alter_view_as_select1.q.out new file mode 100644 index 0000000..f5ecab6 --- /dev/null +++ ql/src/test/results/clientnegative/alter_view_as_select1.q.out @@ -0,0 +1,116 @@ +PREHOOK: query: -- Cannot add or drop partition columns with ALTER VIEW AS SELECT +-- if partitions currently exist + +CREATE VIEW testViewPart +PARTITIONED ON (value) +AS +SELECT key, value +FROM src +WHERE key=86 +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +POSTHOOK: query: -- Cannot add or drop partition columns with ALTER VIEW AS SELECT +-- if partitions currently exist + +CREATE VIEW testViewPart +PARTITIONED ON (value) +AS +SELECT key, value +FROM src +WHERE key=86 +POSTHOOK: type: CREATEVIEW +POSTHOOK: Output: default@testViewPart +#### A masked pattern was here #### +PREHOOK: query: DESCRIBE FORMATTED testViewPart +PREHOOK: type: DESCTABLE +POSTHOOK: query: DESCRIBE FORMATTED testViewPart +POSTHOOK: type: DESCTABLE +# col_name data_type comment + +key string None + +# Partition Information +# col_name data_type comment + +value string None + +# Detailed Table Information +Database: default +#### A masked pattern was here #### +Protect Mode: None +Retention: 0 +Table Type: VIRTUAL_VIEW +Table Parameters: +#### A masked pattern was here #### + +# Storage Information +SerDe Library: null +InputFormat: org.apache.hadoop.mapred.SequenceFileInputFormat +OutputFormat: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat +Compressed: No +Num Buckets: -1 +Bucket Columns: [] +Sort Columns: [] + +# View Information +View Original Text: SELECT key, value +FROM src +WHERE key=86 +View Expanded Text: SELECT `src`.`key`, `src`.`value` +FROM `default`.`src` +WHERE `src`.`key`=86 +PREHOOK: query: ALTER VIEW testViewPart +ADD PARTITION (value='val_86') PARTITION (value='val_xyz') +PREHOOK: type: ALTERTABLE_ADDPARTS +PREHOOK: Input: default@src +PREHOOK: Input: default@testviewpart +POSTHOOK: query: ALTER VIEW testViewPart +ADD PARTITION (value='val_86') PARTITION (value='val_xyz') +POSTHOOK: type: ALTERTABLE_ADDPARTS +POSTHOOK: Input: default@src +POSTHOOK: Input: default@testviewpart +POSTHOOK: Output: default@testviewpart@value=val_86 +POSTHOOK: Output: default@testviewpart@value=val_xyz +PREHOOK: query: DESCRIBE FORMATTED testViewPart +PREHOOK: type: DESCTABLE +POSTHOOK: query: DESCRIBE FORMATTED testViewPart +POSTHOOK: type: DESCTABLE +# col_name data_type comment + +key string None + +# Partition Information +# col_name data_type comment + +value string None + +# Detailed Table Information +Database: default +#### A masked pattern was here #### +Protect Mode: None +Retention: 0 +Table Type: VIRTUAL_VIEW +Table Parameters: +#### A masked pattern was here #### + +# Storage Information +SerDe Library: null +InputFormat: org.apache.hadoop.mapred.SequenceFileInputFormat +OutputFormat: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat +Compressed: No +Num Buckets: -1 +Bucket Columns: [] +Sort Columns: [] + +# View Information +View Original Text: SELECT key, value +FROM src +WHERE key=86 +View Expanded Text: SELECT `src`.`key`, `src`.`value` +FROM `default`.`src` +WHERE `src`.`key`=86 +PREHOOK: query: ALTER VIEW testViewPart as SELECT * FROM srcpart +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +FAILED: Error in metadata: Cannot add or drop partition columns with CREATE OR REPLACE VIEW if partitions currently exist +FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask diff --git ql/src/test/results/clientnegative/alter_view_as_select2.q.out ql/src/test/results/clientnegative/alter_view_as_select2.q.out new file mode 100644 index 0000000..1a0e008 --- /dev/null +++ ql/src/test/results/clientnegative/alter_view_as_select2.q.out @@ -0,0 +1,9 @@ +PREHOOK: query: DROP VIEW testView +PREHOOK: type: DROPVIEW +POSTHOOK: query: DROP VIEW testView +POSTHOOK: type: DROPVIEW +PREHOOK: query: ALTER VIEW testView AS SELECT * FROM srcpart +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +FAILED: Error in metadata: Cannot ALTER VIEW AS SELECT if view currently does not exist +FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask diff --git ql/src/test/results/clientpositive/alter_view_as_select.q.out ql/src/test/results/clientpositive/alter_view_as_select.q.out new file mode 100644 index 0000000..bc00e1e --- /dev/null +++ ql/src/test/results/clientpositive/alter_view_as_select.q.out @@ -0,0 +1,130 @@ +PREHOOK: query: DROP VIEW testView +PREHOOK: type: DROPVIEW +POSTHOOK: query: DROP VIEW testView +POSTHOOK: type: DROPVIEW +PREHOOK: query: CREATE VIEW testView as SELECT * FROM srcpart +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +POSTHOOK: query: CREATE VIEW testView as SELECT * FROM srcpart +POSTHOOK: type: CREATEVIEW +POSTHOOK: Output: default@testView +#### A masked pattern was here #### +PREHOOK: query: DESCRIBE FORMATTED testView +PREHOOK: type: DESCTABLE +POSTHOOK: query: DESCRIBE FORMATTED testView +POSTHOOK: type: DESCTABLE +# col_name data_type comment + +key string None +value string None +ds string None +hr string None + +# Detailed Table Information +Database: default +#### A masked pattern was here #### +Protect Mode: None +Retention: 0 +Table Type: VIRTUAL_VIEW +Table Parameters: +#### A masked pattern was here #### + +# Storage Information +SerDe Library: null +InputFormat: org.apache.hadoop.mapred.SequenceFileInputFormat +OutputFormat: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat +Compressed: No +Num Buckets: -1 +Bucket Columns: [] +Sort Columns: [] + +# View Information +View Original Text: SELECT * FROM srcpart +View Expanded Text: SELECT `srcpart`.`key`, `srcpart`.`value`, `srcpart`.`ds`, `srcpart`.`hr` FROM `default`.`srcpart` +PREHOOK: query: ALTER VIEW testView AS SELECT value FROM src WHERE key=86 +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +POSTHOOK: query: ALTER VIEW testView AS SELECT value FROM src WHERE key=86 +POSTHOOK: type: CREATEVIEW +POSTHOOK: Output: default@testview +#### A masked pattern was here #### +PREHOOK: query: DESCRIBE FORMATTED testView +PREHOOK: type: DESCTABLE +POSTHOOK: query: DESCRIBE FORMATTED testView +POSTHOOK: type: DESCTABLE +# col_name data_type comment + +value string None + +# Detailed Table Information +Database: default +#### A masked pattern was here #### +Protect Mode: None +Retention: 0 +Table Type: VIRTUAL_VIEW +Table Parameters: +#### A masked pattern was here #### + +# Storage Information +SerDe Library: null +InputFormat: org.apache.hadoop.mapred.SequenceFileInputFormat +OutputFormat: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat +Compressed: No +Num Buckets: -1 +Bucket Columns: [] +Sort Columns: [] + +# View Information +View Original Text: SELECT value FROM src WHERE key=86 +View Expanded Text: SELECT `src`.`value` FROM `default`.`src` WHERE `src`.`key`=86 +PREHOOK: query: ALTER VIEW testView AS +SELECT * FROM src +WHERE key > 80 AND key < 100 +ORDER BY key, value +LIMIT 10 +PREHOOK: type: CREATEVIEW +#### A masked pattern was here #### +POSTHOOK: query: ALTER VIEW testView AS +SELECT * FROM src +WHERE key > 80 AND key < 100 +ORDER BY key, value +LIMIT 10 +POSTHOOK: type: CREATEVIEW +POSTHOOK: Output: default@testview +#### A masked pattern was here #### +PREHOOK: query: DESCRIBE FORMATTED testView +PREHOOK: type: DESCTABLE +POSTHOOK: query: DESCRIBE FORMATTED testView +POSTHOOK: type: DESCTABLE +# col_name data_type comment + +key string None +value string None + +# Detailed Table Information +Database: default +#### A masked pattern was here #### +Protect Mode: None +Retention: 0 +Table Type: VIRTUAL_VIEW +Table Parameters: +#### A masked pattern was here #### + +# Storage Information +SerDe Library: null +InputFormat: org.apache.hadoop.mapred.SequenceFileInputFormat +OutputFormat: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat +Compressed: No +Num Buckets: -1 +Bucket Columns: [] +Sort Columns: [] + +# View Information +View Original Text: SELECT * FROM src +WHERE key > 80 AND key < 100 +ORDER BY key, value +LIMIT 10 +View Expanded Text: SELECT `src`.`key`, `src`.`value` FROM `default`.`src` +WHERE `src`.`key` > 80 AND `src`.`key` < 100 +ORDER BY `src`.`key`, `src`.`value` +LIMIT 10