commit 3004450794e40af201da2e3cdc5c94b31f145ff4 (HEAD -> refs/heads/master) Author: Todd Lipcon Date: Wed May 1 16:15:03 2019 -0700 HIVE-21675. CREATE VIEW IF NOT EXISTS should not replace views diff --git ql/src/java/org/apache/hadoop/hive/ql/ddl/table/CreateViewOperation.java ql/src/java/org/apache/hadoop/hive/ql/ddl/table/CreateViewOperation.java index bb9b18977b..f8d95aa747 100644 --- ql/src/java/org/apache/hadoop/hive/ql/ddl/table/CreateViewOperation.java +++ ql/src/java/org/apache/hadoop/hive/ql/ddl/table/CreateViewOperation.java @@ -60,7 +60,11 @@ public int execute() throws HiveException { } } - if (!desc.isReplace() && !desc.getIfNotExists()) { + if (!desc.isReplace()) { + if (desc.getIfNotExists()) { + return 0; + } + // View already exists, thus we should be replacing throw new HiveException(ErrorMsg.TABLE_ALREADY_EXISTS.getMsg(desc.getViewName())); } diff --git ql/src/test/queries/clientpositive/create_view.q ql/src/test/queries/clientpositive/create_view.q index 3fe14f4ecd..392ebf4591 100644 --- ql/src/test/queries/clientpositive/create_view.q +++ ql/src/test/queries/clientpositive/create_view.q @@ -238,7 +238,12 @@ create view view18 as select v+1 from (select 1 as v) t; select * from view18; -- create view if not exists -create view if not exists view18 as select v+1 from (select 1 as v) t; +create view if not exists view18 as select "should be ignored"; +show create table view18; + +-- 'create or replace' +create or replace view view18 as select "should replace"; +show create table view18; DROP VIEW view1; DROP VIEW view2; diff --git ql/src/test/results/clientpositive/create_view.q.out ql/src/test/results/clientpositive/create_view.q.out index f3d7520e1c..7414d4749d 100644 --- ql/src/test/results/clientpositive/create_view.q.out +++ ql/src/test/results/clientpositive/create_view.q.out @@ -1618,16 +1618,40 @@ POSTHOOK: Input: _dummy_database@_dummy_table POSTHOOK: Input: default@view18 #### A masked pattern was here #### 2 -PREHOOK: query: create view if not exists view18 as select v+1 from (select 1 as v) t +PREHOOK: query: create view if not exists view18 as select "should be ignored" PREHOOK: type: CREATEVIEW PREHOOK: Input: _dummy_database@_dummy_table PREHOOK: Output: database:default PREHOOK: Output: default@view18 -POSTHOOK: query: create view if not exists view18 as select v+1 from (select 1 as v) t +POSTHOOK: query: create view if not exists view18 as select "should be ignored" POSTHOOK: type: CREATEVIEW POSTHOOK: Input: _dummy_database@_dummy_table POSTHOOK: Output: database:default POSTHOOK: Output: default@view18 +PREHOOK: query: show create table view18 +PREHOOK: type: SHOW_CREATETABLE +PREHOOK: Input: default@view18 +POSTHOOK: query: show create table view18 +POSTHOOK: type: SHOW_CREATETABLE +POSTHOOK: Input: default@view18 +CREATE VIEW `view18` AS select `t`.`v`+1 from (select 1 as `v`) `t` +PREHOOK: query: create or replace view view18 as select "should replace" +PREHOOK: type: CREATEVIEW +PREHOOK: Input: _dummy_database@_dummy_table +PREHOOK: Output: database:default +PREHOOK: Output: default@view18 +POSTHOOK: query: create or replace view view18 as select "should replace" +POSTHOOK: type: CREATEVIEW +POSTHOOK: Input: _dummy_database@_dummy_table +POSTHOOK: Output: database:default +POSTHOOK: Output: default@view18 +PREHOOK: query: show create table view18 +PREHOOK: type: SHOW_CREATETABLE +PREHOOK: Input: default@view18 +POSTHOOK: query: show create table view18 +POSTHOOK: type: SHOW_CREATETABLE +POSTHOOK: Input: default@view18 +CREATE VIEW `view18` AS select "should replace" PREHOOK: query: DROP VIEW view1 PREHOOK: type: DROPVIEW PREHOOK: Input: default@view1