Repository: hive
Updated Branches:
  refs/heads/master 17e95c7c7 -> 2ccd06169


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/2ccd0616
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/2ccd0616
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/2ccd0616

Branch: refs/heads/master
Commit: 2ccd061691cd52ed9fa341b61590edb2c022b031
Parents: 17e95c7
Author: Daniel Dai <da...@hortonworks.com>
Authored: Fri Aug 14 15:28:44 2015 -0700
Committer: Daniel Dai <da...@hortonworks.com>
Committed: Fri Aug 14 15:28:44 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/2ccd0616/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 21625bc..9f8c756 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
@@ -23,6 +23,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;
@@ -137,6 +138,7 @@ import org.apache.hadoop.mapred.InputFormat;
 import org.apache.hadoop.mapred.TextInputFormat;
 import org.apache.hadoop.util.StringUtils;
 
+import java.io.FileNotFoundException;
 import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
@@ -1464,6 +1466,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/2ccd0616/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/2ccd0616/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