Repository: hive
Updated Branches:
  refs/heads/branch-1 b9af10d28 -> 878e75b9e


HIVE-11441: No DDL allowed on table if user accidentally set table location 
wrong (Daniel Dai reviewed by Thejas Nair)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/b55b6d60
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/b55b6d60
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/b55b6d60

Branch: refs/heads/branch-1
Commit: b55b6d6000e6606a18e4108c404db3c8170c111e
Parents: 89f16ab
Author: Daniel Dai <da...@hortonworks.com>
Authored: Fri Aug 14 15:49:13 2015 -0700
Committer: Daniel Dai <da...@hortonworks.com>
Committed: Fri Aug 14 15:49:13 2015 -0700

----------------------------------------------------------------------
 .../apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java | 11 +++++++++++
 .../queries/clientnegative/alter_table_wrong_location.q  |  4 ++++
 .../clientnegative/alter_table_wrong_location.q.out      |  9 +++++++++
 3 files changed, 24 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/b55b6d60/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
----------------------------------------------------------------------
diff --git 
a/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java 
b/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
index 24ca663..ba8cdd3 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/DDLSemanticAnalyzer.java
@@ -21,6 +21,7 @@ package org.apache.hadoop.hive.ql.parse;
 import static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_DATABASELOCATION;
 import static 
org.apache.hadoop.hive.ql.parse.HiveParser.TOK_DATABASEPROPERTIES;
 
+import java.io.FileNotFoundException;
 import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
@@ -44,6 +45,7 @@ import org.antlr.runtime.tree.CommonTree;
 import org.antlr.runtime.tree.Tree;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hive.common.JavaUtils;
 import org.apache.hadoop.hive.conf.HiveConf;
@@ -1467,6 +1469,15 @@ public class DDLSemanticAnalyzer extends 
BaseSemanticAnalyzer {
       HashMap<String, String> partSpec) throws SemanticException {
 
     String newLocation = unescapeSQLString(ast.getChild(0).getText());
+    try {
+      // To make sure host/port pair is valid, the status of the location
+      // does not matter
+      FileSystem.get(new URI(newLocation), conf).getFileStatus(new 
Path(newLocation));
+    } catch (FileNotFoundException e) {
+      // Only check host/port pair is valid, wheter the file exist or not does 
not matter
+    } catch (Exception e) {
+      throw new SemanticException("Cannot connect to namenode, please check if 
host/port pair for " + newLocation + " is valid", e);
+    }
     addLocationToOutputs(newLocation);
     AlterTableDesc alterTblDesc = new AlterTableDesc(tableName, newLocation, 
partSpec);
 

http://git-wip-us.apache.org/repos/asf/hive/blob/b55b6d60/ql/src/test/queries/clientnegative/alter_table_wrong_location.q
----------------------------------------------------------------------
diff --git a/ql/src/test/queries/clientnegative/alter_table_wrong_location.q 
b/ql/src/test/queries/clientnegative/alter_table_wrong_location.q
new file mode 100644
index 0000000..3721867
--- /dev/null
+++ b/ql/src/test/queries/clientnegative/alter_table_wrong_location.q
@@ -0,0 +1,4 @@
+create table testwrongloc(id int);
+
+-- Assume port 12345 is not open
+alter table testwrongloc set location 
"hdfs://localhost:12345/tmp/testwrongloc";

http://git-wip-us.apache.org/repos/asf/hive/blob/b55b6d60/ql/src/test/results/clientnegative/alter_table_wrong_location.q.out
----------------------------------------------------------------------
diff --git 
a/ql/src/test/results/clientnegative/alter_table_wrong_location.q.out 
b/ql/src/test/results/clientnegative/alter_table_wrong_location.q.out
new file mode 100644
index 0000000..d788d55
--- /dev/null
+++ b/ql/src/test/results/clientnegative/alter_table_wrong_location.q.out
@@ -0,0 +1,9 @@
+PREHOOK: query: create table testwrongloc(id int)
+PREHOOK: type: CREATETABLE
+PREHOOK: Output: database:default
+PREHOOK: Output: default@testwrongloc
+POSTHOOK: query: create table testwrongloc(id int)
+POSTHOOK: type: CREATETABLE
+POSTHOOK: Output: database:default
+POSTHOOK: Output: default@testwrongloc
+#### A masked pattern was here ####

Reply via email to